fixed golint/go vet

Change-Id: Id8aa01511f459d45f9dc3270e1d13b8682944016
This commit is contained in:
Stephen McQuay 2016-05-23 23:54:35 -07:00
parent f69efc8f36
commit 435e28966a
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D
5 changed files with 28 additions and 11 deletions

View File

@ -27,7 +27,7 @@ func TestAdd(t *testing.T) {
ts := httptest.NewServer(sm)
tok, err := db.addUser("sm@example.org")
if err != nil {
t.Error("failure to add user: %v", err)
t.Fatalf("failure to add user: %v", err)
}
resp, err := http.Get(ts.URL)
@ -173,7 +173,7 @@ func TestInvalidPath(t *testing.T) {
ts := httptest.NewServer(sm)
tok, err := db.addUser("sm@example.org")
if err != nil {
t.Error("failure to add user: %v", err)
t.Errorf("failure to add user: %v", err)
}
bad := ts.URL
@ -206,7 +206,7 @@ func TestCannotDuplicateExistingPath(t *testing.T) {
tok, err := db.addUser("sm@example.org")
if err != nil {
t.Error("failure to add user: %v", err)
t.Errorf("failure to add user: %v", err)
}
u := fmt.Sprintf("%s/foo", ts.URL)
@ -252,7 +252,7 @@ func TestCannotAddExistingSubPath(t *testing.T) {
tok, err := db.addUser("sm@example.org")
if err != nil {
t.Error("failure to add user: %v", err)
t.Fatalf("failure to add user: %v", err)
}
{
@ -300,7 +300,7 @@ func TestMissingRepo(t *testing.T) {
tok, err := db.addUser("sm@example.org")
if err != nil {
t.Error("failure to add user: %v", err)
t.Fatalf("failure to add user: %v", err)
}
u := fmt.Sprintf("%s/foo", ts.URL)
@ -333,7 +333,7 @@ func TestBadJson(t *testing.T) {
tok, err := db.addUser("sm@example.org")
if err != nil {
t.Error("failure to add user: %v", err)
t.Fatalf("failure to add user: %v", err)
}
u := fmt.Sprintf("%s/foo", ts.URL)
@ -395,7 +395,7 @@ func TestBadVcs(t *testing.T) {
tok, err := db.addUser("sm@example.org")
if err != nil {
t.Error("failure to add user: %v", err)
t.Fatalf("failure to add user: %v", err)
}
u := fmt.Sprintf("%s/foo", ts.URL)
@ -426,7 +426,7 @@ func TestUnsupportedMethod(t *testing.T) {
tok, err := db.addUser("sm@example.org")
if err != nil {
t.Error("failure to add user: %v", err)
t.Fatalf("failure to add user: %v", err)
}
url := fmt.Sprintf("%s/foo", ts.URL)
@ -458,7 +458,7 @@ func TestDelete(t *testing.T) {
tok, err := db.addUser("sm@example.org")
if err != nil {
t.Error("failure to add user: %v", err)
t.Fatalf("failure to add user: %v", err)
}
t.Logf("%v", tok)
if len(db.Pkgs()) != 0 {

View File

@ -116,7 +116,7 @@ func main() {
}
hostname := "localhost"
if hn, err := os.Hostname(); err != nil {
log.Printf("problem getting hostname:", err)
log.Printf("problem getting hostname: %v", err)
} else {
hostname = hn
}

16
db.go
View File

@ -7,16 +7,20 @@ import (
"time"
"github.com/jmoiron/sqlx"
// for side effects
_ "github.com/mattn/go-sqlite3"
verrors "mcquay.me/vain/errors"
vsql "mcquay.me/vain/sql"
)
// DB wraps a sqlx.DB connection and provides methods for interating with
// a vain database.
type DB struct {
conn *sqlx.DB
}
// NewDB opens a sqlite3 file, sets options, and reports errors.
func NewDB(path string) (*DB, error) {
conn, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc", path))
if _, err := conn.Exec("PRAGMA foreign_keys = ON"); err != nil {
@ -25,6 +29,7 @@ func NewDB(path string) (*DB, error) {
return &DB{conn}, err
}
// Init runs the embedded sql to initialize tables.
func (db *DB) Init() error {
content, err := vsql.Asset("sql/init.sql")
if err != nil {
@ -34,10 +39,12 @@ func (db *DB) Init() error {
return err
}
// Close the underlying connection.
func (db *DB) Close() error {
return db.conn.Close()
}
// AddPackage adds p into packages table.
func (db *DB) AddPackage(p Package) error {
_, err := db.conn.NamedExec(
"INSERT INTO packages(vcs, repo, path, ns) VALUES (:vcs, :repo, :path, :ns)",
@ -46,11 +53,13 @@ func (db *DB) AddPackage(p Package) error {
return err
}
// RemovePackage removes package with given path
func (db *DB) RemovePackage(path string) error {
_, err := db.conn.Exec("DELETE FROM packages WHERE path = ?", path)
return err
}
// Pkgs returns all packages from the database
func (db *DB) Pkgs() []Package {
r := []Package{}
rows, err := db.conn.Queryx("SELECT * FROM packages")
@ -70,6 +79,7 @@ func (db *DB) Pkgs() []Package {
return r
}
// PackageExists tells if a package with path is in the database.
func (db *DB) PackageExists(path string) bool {
var count int
if err := db.conn.Get(&count, "SELECT COUNT(*) FROM packages WHERE path = ?", path); err != nil {
@ -86,12 +96,14 @@ func (db *DB) PackageExists(path string) bool {
return r
}
// Package fetches the package associated with path.
func (db *DB) Package(path string) (Package, error) {
r := Package{}
err := db.conn.Get(&r, "SELECT * FROM packages WHERE path = ?", path)
return r, err
}
// NSForToken creates an entry namespaces with a relation to the token.
func (db *DB) NSForToken(ns string, tok string) error {
var err error
txn, err := db.conn.Beginx()
@ -148,13 +160,14 @@ func (db *DB) NSForToken(ns string, tok string) error {
}
default:
err = verrors.HTTP{
Message: fmt.Sprintf("inconsistent db; found %d results with ns (%s) with token (%s): %d", count, ns, tok),
Message: fmt.Sprintf("inconsistent db; found %d results with ns (%s) with token (%s)", count, ns, tok),
Code: http.StatusInternalServerError,
}
}
return err
}
// Register adds email to the database, returning an error if there was one.
func (db *DB) Register(email string) (string, error) {
var err error
txn, err := db.conn.Beginx()
@ -197,6 +210,7 @@ func (db *DB) Register(email string) (string, error) {
return tok, err
}
// Confirm modifies the user with the given token. Used on register confirmation.
func (db *DB) Confirm(token string) (string, error) {
var err error
txn, err := db.conn.Beginx()

View File

@ -5,6 +5,7 @@ import (
"net/http"
)
// HTTP implements error and keeps track of http return codes.
type HTTP struct {
error
Message string
@ -15,6 +16,7 @@ func (e HTTP) Error() string {
return fmt.Sprintf("%d: %s", e.Code, e.Message)
}
// ToHTTP wraps the type assertion to change an error into an HTTP.
func ToHTTP(err error) *HTTP {
if err == nil {
return nil

View File

@ -69,6 +69,7 @@ func parseNamespace(path string) (string, error) {
return elems[0], nil
}
// FreshToken returns a random token string.
func FreshToken() string {
buf := &bytes.Buffer{}
io.Copy(buf, io.LimitReader(rand.Reader, 6))