Skip to content

Commit b188898

Browse files
authored
Error message cleanup (#12)
* additional testing * increased linting, bumped sdk for better errors * updated golang ci version
1 parent 32afd9a commit b188898

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+523
-211
lines changed

.golangci.yml

+36
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
11
issues:
22
exclude:
33
SA1019
4+
linters:
5+
disable-all: true
6+
enable:
7+
- bodyclose
8+
- deadcode
9+
- depguard
10+
- dogsled
11+
- dupl
12+
- errcheck
13+
- exhaustive
14+
- funlen
15+
- goconst
16+
- gocritic
17+
- gocyclo
18+
- gofmt
19+
- goimports
20+
- golint
21+
- gomnd
22+
- goprintffuncname
23+
- gosec
24+
- gosimple
25+
- govet
26+
- ineffassign
27+
- misspell
28+
- nakedret
29+
- nolintlint
30+
- rowserrcheck
31+
- scopelint
32+
- staticcheck
33+
- structcheck
34+
- typecheck
35+
- unconvert
36+
- unparam
37+
- unused
38+
- varcheck
39+
- whitespace

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Apache 2.0 licensed Astra Cloud Management CLI
1010

1111
## status
1212

13-
- Alpha
13+
Ready for production
1414

1515
## How to install - Homebrew for Mac and Linux
1616

cmd/db.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//Package cmd contains all fo the commands for the cli
15+
// Package cmd contains all fo the commands for the cli
1616
package cmd
1717

1818
import (

cmd/db/create.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//Package db is where the Astra DB commands are
15+
// Package db is where the Astra DB commands are
1616
package db
1717

1818
import (
@@ -42,10 +42,9 @@ func init() {
4242
CreateCmd.Flags().StringVarP(&createDbTier, "tier", "t", "serverless", "tier to give to the Astra Database")
4343
CreateCmd.Flags().IntVarP(&createDbCapacityUnit, "capacityUnit", "c", 1, "capacityUnit flag to give to the Astra Database")
4444
CreateCmd.Flags().StringVarP(&createDbCloudProvider, "cloudProvider", "l", "GCP", "cloud provider flag to give to the Astra Database")
45-
4645
}
4746

48-
//CreateCmd creates a database in Astra
47+
// CreateCmd creates a database in Astra
4948
var CreateCmd = &cobra.Command{
5049
Use: "create",
5150
Short: "creates a database by id",

cmd/db/create_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//Package db is where the Astra DB commands are
15+
// Package db is where the Astra DB commands are
1616
package db
1717

1818
import (
@@ -26,7 +26,7 @@ import (
2626

2727
func TestCreateGetsId(t *testing.T) {
2828
expectedID := "abcd"
29-
//setting package variables by hand, there be dragons
29+
// setting package variables by hand, there be dragons
3030
mockClient := &tests.MockClient{
3131
Databases: []astraops.Database{
3232
{
@@ -46,7 +46,7 @@ func TestCreateGetsId(t *testing.T) {
4646
}
4747
}
4848
func TestCreateLoginFails(t *testing.T) {
49-
//setting package variables by hand, there be dragons
49+
// setting package variables by hand, there be dragons
5050
mockClient := &tests.MockClient{}
5151
err := executeCreate(func() (pkg.Client, error) {
5252
return mockClient, fmt.Errorf("service down")
@@ -65,7 +65,7 @@ func TestCreateLoginFails(t *testing.T) {
6565
}
6666

6767
func TestCreateFails(t *testing.T) {
68-
//setting package variables by hand, there be dragons
68+
// setting package variables by hand, there be dragons
6969
mockClient := &tests.MockClient{
7070
ErrorQueue: []error{fmt.Errorf("service down")},
7171
}

cmd/db/delete.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//Package db provides the sub-commands for the db command
15+
// Package db provides the sub-commands for the db command
1616
package db
1717

1818
import (
@@ -23,7 +23,7 @@ import (
2323
"github.com/spf13/cobra"
2424
)
2525

26-
//DeleteCmd provides the delete database command
26+
// DeleteCmd provides the delete database command
2727
var DeleteCmd = &cobra.Command{
2828
Use: "delete <id>",
2929
Short: "delete database by databaseID",

cmd/db/delete_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//Package db is where the Astra DB commands are
15+
// Package db is where the Astra DB commands are
1616
package db
1717

1818
import (
@@ -62,7 +62,7 @@ func TestDeleteLoginError(t *testing.T) {
6262
}
6363

6464
if "" != msg {
65-
t.Errorf("expected emtpy but was '%v'", msg)
65+
t.Errorf("expected empty but was '%v'", msg)
6666
}
6767
}
6868

cmd/db/get.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//Package db provides the sub-commands for the db command
15+
// Package db provides the sub-commands for the db command
1616
package db
1717

1818
import (
@@ -32,7 +32,7 @@ func init() {
3232
GetCmd.Flags().StringVarP(&getFmt, "output", "o", "text", "Output format for report default is text")
3333
}
3434

35-
//GetCmd provides the get database command
35+
// GetCmd provides the get database command
3636
var GetCmd = &cobra.Command{
3737
Use: "get <id>",
3838
Short: "get database by databaseID",
@@ -60,7 +60,7 @@ func executeGet(args []string, login func() (pkg.Client, error)) (string, error)
6060
return "", fmt.Errorf("unable to get '%s' with error %v", id, err)
6161
}
6262
switch getFmt {
63-
case "text":
63+
case pkg.TextFormat:
6464
var rows [][]string
6565
rows = append(rows, []string{"name", "id", "status"})
6666
rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)})
@@ -70,13 +70,13 @@ func executeGet(args []string, login func() (pkg.Client, error)) (string, error)
7070
return "", fmt.Errorf("unexpected error writing out text %v", err)
7171
}
7272
return buf.String(), nil
73-
case "json":
73+
case pkg.JSONFormat:
7474
b, err := json.MarshalIndent(db, "", " ")
7575
if err != nil {
7676
return "", fmt.Errorf("unexpected error marshaling to json: '%v', Try -output text instead", err)
7777
}
7878
return string(b), nil
7979
default:
80-
return "", fmt.Errorf("-output %q is not valid option", getFmt)
80+
return "", fmt.Errorf("-o %q is not valid option", getFmt)
8181
}
8282
}

cmd/db/get_test.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//Package db is where the Astra DB commands are
15+
// Package db is where the Astra DB commands are
1616
package db
1717

1818
import (
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
func TestGet(t *testing.T) {
29-
getFmt = "json"
29+
getFmt = pkg.JSONFormat
3030
dbs := []astraops.Database{
3131
{ID: "1"},
3232
{ID: "2"},
@@ -35,7 +35,6 @@ func TestGet(t *testing.T) {
3535
return &tests.MockClient{
3636
Databases: dbs,
3737
}, nil
38-
3938
})
4039
if err != nil {
4140
t.Fatalf("unexpected error %v", err)
@@ -51,7 +50,7 @@ func TestGet(t *testing.T) {
5150
}
5251

5352
func TestGetText(t *testing.T) {
54-
getFmt = "text"
53+
getFmt = pkg.TextFormat
5554
dbs := []astraops.Database{
5655
{
5756
ID: "1",
@@ -72,7 +71,6 @@ func TestGetText(t *testing.T) {
7271
return &tests.MockClient{
7372
Databases: dbs,
7473
}, nil
75-
7674
})
7775
if err != nil {
7876
t.Fatalf("unexpected error %v", err)
@@ -86,3 +84,17 @@ func TestGetText(t *testing.T) {
8684
t.Errorf("expected '%v' but was '%v'", expected, txt)
8785
}
8886
}
87+
88+
func TestGetInvalidFmt(t *testing.T) {
89+
getFmt = "badham"
90+
_, err := executeGet([]string{"abc"}, func() (pkg.Client, error) {
91+
return &tests.MockClient{}, nil
92+
})
93+
if err == nil {
94+
t.Fatalf("unexpected error %v", err)
95+
}
96+
expected := "-o \"badham\" is not valid option"
97+
if err.Error() != expected {
98+
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
99+
}
100+
}

cmd/db/list.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//Package db provides the sub-commands for the db command
15+
// Package db provides the sub-commands for the db command
1616
package db
1717

1818
import (
@@ -40,14 +40,14 @@ func init() {
4040
ListCmd.Flags().StringVarP(&listFmt, "output", "o", "text", "Output format for report default is json")
4141
}
4242

43-
//ListCmd provides the list databases command
43+
// ListCmd provides the list databases command
4444
var ListCmd = &cobra.Command{
4545
Use: "list",
4646
Short: "lists all databases",
4747
Long: `lists all databases in your Astra account`,
4848
Run: func(cmd *cobra.Command, args []string) {
4949
creds := &pkg.Creds{}
50-
msg, err := executeList(args, creds.Login)
50+
msg, err := executeList(creds.Login)
5151
if err != nil {
5252
fmt.Fprintf(os.Stderr, "%v\n", err)
5353
os.Exit(1)
@@ -56,7 +56,7 @@ var ListCmd = &cobra.Command{
5656
},
5757
}
5858

59-
func executeList(args []string, login func() (pkg.Client, error)) (string, error) {
59+
func executeList(login func() (pkg.Client, error)) (string, error) {
6060
client, err := login()
6161
if err != nil {
6262
return "", fmt.Errorf("unable to login with error '%v'", err)
@@ -66,7 +66,7 @@ func executeList(args []string, login func() (pkg.Client, error)) (string, error
6666
return "", fmt.Errorf("unable to get list of dbs with error '%v'", err)
6767
}
6868
switch listFmt {
69-
case "text":
69+
case pkg.TextFormat:
7070
var rows [][]string
7171
rows = append(rows, []string{"name", "id", "status"})
7272
for _, db := range dbs {
@@ -78,13 +78,13 @@ func executeList(args []string, login func() (pkg.Client, error)) (string, error
7878
return "", fmt.Errorf("unexpected error writing text output '%v'", err)
7979
}
8080
return out.String(), nil
81-
case "json":
81+
case pkg.JSONFormat:
8282
b, err := json.MarshalIndent(dbs, "", " ")
8383
if err != nil {
8484
return "", fmt.Errorf("unexpected error marshaling to json: '%v', Try -output text instead", err)
8585
}
8686
return string(b), nil
8787
default:
88-
return "", fmt.Errorf("-output %q is not valid option", getFmt)
88+
return "", fmt.Errorf("-o %q is not valid option", listFmt)
8989
}
9090
}

cmd/db/list_test.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//Package db is where the Astra DB commands are
15+
// Package db is where the Astra DB commands are
1616
package db
1717

1818
import (
@@ -26,16 +26,15 @@ import (
2626
)
2727

2828
func TestList(t *testing.T) {
29-
listFmt = "json"
29+
listFmt = pkg.JSONFormat
3030
dbs := []astraops.Database{
3131
{ID: "1"},
3232
{ID: "2"},
3333
}
34-
jsonTxt, err := executeList([]string{"", "", "", "10"}, func() (pkg.Client, error) {
34+
jsonTxt, err := executeList(func() (pkg.Client, error) {
3535
return &tests.MockClient{
3636
Databases: dbs,
3737
}, nil
38-
3938
})
4039
if err != nil {
4140
t.Fatalf("unexpected error %v", err)
@@ -57,7 +56,7 @@ func TestList(t *testing.T) {
5756
}
5857

5958
func TestListText(t *testing.T) {
60-
listFmt = "text"
59+
listFmt = pkg.TextFormat
6160
dbs := []astraops.Database{
6261
{
6362
ID: "1",
@@ -74,11 +73,10 @@ func TestListText(t *testing.T) {
7473
Status: astraops.TERMINATING,
7574
},
7675
}
77-
txt, err := executeList([]string{"", "", "", "10"}, func() (pkg.Client, error) {
76+
txt, err := executeList(func() (pkg.Client, error) {
7877
return &tests.MockClient{
7978
Databases: dbs,
8079
}, nil
81-
8280
})
8381
if err != nil {
8482
t.Fatalf("unexpected error %v", err)
@@ -93,3 +91,17 @@ func TestListText(t *testing.T) {
9391
t.Errorf("expected '%v' but was '%v'", expected, txt)
9492
}
9593
}
94+
95+
func TestListInvalidFmt(t *testing.T) {
96+
listFmt = "listham"
97+
_, err := executeList(func() (pkg.Client, error) {
98+
return &tests.MockClient{}, nil
99+
})
100+
if err == nil {
101+
t.Fatalf("unexpected error %v", err)
102+
}
103+
expected := "-o \"listham\" is not valid option"
104+
if err.Error() != expected {
105+
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
106+
}
107+
}

0 commit comments

Comments
 (0)