Skip to content

Commit

Permalink
cscli machines add: example with "-f -", split creds to stdout and me…
Browse files Browse the repository at this point in the history
…ssages to stderr
  • Loading branch information
mmetc committed Jan 24, 2024
1 parent 8896b38 commit e7b2d3c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/crowdsec-cli/bouncers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -283,7 +284,7 @@ func (cli *cliBouncers) prune(duration time.Duration, force bool) error {
return fmt.Errorf("unable to prune bouncers: %s", err)
}

Check warning on line 285 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L282-L285

Added lines #L282 - L285 were not covered by tests

fmt.Printf("Successfully deleted %d bouncers\n", deleted)
fmt.Fprintf(os.Stderr, "Successfully deleted %d bouncers\n", deleted)

return nil

Check warning on line 289 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L287-L289

Added lines #L287 - L289 were not covered by tests
}
Expand Down
13 changes: 6 additions & 7 deletions cmd/crowdsec-cli/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,10 @@ func (cli *cliMachines) NewAddCmd() *cobra.Command {
Short: "add a single machine to the database",
DisableAutoGenTag: true,
Long: `Register a new machine in the database. cscli should be on the same machine as LAPI.`,
Example: `
cscli machines add --auto
Example: `cscli machines add --auto
cscli machines add MyTestMachine --auto
cscli machines add MyTestMachine --password MyPassword
`,
cscli machines add -f- --auto > /tmp/mycreds.yaml`,
RunE: func(_ *cobra.Command, args []string) error {
return cli.add(args, machinePassword, dumpFile, apiURL, interactive, autoAdd, force)
},
Expand Down Expand Up @@ -307,7 +306,7 @@ func (cli *cliMachines) add(args []string, machinePassword string, dumpFile stri
return fmt.Errorf("unable to create machine: %s", err)
}

fmt.Printf("Machine '%s' successfully added to the local API.\n", machineID)
fmt.Fprintf(os.Stderr, "Machine '%s' successfully added to the local API.\n", machineID)

if apiURL == "" {
if clientCfg != nil && clientCfg.Credentials != nil && clientCfg.Credentials.URL != "" {
Expand Down Expand Up @@ -335,9 +334,9 @@ func (cli *cliMachines) add(args []string, machinePassword string, dumpFile stri
if err != nil {
return fmt.Errorf("write api credentials in '%s' failed: %s", dumpFile, err)
}
fmt.Printf("API credentials written to '%s'.\n", dumpFile)
fmt.Fprintf(os.Stderr, "API credentials written to '%s'.\n", dumpFile)
} else {
fmt.Println(string(apiConfigDump))
fmt.Print(string(apiConfigDump))
}

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L339-L340

Added lines #L339 - L340 were not covered by tests

return nil
Expand Down Expand Up @@ -436,7 +435,7 @@ func (cli *cliMachines) prune(duration time.Duration, notValidOnly bool, force b
return fmt.Errorf("unable to prune machines: %s", err)
}

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L433-L436

Added lines #L433 - L436 were not covered by tests

fmt.Printf("successfully delete %d machines\n", deleted)
fmt.Fprintf(os.Stderr, "successfully delete %d machines\n", deleted)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L438-L439

Added lines #L438 - L439 were not covered by tests
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions test/bats/30_machines.bats
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ teardown() {
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_output --partial "Machine 'local' successfully added to the local API."
assert_stderr --partial "Machine 'local' successfully added to the local API."
}

@test "add a new machine and delete it" {
rune -0 cscli machines add -a -f /dev/null CiTestMachine -o human
assert_output --partial "Machine 'CiTestMachine' successfully added to the local API"
assert_output --partial "API credentials written to '/dev/null'"
assert_stderr --partial "Machine 'CiTestMachine' successfully added to the local API"
assert_stderr --partial "API credentials written to '/dev/null'"

# we now have two machines
rune -0 cscli machines list -o json
Expand Down

0 comments on commit e7b2d3c

Please sign in to comment.