diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index e54765bb0..a9b465c62 100644 --- a/.github/workflows/analysis.yml +++ b/.github/workflows/analysis.yml @@ -12,7 +12,6 @@ jobs: go-version: 1.17.x - name: Static Code Analysis uses: dominikh/staticcheck-action@v1 - Go-Sec: runs-on: ubuntu-latest steps: diff --git a/access_test.go b/access_test.go index 8b40a50d9..43a862a35 100644 --- a/access_test.go +++ b/access_test.go @@ -98,9 +98,8 @@ func TestRefreshableAccessTokens(t *testing.T) { err := coreenvsetup.GenerateNewLongTermRefreshableAccessToken(server) assert.NoError(t, err) assert.NotEmpty(t, server.RefreshToken) - configCmd := commands.NewConfigCommand().SetDetails(server).SetInteractive(false).SetServerId(tests.ServerId) - err = configCmd.Run() - assert.NoError(t, err) + configCmd := commands.NewConfigCommand(commands.AddOrEdit, tests.ServerId).SetDetails(server).SetInteractive(false) + assert.NoError(t, configCmd.Run()) defer deleteServerConfig(t) // Upload a file and assert the refreshable tokens were generated. diff --git a/artifactory/cli.go b/artifactory/cli.go index 1efcb21c9..d64d2c09b 100644 --- a/artifactory/cli.go +++ b/artifactory/cli.go @@ -1493,31 +1493,36 @@ func prepareSearchCommand(c *cli.Context) (*spec.SpecFiles, error) { return searchSpec, err } -func searchCmd(c *cli.Context) error { +func searchCmd(c *cli.Context) (err error) { searchSpec, err := prepareSearchCommand(c) if err != nil { - return err + return } artDetails, err := cliutils.CreateArtifactoryDetailsByFlags(c) if err != nil { - return err + return } retries, err := getRetries(c) if err != nil { - return err + return } retryWaitTime, err := getRetryWaitTime(c) if err != nil { - return err + return } searchCmd := generic.NewSearchCommand() searchCmd.SetServerDetails(artDetails).SetSpec(searchSpec).SetRetries(retries).SetRetryWaitMilliSecs(retryWaitTime) err = commands.Exec(searchCmd) if err != nil { - return err + return } reader := searchCmd.Result().Reader() - defer reader.Close() + defer func() { + e := reader.Close() + if err == nil { + err = e + } + }() length, err := reader.Length() if err != nil { return err diff --git a/artifactory_test.go b/artifactory_test.go index f865a75aa..9cb2da3cd 100644 --- a/artifactory_test.go +++ b/artifactory_test.go @@ -694,9 +694,9 @@ func TestArtifactoryCreateUsers(t *testing.T) { func verifyUsersExistInArtifactory(csvFilePath string, t *testing.T) { // Parse input CSV - content, err := os.Open(csvFilePath) + output, err := os.Open(csvFilePath) assert.NoError(t, err) - csvReader := csv.NewReader(content) + csvReader := csv.NewReader(output) // Ignore the header _, err = csvReader.Read() assert.NoError(t, err) @@ -4318,18 +4318,18 @@ func verifySummary(t *testing.T, buffer *bytes.Buffer, logger log.Log, cmdError assert.NoError(t, cmdError) } - content := buffer.Bytes() + output := buffer.Bytes() buffer.Reset() - logger.Output(string(content)) + logger.Output(string(output)) - status, err := jsonparser.GetString(content, "status") + status, err := jsonparser.GetString(output, "status") assert.NoError(t, err) assert.Equal(t, expected.status, status, "Summary validation failed") - resultSuccess, err := jsonparser.GetInt(content, "totals", "success") + resultSuccess, err := jsonparser.GetInt(output, "totals", "success") assert.NoError(t, err) - resultFailure, err := jsonparser.GetInt(content, "totals", "failure") + resultFailure, err := jsonparser.GetInt(output, "totals", "failure") assert.NoError(t, err) assert.Equal(t, expected.success, resultSuccess, "Summary validation failed") @@ -4466,7 +4466,7 @@ func execListBuildNamesRest() ([]string, error) { } func execCreateRepoRest(repoConfig, repoName string) { - content, err := ioutil.ReadFile(repoConfig) + output, err := ioutil.ReadFile(repoConfig) if err != nil { log.Error(err) os.Exit(1) @@ -4477,7 +4477,7 @@ func execCreateRepoRest(repoConfig, repoName string) { log.Error(err) os.Exit(1) } - resp, body, err := client.SendPut(serverDetails.ArtifactoryUrl+"api/repositories/"+repoName, content, artHttpDetails, "") + resp, body, err := client.SendPut(serverDetails.ArtifactoryUrl+"api/repositories/"+repoName, output, artHttpDetails, "") if err != nil { log.Error(err) os.Exit(1) @@ -4930,11 +4930,11 @@ func TestAccessTokenCreate(t *testing.T) { func checkAccessToken(t *testing.T, buffer *bytes.Buffer) { // Write the command output to the origin - content := buffer.Bytes() + output := buffer.Bytes() buffer.Reset() // Extract the token from the output - token, err := jsonparser.GetString(content, "access_token") + token, err := jsonparser.GetString(output, "access_token") assert.NoError(t, err) // Try ping with the new token diff --git a/config/cli.go b/config/cli.go index 6ab75a3c2..63673e1c0 100644 --- a/config/cli.go +++ b/config/cli.go @@ -148,9 +148,9 @@ func addOrEdit(c *cli.Context, operation configOperation) error { if err != nil { return err } - configCmd := commands.NewConfigCommand().SetDetails(configCommandConfiguration.ServerDetails).SetInteractive(configCommandConfiguration.Interactive). - SetServerId(serverId).SetEncPassword(configCommandConfiguration.EncPassword).SetUseBasicAuthOnly(configCommandConfiguration.BasicAuthOnly) - return configCmd.Config() + configCmd := commands.NewConfigCommand(commands.AddOrEdit, serverId).SetDetails(configCommandConfiguration.ServerDetails).SetInteractive(configCommandConfiguration.Interactive). + SetEncPassword(configCommandConfiguration.EncPassword).SetUseBasicAuthOnly(configCommandConfiguration.BasicAuthOnly) + return configCmd.Run() } func showCmd(c *cli.Context) error { @@ -172,7 +172,7 @@ func deleteCmd(c *cli.Context) error { // Clear all configurations if c.NArg() == 0 { - return commands.ClearConfig(!quiet) + return commands.NewConfigCommand(commands.Clear, "").SetInteractive(!quiet).Run() } // Delete single configuration @@ -180,7 +180,7 @@ func deleteCmd(c *cli.Context) error { if !quiet && !coreutils.AskYesNo("Are you sure you want to delete \""+serverId+"\" configuration?", false) { return nil } - return commands.DeleteConfig(serverId) + return commands.NewConfigCommand(commands.Delete, serverId).Run() } func importCmd(c *cli.Context) error { @@ -206,7 +206,8 @@ func useCmd(c *cli.Context) error { if c.NArg() != 1 { return cliutils.WrongNumberOfArgumentsHandler(c) } - return commands.Use(c.Args()[0]) + serverId := c.Args()[0] + return commands.NewConfigCommand(commands.Use, serverId).Run() } func CreateConfigCommandConfiguration(c *cli.Context) (configCommandConfiguration *commands.ConfigCommandConfiguration, err error) { @@ -240,7 +241,7 @@ func validateServerExistence(serverId string, operation configOperation) error { } func validateConfigFlags(configCommandConfiguration *commands.ConfigCommandConfiguration) error { - // Validate the option is not used along with an access token + // Validate the option is not used along with access token if configCommandConfiguration.BasicAuthOnly && configCommandConfiguration.ServerDetails.AccessToken != "" { return errorutils.CheckErrorf("the --%s option is only supported when username and password/API key are provided", cliutils.BasicAuthOnly) } diff --git a/general/cisetup/cisetup.go b/general/cisetup/cisetup.go index a5c658fce..e0471b03e 100644 --- a/general/cisetup/cisetup.go +++ b/general/cisetup/cisetup.go @@ -319,8 +319,8 @@ func getPipelinesToken() (string, error) { func runConfigCmd() (err error) { for { - configCmd := coreCommonCommands.NewConfigCommand().SetInteractive(true).SetServerId(cisetup.ConfigServerId).SetEncPassword(true) - err = configCmd.Config() + configCmd := coreCommonCommands.NewConfigCommand(coreCommonCommands.AddOrEdit, cisetup.ConfigServerId).SetInteractive(true).SetEncPassword(true) + err = configCmd.Run() if err != nil { log.Error(err) continue diff --git a/go.mod b/go.mod index 8f3ee8ae9..1699237bf 100644 --- a/go.mod +++ b/go.mod @@ -95,4 +95,4 @@ replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1. replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.3.0 -replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.15.3-0.20220607100216-b587c75985e2 +replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.15.3-0.20220612140652-15952821e71d diff --git a/go.sum b/go.sum index c3884e9df..77d00a029 100644 --- a/go.sum +++ b/go.sum @@ -296,8 +296,8 @@ github.com/jfrog/build-info-go v1.3.0 h1:0NutMPwG4qTCXyPhTCo7eGeot9cTT5A7d9LLCdD github.com/jfrog/build-info-go v1.3.0/go.mod h1:S2x0YOFBqBYp22goGRXGfy3ut7XaespgroVJpD6fPwk= github.com/jfrog/gofrog v1.1.2 h1:txts7zSFEGan3a8G+AJCrcq4a/z8PrCmZ7m6c7qaALg= github.com/jfrog/gofrog v1.1.2/go.mod h1:9YN5v4LlsCfLIXpwQnzSf1wVtgjdHM20FzuIu58RMI4= -github.com/jfrog/jfrog-cli-core/v2 v2.15.3-0.20220607100216-b587c75985e2 h1:uHwYRKO2rbgZXOmeSVdQiXbgHI2b+pKyIHLW8jl9TM8= -github.com/jfrog/jfrog-cli-core/v2 v2.15.3-0.20220607100216-b587c75985e2/go.mod h1:V0I57x3VCbzbDZMYAeecH8S+mwNSMQOmnXmxKMqUjbg= +github.com/jfrog/jfrog-cli-core/v2 v2.15.3-0.20220612140652-15952821e71d h1:276bGXr5QxPxuGbMnA/rrzmz191+rjgK8+UIoZG81No= +github.com/jfrog/jfrog-cli-core/v2 v2.15.3-0.20220612140652-15952821e71d/go.mod h1:V0I57x3VCbzbDZMYAeecH8S+mwNSMQOmnXmxKMqUjbg= github.com/jfrog/jfrog-client-go v1.13.2-0.20220607094927-52dc3f597671 h1:qtBo1ISPQKqLzyVBDJxLN4XxGy7ZvBCjxk5ICBbrq44= github.com/jfrog/jfrog-client-go v1.13.2-0.20220607094927-52dc3f597671/go.mod h1:IRxbnT/kVsTEpqAYNC9sZgKUrL4ekUYiqXFXhoLe0J4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= diff --git a/missioncontrol/cli.go b/missioncontrol/cli.go index 29a40b7d4..11fa50084 100644 --- a/missioncontrol/cli.go +++ b/missioncontrol/cli.go @@ -155,8 +155,8 @@ func offerConfig(c *cli.Context) (*config.ServerDetails, error) { return nil, err } details := createMCDetailsFromFlags(c) - configCmd := coreCommonCommands.NewConfigCommand().SetDefaultDetails(details).SetInteractive(true) - err = configCmd.Config() + configCmd := coreCommonCommands.NewConfigCommand(coreCommonCommands.AddOrEdit, details.ServerId).SetDefaultDetails(details).SetInteractive(true) + err = configCmd.Run() if err != nil { return nil, err } diff --git a/utils/cliutils/utils.go b/utils/cliutils/utils.go index ae23c327b..fa702af75 100644 --- a/utils/cliutils/utils.go +++ b/utils/cliutils/utils.go @@ -278,13 +278,13 @@ func ShouldOfferConfig() (bool, error) { if err != nil || exists { return false, err } - + clearConfigCmd := coreCommonCommands.NewConfigCommand(coreCommonCommands.Clear, "") var ci bool if ci, err = clientutils.GetBoolEnvValue(coreutils.CI, false); err != nil { return false, err } if ci { - _ = coreConfig.SaveServersConf(make([]*coreConfig.ServerDetails, 0)) + _ = clearConfigCmd.Run() return false, nil } @@ -295,7 +295,7 @@ func ShouldOfferConfig() (bool, error) { "Configure now?", coreutils.CI) confirmed := coreutils.AskYesNo(msg, false) if !confirmed { - _ = coreConfig.SaveServersConf(make([]*coreConfig.ServerDetails, 0)) + _ = clearConfigCmd.Run() return false, nil } return true, nil @@ -363,8 +363,8 @@ func offerConfig(c *cli.Context, domain CommandDomain) (*coreConfig.ServerDetail return nil, err } details := createServerDetailsFromFlags(c, domain) - configCmd := coreCommonCommands.NewConfigCommand().SetDefaultDetails(details).SetInteractive(true).SetEncPassword(true) - err = configCmd.Config() + configCmd := coreCommonCommands.NewConfigCommand(coreCommonCommands.AddOrEdit, details.ServerId).SetDefaultDetails(details).SetInteractive(true).SetEncPassword(true) + err = configCmd.Run() if err != nil { return nil, err }