diff --git a/pkg/cmd/clean.go b/pkg/cmd/clean.go index 9836afa5a..07b782a94 100644 --- a/pkg/cmd/clean.go +++ b/pkg/cmd/clean.go @@ -13,6 +13,6 @@ func NewCleanCmd() *cobra.Command { return &cobra.Command{ Use: "clean SUBCOMMAND", Short: "clean objects", - Long: descAddLong, + Long: descCleanLong, } } \ No newline at end of file diff --git a/pkg/cmd/show_context.go b/pkg/cmd/show_context.go index ed4269c9c..2bf3ef0aa 100644 --- a/pkg/cmd/show_context.go +++ b/pkg/cmd/show_context.go @@ -34,7 +34,7 @@ func (s showContextCmd) runFunc() CommandRunnerFunc { ctx.Current = rcontext.DefaultCtx } - fmt.Println(fmt.Sprintf("Current context: %s", ctx.Current)) + fmt.Printf("Current context: %s", ctx.Current) return nil } diff --git a/pkg/credential/credsingle/finder_test.go b/pkg/credential/credsingle/finder_test.go index 5c4e63816..6331bc96d 100644 --- a/pkg/credential/credsingle/finder_test.go +++ b/pkg/credential/credsingle/finder_test.go @@ -1,6 +1,7 @@ package credsingle import ( + "fmt" "os" "reflect" "testing" @@ -11,7 +12,11 @@ import ( func TestFind(t *testing.T) { tmp := os.TempDir() setter := NewSetter(tmp, ctxFinder, sessManager) - setter.Set(githubCred) + err := setter.Set(githubCred) + if err != nil { + fmt.Sprintf("Error in Set") + return + } finder := NewFinder(tmp, ctxFinder, sessManager) type out struct { diff --git a/pkg/credential/credteam/credteam_test.go b/pkg/credential/credteam/credteam_test.go index fc31fc168..48e15856c 100644 --- a/pkg/credential/credteam/credteam_test.go +++ b/pkg/credential/credteam/credteam_test.go @@ -1,6 +1,7 @@ package credteam import ( + "fmt" "github.com/ZupIT/ritchie-cli/pkg/credential" "github.com/ZupIT/ritchie-cli/pkg/rcontext" "github.com/ZupIT/ritchie-cli/pkg/session" @@ -59,6 +60,10 @@ func TestMain(m *testing.M) { func mockServer(status int, body []byte) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { rw.WriteHeader(status) - rw.Write(body) + _, err := rw.Write(body) + if err != nil { + fmt.Sprintf("Error in Write") + return + } })) } diff --git a/pkg/crypto/cryptoutil/crypto_util.go b/pkg/crypto/cryptoutil/crypto_util.go index ac58caecf..8866e6b10 100644 --- a/pkg/crypto/cryptoutil/crypto_util.go +++ b/pkg/crypto/cryptoutil/crypto_util.go @@ -69,8 +69,16 @@ func SumHashMachine(text string) ([]byte, error) { } hash := sha256.New() - hash.Write([]byte(text)) - hash.Write([]byte(id)) + _, err = hash.Write([]byte(text)) + if err != nil { + return nil, err + } + + _, err = hash.Write([]byte(id)) + if err != nil { + return nil, err + } + sum := hash.Sum(nil) return sum, nil diff --git a/pkg/file/fileutil/file_util.go b/pkg/file/fileutil/file_util.go index 41425c764..86d09527c 100644 --- a/pkg/file/fileutil/file_util.go +++ b/pkg/file/fileutil/file_util.go @@ -119,10 +119,13 @@ func CreateDirIfNotExists(dir string, perm os.FileMode) error { } // CreateFileIfNotExist creates file if not exists -func CreateFileIfNotExist(file string, content []byte) { +func CreateFileIfNotExist(file string, content []byte) error { if _, err := os.Stat(file); os.IsNotExist(err) { - WriteFile(file, content) + if err := WriteFile(file, content); err != nil { + return err + } } + return nil } // RemoveDir removes path and any children it contains. @@ -198,7 +201,10 @@ func Unzip(src string, dest string) error { if file.FileInfo().IsDir() { log.Println("Directory Created:", extractedFilePath) - os.MkdirAll(extractedFilePath, file.Mode()) + err := os.MkdirAll(extractedFilePath, file.Mode()) + if err != nil { + return err + } } else { log.Println("File extracted:", file.Name) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index af20cd990..ad60a199b 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -37,10 +37,15 @@ func (c CreateManager) Create(fCmd string) error { } if fileutil.Exists(fmt.Sprintf(TreeCreatePathPattern, c.formPath)) && (fileutil.Exists(fmt.Sprintf("%s/%s", c.formPath, Makefile))) { - generateFormulaFiles(c.formPath, fCmd, false) - + err = generateFormulaFiles(c.formPath, fCmd, false) + if err != nil { + return err + } } else { - generateFormulaFiles(c.formPath, fCmd, true) + err = generateFormulaFiles(c.formPath, fCmd, true) + if err != nil { + return err + } } err = generateTreeJsonFile(c.formPath, fCmd) if err != nil { diff --git a/pkg/formula/default_repo.go b/pkg/formula/default_repo.go index caf8e81c7..b07e794c7 100644 --- a/pkg/formula/default_repo.go +++ b/pkg/formula/default_repo.go @@ -37,7 +37,7 @@ type RepoManager struct { homePath string httpClient *http.Client sessionManager session.Manager - serverURL string + serverURL string } // ByPriority implements sort.Interface for []Repository based on @@ -63,7 +63,7 @@ func NewTeamRepoManager(homePath string, serverURL string, hc *http.Client, sm s repoFile: fmt.Sprintf(repositoryConfFilePattern, homePath), cacheFile: fmt.Sprintf(repositoryCacheFolderPattern, homePath), homePath: homePath, - serverURL : serverURL, + serverURL: serverURL, httpClient: hc, sessionManager: sm, } @@ -81,7 +81,9 @@ func (dm RepoManager) Add(r Repository) error { defer cancel() locked, err := lock.TryLockContext(lockCtx, time.Second) if locked { - defer lock.Unlock() + defer func() { + _ = lock.Unlock() + }() } if err != nil { return err @@ -92,7 +94,10 @@ func (dm RepoManager) Add(r Repository) error { if err != nil { return err } - fileutil.WriteFile(dm.repoFile, wb) + err = fileutil.WriteFile(dm.repoFile, wb) + if err != nil { + return err + } } rb, err := fileutil.ReadFile(dm.repoFile) @@ -247,7 +252,10 @@ func (dm RepoManager) Load() error { } for _, v := range dd { - dm.Add(v) + err = dm.Add(v) + if err != nil { + return err + } } return nil diff --git a/pkg/formula/runner.go b/pkg/formula/runner.go index 0607a67f9..5323a3017 100644 --- a/pkg/formula/runner.go +++ b/pkg/formula/runner.go @@ -180,7 +180,12 @@ func (d DefaultRunner) persistCache(formulaPath, inputVal string, input Input, i items = items[0:qtd] } itemsBytes, _ := json.Marshal(items) - fileutil.WriteFile(cachePath, itemsBytes) + err := fileutil.WriteFile(cachePath, itemsBytes) + if err != nil { + fmt.Sprintf("Error in WriteFile") + return + } + } } diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 8d9e5cec3..c5f274a1c 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "net/http" "os" "strings" @@ -62,12 +61,6 @@ func (s Sender) SendCommand() { } defer resp.Body.Close() - - b, err = ioutil.ReadAll(resp.Body) - if err != nil { - return - } - } func cmd() string { diff --git a/pkg/rcontext/finder_test.go b/pkg/rcontext/finder_test.go index 44b86a248..ea1f3da97 100644 --- a/pkg/rcontext/finder_test.go +++ b/pkg/rcontext/finder_test.go @@ -1,6 +1,7 @@ package rcontext import ( + "fmt" "os" "reflect" "testing" @@ -52,7 +53,11 @@ func TestFind(t *testing.T) { in := tt.in if in != nil { - setter.Set(in.ctx) + _, err := setter.Set(in.ctx) + if err != nil { + fmt.Sprintf("Error in Set") + return + } } out := tt.out diff --git a/pkg/rcontext/remover_test.go b/pkg/rcontext/remover_test.go index a2ce156a1..42c8aedd0 100644 --- a/pkg/rcontext/remover_test.go +++ b/pkg/rcontext/remover_test.go @@ -1,6 +1,7 @@ package rcontext import ( + "fmt" "os" "reflect" "testing" @@ -12,8 +13,16 @@ func TestRemove(t *testing.T) { setter := NewSetter(tmp, finder) remover := NewRemover(tmp, finder) - setter.Set(dev) - setter.Set(qa) + _, err := setter.Set(dev) + if err != nil { + fmt.Sprintf("Error in Set") + return + } + _, err = setter.Set(qa) + if err != nil { + fmt.Sprintf("Error in Set") + return + } type out struct { want ContextHolder diff --git a/pkg/security/oauth/login.go b/pkg/security/oauth/login.go index 9c41f0479..44657185f 100644 --- a/pkg/security/oauth/login.go +++ b/pkg/security/oauth/login.go @@ -4,10 +4,12 @@ import ( "context" "errors" "fmt" - "github.com/ZupIT/ritchie-cli/pkg/security" + "net/http" + "github.com/coreos/go-oidc" "golang.org/x/oauth2" - "net/http" + + "github.com/ZupIT/ritchie-cli/pkg/security" ) const ( @@ -65,13 +67,13 @@ const ( ) type LoginManager struct { - Resp chan security.ChanResponse + Resp chan security.ChanResponse serverURL string } func NewLoginManager(resp chan security.ChanResponse, serverURL string) *LoginManager { return &LoginManager{ - Resp: resp, + Resp: resp, serverURL: serverURL, } } @@ -146,8 +148,16 @@ func (l LoginManager) handlerLogin(provider *oidc.Provider, state string, oauth2 Email string `json:"email"` Username string `json:"preferred_username"` }{} - idToken.Claims(&user) - w.Write([]byte(htmlLogin)) + err = idToken.Claims(&user) + if err != nil { + fmt.Sprintf("Error in Claims") + return + } + _, err = w.Write([]byte(htmlLogin)) + if err != nil { + fmt.Sprintf("Error in Write") + return + } l.Resp <- security.ChanResponse{ Token: token, Username: user.Username, diff --git a/pkg/security/oauth/logout.go b/pkg/security/oauth/logout.go index ecdfaa38b..8470728c0 100644 --- a/pkg/security/oauth/logout.go +++ b/pkg/security/oauth/logout.go @@ -61,15 +61,15 @@ const ( ) type LogoutManager struct { - Resp chan security.ChanResponse - Org string + Resp chan security.ChanResponse + Org string serverURL string } func NewLogoutManager(org string, resp chan security.ChanResponse, serverURL string) *LogoutManager { return &LogoutManager{ - Resp: resp, - Org: org, + Resp: resp, + Org: org, serverURL: serverURL, } } @@ -94,7 +94,11 @@ func (l *LogoutManager) Logout() error { func (l *LogoutManager) handlerLogout() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(htmlLogout)) + _, err := w.Write([]byte(htmlLogout)) + if err != nil { + fmt.Sprintf("Error in Write") + return + } l.Resp <- security.ChanResponse{} } } diff --git a/pkg/security/oauth/oauth.go b/pkg/security/oauth/oauth.go index 6e746063d..a6142093d 100644 --- a/pkg/security/oauth/oauth.go +++ b/pkg/security/oauth/oauth.go @@ -50,7 +50,11 @@ func providerConfig(organization, serverURL string) (ProviderConfig, error) { return provideConfig, fmt.Errorf("Failed parse response to body: %s\n", string(bodyBytes)) } - json.Unmarshal(bodyBytes, &provideConfig) + err = json.Unmarshal(bodyBytes, &provideConfig) + if err != nil { + return provideConfig, err + } + return provideConfig, nil } diff --git a/pkg/security/secteam/provider.go b/pkg/security/secteam/provider.go index 4b8628528..a853b4357 100644 --- a/pkg/security/secteam/provider.go +++ b/pkg/security/secteam/provider.go @@ -1,6 +1,8 @@ package secteam import ( + "fmt" + "github.com/ZupIT/ritchie-cli/pkg/security" "github.com/ZupIT/ritchie-cli/pkg/security/oauth" ) @@ -10,7 +12,13 @@ func loginChannelProvider(p security.AuthProvider, org, serverURL string) (chan switch p { case security.OAuthProvider: oauthCli := oauth.NewLoginManager(cr, serverURL) - go oauthCli.Login(org) + go func() { + err := oauthCli.Login(org) + if err != nil { + fmt.Sprintf("Error in Login") + return + } + }() default: return nil, security.ErrUnknownProvider } @@ -22,7 +30,13 @@ func logoutChannelProvider(p security.AuthProvider, org, serverURL string) (chan switch p { case security.OAuthProvider: oauthCli := oauth.NewLogoutManager(org, cr, serverURL) - go oauthCli.Logout() + go func() { + err := oauthCli.Logout() + if err != nil { + fmt.Sprintf("Error in Logout") + return + } + }() default: return nil, security.ErrUnknownProvider } diff --git a/pkg/workspace/default_workspace.go b/pkg/workspace/default_workspace.go index 664ab08e1..34b405eb9 100644 --- a/pkg/workspace/default_workspace.go +++ b/pkg/workspace/default_workspace.go @@ -4,12 +4,14 @@ import ( "context" "encoding/json" "fmt" - "github.com/ZupIT/ritchie-cli/pkg/formula" - "github.com/gofrs/flock" "path/filepath" "strings" "time" + "github.com/gofrs/flock" + + "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/file/fileutil" ) @@ -47,7 +49,13 @@ func (d DefaultChecker) Check() error { defer cancel() locked, err := lock.TryLockContext(lockCtx, time.Second) if locked { - defer lock.Unlock() + defer func() { + err := lock.Unlock() + if err != nil { + fmt.Sprintf("Error in Unlock") + return + } + }() } if err != nil { @@ -58,7 +66,10 @@ func (d DefaultChecker) Check() error { if err != nil { return err } - fileutil.WriteFile(repoFile, b) + err = fileutil.WriteFile(repoFile, b) + if err != nil { + return err + } return nil }