Skip to content

Commit

Permalink
keep old behavior with --force
Browse files Browse the repository at this point in the history
Now --force is used both to override the replacement of and existing machine,
and an existing credentials file. To retain the old behavior, the
existence of the file is only checked for the default configuration, not
if explicitly specified.
  • Loading branch information
mmetc committed Nov 30, 2023
1 parent 6264fc3 commit c0043fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions cmd/crowdsec-cli/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func runMachinesAdd(cmd *cobra.Command, args []string) error {
return err
}

forceAdd, err := flags.GetBool("force")
force, err := flags.GetBool("force")
if err != nil {
return err
}
Expand All @@ -247,12 +247,12 @@ func runMachinesAdd(cmd *cobra.Command, args []string) error {
// use the default only if the file does not exist
_, err := os.Stat(credFile)
switch {
case os.IsNotExist(err):
case os.IsNotExist(err) || force:
dumpFile = csConfig.API.Client.CredentialsFilePath
case err != nil:
return fmt.Errorf("unable to stat '%s': %s", credFile, err)

Check warning on line 253 in cmd/crowdsec-cli/machines.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L252-L253

Added lines #L252 - L253 were not covered by tests
default:
return fmt.Errorf(`credentials file '%s' already exists, please remove it or specify a different file with -f ("-f -" for standard output)`, credFile)
return fmt.Errorf(`credentials file '%s' already exists: please remove it, use "--force" or specify a different file with "-f" ("-f -" for standard output)`, credFile)
}
}

Expand All @@ -273,7 +273,7 @@ func runMachinesAdd(cmd *cobra.Command, args []string) error {
survey.AskOne(qs, &machinePassword)
}
password := strfmt.Password(machinePassword)
_, err = dbClient.CreateMachine(&machineID, &password, "", true, forceAdd, types.PasswordAuthType)
_, err = dbClient.CreateMachine(&machineID, &password, "", true, force, types.PasswordAuthType)
if err != nil {
return fmt.Errorf("unable to create machine: %s", err)
}
Expand Down
11 changes: 6 additions & 5 deletions test/bats/30_machines.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ teardown() {
assert_output '[1,"githubciXXXXXXXXXXXXXXXXXXXXXXXX",true]'
}

@test "don't overwrite local credentials" {
rune -1 cscli machines add -a
local_credentials=$(config_get '.api.client.credentials_path')
assert_stderr --partial "credentials file '$local_credentials' already exists, please remove it or specify a different file with -f"
refute_output
@test "don't overwrite local credentials by default" {
rune -1 cscli machines add local -a -o json
rune -0 jq -r '.msg' <(stderr)
assert_output --partial 'already exists: please remove it, use "--force" or specify a different file with "-f"'
rune -0 cscli machines add local -a --force
assert_stderr --partial "Machine 'local' successfully added to the local API"
}

@test "add a new machine and delete it" {
Expand Down

0 comments on commit c0043fa

Please sign in to comment.