Browse Source

fixed golint/go vet

Change-Id: Id8aa01511f459d45f9dc3270e1d13b8682944016
Stephen McQuay 3 years ago
parent
commit
435e28966a
No known key found for this signature in database
5 changed files with 28 additions and 11 deletions
  1. 9
    9
      api_test.go
  2. 1
    1
      cmd/vaind/main.go
  3. 15
    1
      db.go
  4. 2
    0
      errors/errors.go
  5. 1
    0
      vain.go

+ 9
- 9
api_test.go View File

@@ -27,7 +27,7 @@ func TestAdd(t *testing.T) {
27 27
 	ts := httptest.NewServer(sm)
28 28
 	tok, err := db.addUser("sm@example.org")
29 29
 	if err != nil {
30
-		t.Error("failure to add user: %v", err)
30
+		t.Fatalf("failure to add user: %v", err)
31 31
 	}
32 32
 
33 33
 	resp, err := http.Get(ts.URL)
@@ -173,7 +173,7 @@ func TestInvalidPath(t *testing.T) {
173 173
 	ts := httptest.NewServer(sm)
174 174
 	tok, err := db.addUser("sm@example.org")
175 175
 	if err != nil {
176
-		t.Error("failure to add user: %v", err)
176
+		t.Errorf("failure to add user: %v", err)
177 177
 	}
178 178
 
179 179
 	bad := ts.URL
@@ -206,7 +206,7 @@ func TestCannotDuplicateExistingPath(t *testing.T) {
206 206
 
207 207
 	tok, err := db.addUser("sm@example.org")
208 208
 	if err != nil {
209
-		t.Error("failure to add user: %v", err)
209
+		t.Errorf("failure to add user: %v", err)
210 210
 	}
211 211
 
212 212
 	u := fmt.Sprintf("%s/foo", ts.URL)
@@ -252,7 +252,7 @@ func TestCannotAddExistingSubPath(t *testing.T) {
252 252
 
253 253
 	tok, err := db.addUser("sm@example.org")
254 254
 	if err != nil {
255
-		t.Error("failure to add user: %v", err)
255
+		t.Fatalf("failure to add user: %v", err)
256 256
 	}
257 257
 
258 258
 	{
@@ -300,7 +300,7 @@ func TestMissingRepo(t *testing.T) {
300 300
 
301 301
 	tok, err := db.addUser("sm@example.org")
302 302
 	if err != nil {
303
-		t.Error("failure to add user: %v", err)
303
+		t.Fatalf("failure to add user: %v", err)
304 304
 	}
305 305
 
306 306
 	u := fmt.Sprintf("%s/foo", ts.URL)
@@ -333,7 +333,7 @@ func TestBadJson(t *testing.T) {
333 333
 
334 334
 	tok, err := db.addUser("sm@example.org")
335 335
 	if err != nil {
336
-		t.Error("failure to add user: %v", err)
336
+		t.Fatalf("failure to add user: %v", err)
337 337
 	}
338 338
 
339 339
 	u := fmt.Sprintf("%s/foo", ts.URL)
@@ -395,7 +395,7 @@ func TestBadVcs(t *testing.T) {
395 395
 
396 396
 	tok, err := db.addUser("sm@example.org")
397 397
 	if err != nil {
398
-		t.Error("failure to add user: %v", err)
398
+		t.Fatalf("failure to add user: %v", err)
399 399
 	}
400 400
 
401 401
 	u := fmt.Sprintf("%s/foo", ts.URL)
@@ -426,7 +426,7 @@ func TestUnsupportedMethod(t *testing.T) {
426 426
 
427 427
 	tok, err := db.addUser("sm@example.org")
428 428
 	if err != nil {
429
-		t.Error("failure to add user: %v", err)
429
+		t.Fatalf("failure to add user: %v", err)
430 430
 	}
431 431
 
432 432
 	url := fmt.Sprintf("%s/foo", ts.URL)
@@ -458,7 +458,7 @@ func TestDelete(t *testing.T) {
458 458
 
459 459
 	tok, err := db.addUser("sm@example.org")
460 460
 	if err != nil {
461
-		t.Error("failure to add user: %v", err)
461
+		t.Fatalf("failure to add user: %v", err)
462 462
 	}
463 463
 	t.Logf("%v", tok)
464 464
 	if len(db.Pkgs()) != 0 {

+ 1
- 1
cmd/vaind/main.go View File

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

+ 15
- 1
db.go View File

@@ -7,16 +7,20 @@ import (
7 7
 	"time"
8 8
 
9 9
 	"github.com/jmoiron/sqlx"
10
+	// for side effects
10 11
 	_ "github.com/mattn/go-sqlite3"
11 12
 
12 13
 	verrors "mcquay.me/vain/errors"
13 14
 	vsql "mcquay.me/vain/sql"
14 15
 )
15 16
 
17
+// DB wraps a sqlx.DB connection and provides methods for interating with
18
+// a vain database.
16 19
 type DB struct {
17 20
 	conn *sqlx.DB
18 21
 }
19 22
 
23
+// NewDB opens a sqlite3 file, sets options, and reports errors.
20 24
 func NewDB(path string) (*DB, error) {
21 25
 	conn, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc", path))
22 26
 	if _, err := conn.Exec("PRAGMA foreign_keys = ON"); err != nil {
@@ -25,6 +29,7 @@ func NewDB(path string) (*DB, error) {
25 29
 	return &DB{conn}, err
26 30
 }
27 31
 
32
+// Init runs the embedded sql to initialize tables.
28 33
 func (db *DB) Init() error {
29 34
 	content, err := vsql.Asset("sql/init.sql")
30 35
 	if err != nil {
@@ -34,10 +39,12 @@ func (db *DB) Init() error {
34 39
 	return err
35 40
 }
36 41
 
42
+// Close the underlying connection.
37 43
 func (db *DB) Close() error {
38 44
 	return db.conn.Close()
39 45
 }
40 46
 
47
+// AddPackage adds p into packages table.
41 48
 func (db *DB) AddPackage(p Package) error {
42 49
 	_, err := db.conn.NamedExec(
43 50
 		"INSERT INTO packages(vcs, repo, path, ns) VALUES (:vcs, :repo, :path, :ns)",
@@ -46,11 +53,13 @@ func (db *DB) AddPackage(p Package) error {
46 53
 	return err
47 54
 }
48 55
 
56
+// RemovePackage removes package with given path
49 57
 func (db *DB) RemovePackage(path string) error {
50 58
 	_, err := db.conn.Exec("DELETE FROM packages WHERE path = ?", path)
51 59
 	return err
52 60
 }
53 61
 
62
+// Pkgs returns all packages from the database
54 63
 func (db *DB) Pkgs() []Package {
55 64
 	r := []Package{}
56 65
 	rows, err := db.conn.Queryx("SELECT * FROM packages")
@@ -70,6 +79,7 @@ func (db *DB) Pkgs() []Package {
70 79
 	return r
71 80
 }
72 81
 
82
+// PackageExists tells if a package with path is in the database.
73 83
 func (db *DB) PackageExists(path string) bool {
74 84
 	var count int
75 85
 	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 {
86 96
 	return r
87 97
 }
88 98
 
99
+// Package fetches the package associated with path.
89 100
 func (db *DB) Package(path string) (Package, error) {
90 101
 	r := Package{}
91 102
 	err := db.conn.Get(&r, "SELECT * FROM packages WHERE path = ?", path)
92 103
 	return r, err
93 104
 }
94 105
 
106
+// NSForToken creates an entry namespaces with a relation to the token.
95 107
 func (db *DB) NSForToken(ns string, tok string) error {
96 108
 	var err error
97 109
 	txn, err := db.conn.Beginx()
@@ -148,13 +160,14 @@ func (db *DB) NSForToken(ns string, tok string) error {
148 160
 		}
149 161
 	default:
150 162
 		err = verrors.HTTP{
151
-			Message: fmt.Sprintf("inconsistent db; found %d results with ns (%s) with token (%s): %d", count, ns, tok),
163
+			Message: fmt.Sprintf("inconsistent db; found %d results with ns (%s) with token (%s)", count, ns, tok),
152 164
 			Code:    http.StatusInternalServerError,
153 165
 		}
154 166
 	}
155 167
 	return err
156 168
 }
157 169
 
170
+// Register adds email to the database, returning an error if there was one.
158 171
 func (db *DB) Register(email string) (string, error) {
159 172
 	var err error
160 173
 	txn, err := db.conn.Beginx()
@@ -197,6 +210,7 @@ func (db *DB) Register(email string) (string, error) {
197 210
 	return tok, err
198 211
 }
199 212
 
213
+// Confirm  modifies the user with the given token. Used on register confirmation.
200 214
 func (db *DB) Confirm(token string) (string, error) {
201 215
 	var err error
202 216
 	txn, err := db.conn.Beginx()

+ 2
- 0
errors/errors.go View File

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

+ 1
- 0
vain.go View File

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

Loading…
Cancel
Save