1
0
forked from sm/vain

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

View File

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

16
db.go
View File

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

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
) )
// HTTP implements error and keeps track of http return codes.
type HTTP struct { type HTTP struct {
error error
Message string Message string
@ -15,6 +16,7 @@ func (e HTTP) Error() string {
return fmt.Sprintf("%d: %s", e.Code, e.Message) 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 { func ToHTTP(err error) *HTTP {
if err == nil { if err == nil {
return nil return nil

View File

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