Skip to content

Commit

Permalink
Error message cleanup (#12)
Browse files Browse the repository at this point in the history
* additional testing
* increased linting, bumped sdk for better errors
* updated golang ci version
  • Loading branch information
Ryan SVIHLA authored and foundev committed Mar 31, 2021
1 parent 322339a commit a105c7c
Show file tree
Hide file tree
Showing 43 changed files with 523 additions and 211 deletions.
36 changes: 36 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
issues:
exclude:
SA1019
linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- exhaustive
- funlen
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- golint
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Apache 2.0 licensed Astra Cloud Management CLI

## status

- Alpha
Ready for production

## How to install - Homebrew for Mac and Linux

Expand Down
2 changes: 1 addition & 1 deletion cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//Package cmd contains all fo the commands for the cli
// Package cmd contains all fo the commands for the cli
package cmd

import (
Expand Down
5 changes: 2 additions & 3 deletions cmd/db/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//Package db is where the Astra DB commands are
// Package db is where the Astra DB commands are
package db

import (
Expand Down Expand Up @@ -42,10 +42,9 @@ func init() {
CreateCmd.Flags().StringVarP(&createDbTier, "tier", "t", "serverless", "tier to give to the Astra Database")
CreateCmd.Flags().IntVarP(&createDbCapacityUnit, "capacityUnit", "c", 1, "capacityUnit flag to give to the Astra Database")
CreateCmd.Flags().StringVarP(&createDbCloudProvider, "cloudProvider", "l", "GCP", "cloud provider flag to give to the Astra Database")

}

//CreateCmd creates a database in Astra
// CreateCmd creates a database in Astra
var CreateCmd = &cobra.Command{
Use: "create",
Short: "creates a database by id",
Expand Down
8 changes: 4 additions & 4 deletions cmd/db/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//Package db is where the Astra DB commands are
// Package db is where the Astra DB commands are
package db

import (
Expand All @@ -26,7 +26,7 @@ import (

func TestCreateGetsId(t *testing.T) {
expectedID := "abcd"
//setting package variables by hand, there be dragons
// setting package variables by hand, there be dragons
mockClient := &tests.MockClient{
Databases: []astraops.Database{
{
Expand All @@ -46,7 +46,7 @@ func TestCreateGetsId(t *testing.T) {
}
}
func TestCreateLoginFails(t *testing.T) {
//setting package variables by hand, there be dragons
// setting package variables by hand, there be dragons
mockClient := &tests.MockClient{}
err := executeCreate(func() (pkg.Client, error) {
return mockClient, fmt.Errorf("service down")
Expand All @@ -65,7 +65,7 @@ func TestCreateLoginFails(t *testing.T) {
}

func TestCreateFails(t *testing.T) {
//setting package variables by hand, there be dragons
// setting package variables by hand, there be dragons
mockClient := &tests.MockClient{
ErrorQueue: []error{fmt.Errorf("service down")},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/db/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//Package db provides the sub-commands for the db command
// Package db provides the sub-commands for the db command
package db

import (
Expand All @@ -23,7 +23,7 @@ import (
"github.com/spf13/cobra"
)

//DeleteCmd provides the delete database command
// DeleteCmd provides the delete database command
var DeleteCmd = &cobra.Command{
Use: "delete <id>",
Short: "delete database by databaseID",
Expand Down
4 changes: 2 additions & 2 deletions cmd/db/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//Package db is where the Astra DB commands are
// Package db is where the Astra DB commands are
package db

import (
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestDeleteLoginError(t *testing.T) {
}

if "" != msg {
t.Errorf("expected emtpy but was '%v'", msg)
t.Errorf("expected empty but was '%v'", msg)
}
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/db/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//Package db provides the sub-commands for the db command
// Package db provides the sub-commands for the db command
package db

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

//GetCmd provides the get database command
// GetCmd provides the get database command
var GetCmd = &cobra.Command{
Use: "get <id>",
Short: "get database by databaseID",
Expand Down Expand Up @@ -60,7 +60,7 @@ func executeGet(args []string, login func() (pkg.Client, error)) (string, error)
return "", fmt.Errorf("unable to get '%s' with error %v", id, err)
}
switch getFmt {
case "text":
case pkg.TextFormat:
var rows [][]string
rows = append(rows, []string{"name", "id", "status"})
rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)})
Expand All @@ -70,13 +70,13 @@ func executeGet(args []string, login func() (pkg.Client, error)) (string, error)
return "", fmt.Errorf("unexpected error writing out text %v", err)
}
return buf.String(), nil
case "json":
case pkg.JSONFormat:
b, err := json.MarshalIndent(db, "", " ")
if err != nil {
return "", fmt.Errorf("unexpected error marshaling to json: '%v', Try -output text instead", err)
}
return string(b), nil
default:
return "", fmt.Errorf("-output %q is not valid option", getFmt)
return "", fmt.Errorf("-o %q is not valid option", getFmt)
}
}
22 changes: 17 additions & 5 deletions cmd/db/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//Package db is where the Astra DB commands are
// Package db is where the Astra DB commands are
package db

import (
Expand All @@ -26,7 +26,7 @@ import (
)

func TestGet(t *testing.T) {
getFmt = "json"
getFmt = pkg.JSONFormat
dbs := []astraops.Database{
{ID: "1"},
{ID: "2"},
Expand All @@ -35,7 +35,6 @@ func TestGet(t *testing.T) {
return &tests.MockClient{
Databases: dbs,
}, nil

})
if err != nil {
t.Fatalf("unexpected error %v", err)
Expand All @@ -51,7 +50,7 @@ func TestGet(t *testing.T) {
}

func TestGetText(t *testing.T) {
getFmt = "text"
getFmt = pkg.TextFormat
dbs := []astraops.Database{
{
ID: "1",
Expand All @@ -72,7 +71,6 @@ func TestGetText(t *testing.T) {
return &tests.MockClient{
Databases: dbs,
}, nil

})
if err != nil {
t.Fatalf("unexpected error %v", err)
Expand All @@ -86,3 +84,17 @@ func TestGetText(t *testing.T) {
t.Errorf("expected '%v' but was '%v'", expected, txt)
}
}

func TestGetInvalidFmt(t *testing.T) {
getFmt = "badham"
_, err := executeGet([]string{"abc"}, func() (pkg.Client, error) {
return &tests.MockClient{}, nil
})
if err == nil {
t.Fatalf("unexpected error %v", err)
}
expected := "-o \"badham\" is not valid option"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
}
14 changes: 7 additions & 7 deletions cmd/db/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//Package db provides the sub-commands for the db command
// Package db provides the sub-commands for the db command
package db

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

//ListCmd provides the list databases command
// ListCmd provides the list databases command
var ListCmd = &cobra.Command{
Use: "list",
Short: "lists all databases",
Long: `lists all databases in your Astra account`,
Run: func(cmd *cobra.Command, args []string) {
creds := &pkg.Creds{}
msg, err := executeList(args, creds.Login)
msg, err := executeList(creds.Login)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
Expand All @@ -56,7 +56,7 @@ var ListCmd = &cobra.Command{
},
}

func executeList(args []string, login func() (pkg.Client, error)) (string, error) {
func executeList(login func() (pkg.Client, error)) (string, error) {
client, err := login()
if err != nil {
return "", fmt.Errorf("unable to login with error '%v'", err)
Expand All @@ -66,7 +66,7 @@ func executeList(args []string, login func() (pkg.Client, error)) (string, error
return "", fmt.Errorf("unable to get list of dbs with error '%v'", err)
}
switch listFmt {
case "text":
case pkg.TextFormat:
var rows [][]string
rows = append(rows, []string{"name", "id", "status"})
for _, db := range dbs {
Expand All @@ -78,13 +78,13 @@ func executeList(args []string, login func() (pkg.Client, error)) (string, error
return "", fmt.Errorf("unexpected error writing text output '%v'", err)
}
return out.String(), nil
case "json":
case pkg.JSONFormat:
b, err := json.MarshalIndent(dbs, "", " ")
if err != nil {
return "", fmt.Errorf("unexpected error marshaling to json: '%v', Try -output text instead", err)
}
return string(b), nil
default:
return "", fmt.Errorf("-output %q is not valid option", getFmt)
return "", fmt.Errorf("-o %q is not valid option", listFmt)
}
}
26 changes: 19 additions & 7 deletions cmd/db/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//Package db is where the Astra DB commands are
// Package db is where the Astra DB commands are
package db

import (
Expand All @@ -26,16 +26,15 @@ import (
)

func TestList(t *testing.T) {
listFmt = "json"
listFmt = pkg.JSONFormat
dbs := []astraops.Database{
{ID: "1"},
{ID: "2"},
}
jsonTxt, err := executeList([]string{"", "", "", "10"}, func() (pkg.Client, error) {
jsonTxt, err := executeList(func() (pkg.Client, error) {
return &tests.MockClient{
Databases: dbs,
}, nil

})
if err != nil {
t.Fatalf("unexpected error %v", err)
Expand All @@ -57,7 +56,7 @@ func TestList(t *testing.T) {
}

func TestListText(t *testing.T) {
listFmt = "text"
listFmt = pkg.TextFormat
dbs := []astraops.Database{
{
ID: "1",
Expand All @@ -74,11 +73,10 @@ func TestListText(t *testing.T) {
Status: astraops.TERMINATING,
},
}
txt, err := executeList([]string{"", "", "", "10"}, func() (pkg.Client, error) {
txt, err := executeList(func() (pkg.Client, error) {
return &tests.MockClient{
Databases: dbs,
}, nil

})
if err != nil {
t.Fatalf("unexpected error %v", err)
Expand All @@ -93,3 +91,17 @@ func TestListText(t *testing.T) {
t.Errorf("expected '%v' but was '%v'", expected, txt)
}
}

func TestListInvalidFmt(t *testing.T) {
listFmt = "listham"
_, err := executeList(func() (pkg.Client, error) {
return &tests.MockClient{}, nil
})
if err == nil {
t.Fatalf("unexpected error %v", err)
}
expected := "-o \"listham\" is not valid option"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
}
Loading

0 comments on commit a105c7c

Please sign in to comment.