diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 6751b11..4d6c235 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -73,4 +73,20 @@ jobs: run: | rm -Rf ~/.nuv/olaris bats/bin/bats . - \ No newline at end of file + + linter: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Checkout recursive + uses: actions/checkout@v3 + with: + submodules: recursive + - uses: actions/setup-go@v4 + with: + go-version: '>=1.20.2' + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: latest + args: --timeout=3m \ No newline at end of file diff --git a/auth/login_test.go b/auth/login_test.go index c016eab..76abd95 100644 --- a/auth/login_test.go +++ b/auth/login_test.go @@ -49,7 +49,7 @@ func setupMockServer(t *testing.T, expectedLogin, expectedPass, expectedRes stri t.Errorf("expected password %s, got %s", expectedPass, password) } - w.Write([]byte(expectedRes)) + _, _ = w.Write([]byte(expectedRes)) })) return server diff --git a/common.go b/common.go index 2d67241..d37febf 100644 --- a/common.go +++ b/common.go @@ -99,7 +99,9 @@ func readNuvRootFile(dir string) (NuvRootJSON, error) { if err != nil { return NuvRootJSON{}, err } - json.Unmarshal(json_buf, &data) + if err := json.Unmarshal(json_buf, &data); err != nil { + warn("nuvroot.json parsed with an error", err) + } return data, nil } @@ -113,7 +115,12 @@ func readNuvConfigFile(dir string) (map[string]interface{}, error) { if err != nil { return nil, err } - json.Unmarshal(json_buf, &data) + if err := json.Unmarshal(json_buf, &data); err != nil { + if data == nil { + return nil, err + } + warn("config.json parsed with an error", err) + } return data, nil } diff --git a/config_test.go b/config_test.go index 123f0e5..d1f11fd 100644 --- a/config_test.go +++ b/config_test.go @@ -630,7 +630,7 @@ func createTestNuvRootFile(t *testing.T, dir string, onlyVersion bool) { } nuvRootStr = fmt.Sprintf("{ \"version\": \"0.3.0\", \"config\": %s }", configJson) } - os.WriteFile(path, []byte(nuvRootStr), 0644) + _ = os.WriteFile(path, []byte(nuvRootStr), 0644) } func createTestConfigJson(t *testing.T, dir string, envVars map[string]interface{}) { @@ -639,7 +639,7 @@ func createTestConfigJson(t *testing.T, dir string, envVars map[string]interface if err != nil { t.Errorf("error: %v", err) } - os.WriteFile(filepath.Join(dir, "config.json"), []byte(configJson), 0644) + _ = os.WriteFile(filepath.Join(dir, "config.json"), []byte(configJson), 0644) } func Test_mergeMaps(t *testing.T) { diff --git a/main_test.go b/main_test.go index 0240b76..613a12f 100644 --- a/main_test.go +++ b/main_test.go @@ -34,9 +34,9 @@ func pr(args ...any) { } // as creates a string array -func as(s ...string) []string { - return s -} +// func as(s ...string) []string { +// return s +// } var homeDir = "" var workDir = "" diff --git a/nuv.go b/nuv.go index 1236dba..3130327 100644 --- a/nuv.go +++ b/nuv.go @@ -19,7 +19,6 @@ package main import ( "bufio" "fmt" - "io/ioutil" "os" "regexp" "sort" @@ -115,7 +114,7 @@ func setupTmp() { // load saved args in files names _*_ in current directory func loadSavedArgs() []string { res := []string{} - files, err := ioutil.ReadDir(".") + files, err := os.ReadDir(".") if err != nil { return res } @@ -174,7 +173,9 @@ func Nuv(base string, args []string) error { } // if valid, check if it's a folder and move to it if isDir(taskName) && exists(taskName, NUVFILE) { - os.Chdir(taskName) + if err := os.Chdir(taskName); err != nil { + return err + } //remove it from the args rest = rest[1:] } else { diff --git a/nuv_test.go b/nuv_test.go index e041501..3a4f5c5 100644 --- a/nuv_test.go +++ b/nuv_test.go @@ -27,9 +27,9 @@ import ( "golang.org/x/exp/slices" ) -func ExampleNuvArg() { +func Example_nuvArg() { // test - os.Chdir(workDir) + _ = os.Chdir(workDir) olaris, _ := filepath.Abs(joinpath("tests", "olaris")) err := Nuv(olaris, split("testcmd")) pr(2, err) @@ -52,7 +52,7 @@ func ExampleNuvArg() { func ExampleNuv() { // test - os.Chdir(workDir) + _ = os.Chdir(workDir) olaris, _ := filepath.Abs(joinpath("tests", "olaris")) err := Nuv(olaris, split("")) pr(1, err) @@ -80,7 +80,7 @@ func ExampleNuv() { } func ExampleParseArgs() { - os.Chdir(workDir) + _ = os.Chdir(workDir) usage := readfile("tests/olaris/sub/opts/nuvopts.txt") args := parseArgs(usage, split("ciao mike miri max")) pr(1, args) @@ -116,7 +116,7 @@ func Test_validateTaskName(t *testing.T) { tmpDir := createTmpNuvfile(t, testNuvfile) defer os.RemoveAll(tmpDir) - os.Chdir(tmpDir) + _ = os.Chdir(tmpDir) for _, tt := range validateTaskTests { task, err := validateTaskName(tt.argTask) if err != nil && !strings.Contains(err.Error(), tt.expected) { @@ -129,8 +129,8 @@ func Test_validateTaskName(t *testing.T) { } } -func ExampleTmp() { - os.Chdir(workDir) +func Example_setupTmp() { + _ = os.Chdir(workDir) nuvdir, _ := homedir.Expand("~/.nuv") os.RemoveAll(nuvdir) setupTmp() @@ -140,10 +140,10 @@ func ExampleTmp() { // /home/.nuv/tmp } -func ExampleLoadArgs() { - os.Chdir(workDir) +func Example_loadArgs() { + _ = os.Chdir(workDir) fmt.Println(1, loadSavedArgs()) - os.Chdir(joinpath("tests", "testdata")) + _ = os.Chdir(joinpath("tests", "testdata")) fmt.Println(2, loadSavedArgs()) // Output: // 1 [] diff --git a/prepare.go b/prepare.go index 557dca6..f646629 100644 --- a/prepare.go +++ b/prepare.go @@ -34,7 +34,9 @@ func downloadTasksFromGitHub(force bool, silent bool) (string, error) { if err != nil { return "", err } - os.MkdirAll(nuvDir, 0755) + if err := os.MkdirAll(nuvDir, 0755); err != nil { + return "", err + } localDir, err := homedir.Expand("~/.nuv/olaris") if err != nil { return "", err @@ -121,8 +123,8 @@ func pullTasks(force, silent bool) error { // check if the version is up to date, if not warn the user but continue if nuvVersion.LessThan(nuvRootVersion) { - warn("Your nuv version", nuvVersion, "is older than the required version in nuvroot.json.") - warn("Please update nuv to the latest version.") + fmt.Printf("Your nuv version %v is older than the required version in nuvroot.json.\n", nuvVersion) + fmt.Println("Please update nuv to the latest version.") } return nil diff --git a/prepare_test.go b/prepare_test.go index 44efe9a..9e1f746 100644 --- a/prepare_test.go +++ b/prepare_test.go @@ -23,7 +23,7 @@ import ( ) func Example_locate() { - os.Chdir(workDir) + _ = os.Chdir(workDir) dir, err := locateNuvRoot("tests") pr(1, err, npath(dir)) dir, err = locateNuvRoot(joinpath("tests", "olaris")) @@ -37,21 +37,20 @@ func Example_locate() { } func Example_locate_git() { - os.Chdir(workDir) + _ = os.Chdir(workDir) NuvBranch = "test" nuvdir, _ := homedir.Expand("~/.nuv") - os.RemoveAll(nuvdir) - os.Setenv("NUV_BIN", "") - //_, err := + _ = os.RemoveAll(nuvdir) + _ = os.Setenv("NUV_BIN", "") _, err := locateNuvRoot(".") pr(1, err) - os.Setenv("NUV_BIN", workDir) + _ = os.Setenv("NUV_BIN", workDir) dir, err := locateNuvRoot("tests") pr(2, err, npath(dir)) - downloadTasksFromGitHub(true, true) + _, _ = downloadTasksFromGitHub(true, true) dir, err = locateNuvRoot(".") pr(3, err, nhpath(dir)) - downloadTasksFromGitHub(true, true) + _, _ = downloadTasksFromGitHub(true, true) dir, err = locateNuvRoot(".") pr(4, err, nhpath(dir)) os.RemoveAll(nuvdir) diff --git a/serve.go b/serve.go index de65498..c464677 100644 --- a/serve.go +++ b/serve.go @@ -111,7 +111,9 @@ func nuvTaskServer(w http.ResponseWriter, r *http.Request) { // write output to response outjson, _ := json.Marshal(nuvOut) - w.Write(outjson) + if _, err := w.Write(outjson); err != nil { + debug("Failed to write response", err) + } } func execCommandTasks(tasks []string) NuvOutput { diff --git a/serve_test.go b/serve_test.go index f9765ce..ad8825c 100644 --- a/serve_test.go +++ b/serve_test.go @@ -28,7 +28,7 @@ import ( ) func TestIndex(t *testing.T) { - os.Chdir(workDir) + _ = os.Chdir(workDir) olaris, _ := filepath.Abs(joinpath("tests", "olaris")) handler := webFileServerHandler(joinpath(olaris, WebDir)) ts := httptest.NewServer(handler) @@ -65,7 +65,7 @@ func TestIndex(t *testing.T) { } func TestGetTask(t *testing.T) { - os.Chdir(joinpath(workDir, "tests")) + _ = os.Chdir(joinpath(workDir, "tests")) t.Run("/api/nuv?test invokes test task", func(t *testing.T) { request := newTaskRequest("test") diff --git a/updates_check.go b/updates_check.go index 16319d9..a9c2bf1 100644 --- a/updates_check.go +++ b/updates_check.go @@ -92,28 +92,38 @@ func touchLatestCheckFile(latest_check_path string) { } func checkRemoteOlarisNewer(olaris_path string) bool { - isRemoteNewer := false - repo, err := git.PlainOpen(olaris_path) if err != nil { warn("failed to check olaris folder", err) - return isRemoteNewer + return false } - localRef, _ := repo.Head() + localRef, err := repo.Head() + if err != nil { + warn("failed to check olaris folder", err) + return false + } - remote, _ := repo.Remote("origin") - remote.Fetch(&git.FetchOptions{}) - remoteRefs, _ := remote.List(&git.ListOptions{}) + remote, err := repo.Remote("origin") + if err != nil { + warn("failed to check remote olaris", err) + return false + } + _ = remote.Fetch(&git.FetchOptions{}) + remoteRefs, err := remote.List(&git.ListOptions{}) + if err != nil { + warn("failed to check remote olaris", err) + return false + } // check ref is in refs for _, remoteRef := range remoteRefs { if localRef.Name().String() == remoteRef.Name().String() { // is hash different? if localRef.Hash().String() != remoteRef.Hash().String() { - isRemoteNewer = true + return true } } } - return isRemoteNewer + return false } diff --git a/updates_check_test.go b/updates_check_test.go index 5275ba8..748b69c 100644 --- a/updates_check_test.go +++ b/updates_check_test.go @@ -43,14 +43,22 @@ func resetOneCommit(repo *git.Repository) { Order: git.LogOrderCommitterTime, }) - commIter.Next() - secondLastCommit, _ := commIter.Next() + if _, err := commIter.Next(); err != nil { + pr("failed to get first commit", err) + } + secondLastCommit, err := commIter.Next() + if err != nil { + pr("failed to get second last commit", err) + } w, _ := repo.Worktree() - w.Reset(&git.ResetOptions{ + if err := w.Reset(&git.ResetOptions{ Mode: git.HardReset, Commit: secondLastCommit.Hash, - }) + }); err != nil { + pr("failed to reset repo", err) + } + } func Example_checkUpdated_uptodate() { @@ -63,7 +71,7 @@ func Example_checkUpdated_uptodate() { olarisTmpPath := joinpath(tmpDir, "olaris") - _, err = git.PlainClone(olarisTmpPath, false, &git.CloneOptions{ + _, _ = git.PlainClone(olarisTmpPath, false, &git.CloneOptions{ URL: getNuvRepo(), Progress: os.Stderr}, ) @@ -97,7 +105,7 @@ func Example_checkUpdated_outdated() { olarisTmpPath := joinpath(tmpDir, "olaris") - repo, err := git.PlainClone(olarisTmpPath, false, &git.CloneOptions{ + repo, _ := git.PlainClone(olarisTmpPath, false, &git.CloneOptions{ URL: getNuvRepo(), Progress: os.Stderr}, )