diff --git a/README.md b/README.md index 1a2c1ff9..f643b68d 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,5 @@ Please refer to [commands](/COMMANDS.md). MIT [![Build Status](https://travis-ci.org/dnote-io/cli.svg?branch=master)](https://travis-ci.org/dnote-io/cli) +[![Go Report Card](https://goreportcard.com/badge/github.com/dnote-io/cli)](https://goreportcard.com/report/github.com/dnote-io/cli) +[![GolangCI](https://golangci.com/badges/github.com/dnote-io/cli.svg)](https://golangci.com) diff --git a/cmd/edit/edit.go b/cmd/edit/edit.go index 876fd94e..bd59f612 100644 --- a/cmd/edit/edit.go +++ b/cmd/edit/edit.go @@ -71,8 +71,8 @@ func newRun(ctx infra.DnoteCtx) core.RunEFunc { fpath := core.GetDnoteTmpContentPath(ctx) e := ioutil.WriteFile(fpath, []byte(targetNote.Content), 0644) - if err != nil { - return errors.Wrap(err, "Failed to prepare editor content") + if e != nil { + return errors.Wrap(e, "Failed to prepare editor content") } e = core.GetEditorInput(ctx, fpath, &newContent) diff --git a/cmd/login/login.go b/cmd/login/login.go index 3023e4c0..03222d53 100644 --- a/cmd/login/login.go +++ b/cmd/login/login.go @@ -36,7 +36,9 @@ func newRun(ctx infra.DnoteCtx) core.RunEFunc { log.Printf("API key: ") var apiKey string - fmt.Scanln(&apiKey) + if _, err := fmt.Scanln(&apiKey); err != nil { + return err + } if apiKey == "" { return errors.New("Empty API key") diff --git a/cmd/sync/sync.go b/cmd/sync/sync.go index e309a6e2..85ad443b 100644 --- a/cmd/sync/sync.go +++ b/cmd/sync/sync.go @@ -5,6 +5,7 @@ import ( "compress/gzip" "encoding/json" "fmt" + "io" "io/ioutil" "net/http" @@ -100,6 +101,9 @@ func newRun(ctx infra.DnoteCtx) core.RunEFunc { // Update bookmark ts, err := core.ReadTimestamp(ctx) + if err != nil { + return errors.Wrap(err, "Failed to read the timestamp") + } ts.Bookmark = respData.Bookmark err = core.WriteTimestamp(ctx, ts) @@ -157,7 +161,7 @@ func compressActions(actions []core.Action) ([]byte, error) { return buf.Bytes(), nil } -func postActions(ctx infra.DnoteCtx, APIKey string, payload *bytes.Buffer) (*http.Response, error) { +func postActions(ctx infra.DnoteCtx, APIKey string, payload io.Reader) (*http.Response, error) { endpoint := fmt.Sprintf("%s/v1/sync", ctx.APIEndpoint) req, err := http.NewRequest("POST", endpoint, payload) if err != nil { diff --git a/core/core.go b/core/core.go index 487bac1c..ef8685e0 100644 --- a/core/core.go +++ b/core/core.go @@ -242,12 +242,7 @@ func WriteDnote(ctx infra.DnoteCtx, dnote infra.Dnote) error { notePath := GetDnotePath(ctx) - err = ioutil.WriteFile(notePath, d, 0644) - if err != nil { - return err - } - - return nil + return ioutil.WriteFile(notePath, d, 0644) } func WriteConfig(ctx infra.DnoteCtx, config infra.Config) error { @@ -258,12 +253,7 @@ func WriteConfig(ctx infra.DnoteCtx, config infra.Config) error { configPath := GetConfigPath(ctx) - err = ioutil.WriteFile(configPath, d, 0644) - if err != nil { - return err - } - - return nil + return ioutil.WriteFile(configPath, d, 0644) } // LogAction appends the action to the action log and updates the last_action @@ -297,12 +287,7 @@ func WriteActionLog(ctx infra.DnoteCtx, actions []Action) error { return errors.Wrap(err, "Failed to marshal newly generated actions to JSON") } - err = ioutil.WriteFile(path, d, 0644) - if err != nil { - return err - } - - return nil + return ioutil.WriteFile(path, d, 0644) } func ClearActionLog(ctx infra.DnoteCtx) error { @@ -336,11 +321,7 @@ func ReadActionLog(ctx infra.DnoteCtx) ([]Action, error) { } err = json.Unmarshal(b, &ret) - if err != nil { - return ret, err - } - - return ret, nil + return ret, err } func ReadConfig(ctx infra.DnoteCtx) (infra.Config, error) { @@ -353,11 +334,7 @@ func ReadConfig(ctx infra.DnoteCtx) (infra.Config, error) { } err = yaml.Unmarshal(b, &ret) - if err != nil { - return ret, err - } - - return ret, nil + return ret, err } func UpdateLastActionTimestamp(ctx infra.DnoteCtx, val int64) error { diff --git a/core/reducer_test.go b/core/reducer_test.go index cce8074c..36c8780f 100644 --- a/core/reducer_test.go +++ b/core/reducer_test.go @@ -27,7 +27,7 @@ func TestReduceAddNote(t *testing.T) { Data: b, Timestamp: 1517629805, } - if err := Reduce(ctx, action); err != nil { + if err = Reduce(ctx, action); err != nil { t.Fatal(errors.Wrap(err, "Failed to process action")) } @@ -70,7 +70,7 @@ func TestReduceAddNote_SortByAddedOn(t *testing.T) { Data: b, Timestamp: 1515199944, } - if err := Reduce(ctx, action); err != nil { + if err = Reduce(ctx, action); err != nil { t.Fatal(errors.Wrap(err, "Failed to process action")) } @@ -115,7 +115,7 @@ func TestReduceRemoveNote(t *testing.T) { Data: b, Timestamp: 1517629805, } - if err := Reduce(ctx, action); err != nil { + if err = Reduce(ctx, action); err != nil { t.Fatal(errors.Wrap(err, "Failed to process action")) } @@ -197,7 +197,7 @@ func TestReduceAddBook(t *testing.T) { Data: b, Timestamp: 1517629805, } - if err := Reduce(ctx, action); err != nil { + if err = Reduce(ctx, action); err != nil { t.Fatal(errors.Wrap(err, "Failed to process action")) } @@ -229,7 +229,7 @@ func TestReduceRemoveBook(t *testing.T) { Data: b, Timestamp: 1517629805, } - if err := Reduce(ctx, action); err != nil { + if err = Reduce(ctx, action); err != nil { t.Fatal(errors.Wrap(err, "Failed to process action")) } diff --git a/main_test.go b/main_test.go index bbc31121..e93638fb 100644 --- a/main_test.go +++ b/main_test.go @@ -23,7 +23,7 @@ var binaryName = "test-dnote" func TestMain(m *testing.M) { if err := exec.Command("go", "build", "-o", binaryName).Run(); err != nil { - log.Printf(errors.Wrap(err, "Failed to build a binary").Error()) + log.Print(errors.Wrap(err, "Failed to build a binary").Error()) os.Exit(1) } @@ -66,7 +66,7 @@ func TestInit(t *testing.T) { runDnoteCmd(ctx) // Test - if !utils.FileExists(fmt.Sprintf("%s", ctx.DnoteDir)) { + if !utils.FileExists(ctx.DnoteDir) { t.Errorf("dnote directory was not initialized") } if !utils.FileExists(fmt.Sprintf("%s/%s", ctx.DnoteDir, core.DnoteFilename)) { diff --git a/migrate/migrate.go b/migrate/migrate.go index 93c19970..98bbdb06 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -186,11 +186,7 @@ func readSchema(ctx infra.DnoteCtx) (schema, error) { } err = yaml.Unmarshal(b, &ret) - if err != nil { - return ret, err - } - - return ret, nil + return ret, err } func writeSchema(ctx infra.DnoteCtx, s schema) error { diff --git a/utils/utils.go b/utils/utils.go index ad2fa62a..f8adeea6 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -82,7 +82,7 @@ func CopyFile(src, dest string) error { return errors.Wrap(err, "Failed to copy the file content") } - if err := out.Sync(); err != nil { + if err = out.Sync(); err != nil { return errors.Wrap(err, "Failed to flush the output file to disk") } @@ -91,12 +91,12 @@ func CopyFile(src, dest string) error { return errors.Wrap(err, "Failed to get file info for the input file") } - if err := os.Chmod(dest, fi.Mode()); err != nil { + if err = os.Chmod(dest, fi.Mode()); err != nil { return errors.Wrap(err, "Failed to copy permission to the output file") } // Close the output file - if err := out.Close(); err != nil { + if err = out.Close(); err != nil { return errors.Wrap(err, "Failed to close the output file") }