Skip to content

Commit

Permalink
Make all config commands - atomic (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverdlov93 authored Jun 12, 2022
1 parent 79d303f commit a17cbb3
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 41 deletions.
1 change: 0 additions & 1 deletion .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 12 additions & 7 deletions artifactory/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions artifactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -172,15 +172,15 @@ 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
serverId := c.Args()[0]
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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions general/cisetup/cisetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions missioncontrol/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions utils/cliutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit a17cbb3

Please sign in to comment.