From 1bc3b0870b8b9ca20c5027750fef1a6599a28bc7 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Wed, 7 Aug 2024 12:45:54 +0200 Subject: [PATCH] command "cscli doc --target /path/to/dir" (#3169) * command "cscli doc --target /path/to/dir" * typos and improved messages * CI: remove obsolete parameters for golangi-lint action * lint --- .github/workflows/go-tests-windows.yml | 4 ---- .github/workflows/go-tests.yml | 4 ---- .golangci.yml | 6 +----- cmd/crowdsec-cli/dashboard.go | 11 +++++----- cmd/crowdsec-cli/doc.go | 20 ++++++++++++++----- .../wineventlog/wineventlog_windows.go | 4 ++-- pkg/csplugin/utils_windows.go | 5 +++-- pkg/cwhub/iteminstall.go | 4 ++-- test/bats/01_cscli.bats | 14 +++++++++---- test/bats/20_hub_collections.bats | 2 +- test/bats/20_hub_parsers.bats | 2 +- test/bats/20_hub_postoverflows.bats | 2 +- test/bats/20_hub_scenarios.bats | 2 +- 13 files changed, 43 insertions(+), 37 deletions(-) diff --git a/.github/workflows/go-tests-windows.yml b/.github/workflows/go-tests-windows.yml index 9d8051821c8..b99291f06b7 100644 --- a/.github/workflows/go-tests-windows.yml +++ b/.github/workflows/go-tests-windows.yml @@ -60,7 +60,3 @@ jobs: version: v1.59 args: --issues-exit-code=1 --timeout 10m only-new-issues: false - # the cache is already managed above, enabling it here - # gives errors when extracting - skip-pkg-cache: true - skip-build-cache: true diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml index ab45bd88717..a7c34adfe50 100644 --- a/.github/workflows/go-tests.yml +++ b/.github/workflows/go-tests.yml @@ -161,7 +161,3 @@ jobs: version: v1.59 args: --issues-exit-code=1 --timeout 10m only-new-issues: false - # the cache is already managed above, enabling it here - # gives errors when extracting - skip-pkg-cache: true - skip-build-cache: true diff --git a/.golangci.yml b/.golangci.yml index 2ac39793731..fb1dab623c1 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -271,7 +271,7 @@ linters: # - dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) - - errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and reports occations, where the check for the returned error can be omitted. + - errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and reports occasions, where the check for the returned error can be omitted. - exhaustive # check exhaustiveness of enum switch statements - gci # Gci control golang package import order and make it always deterministic. - godot # Check if comments end in a period @@ -387,10 +387,6 @@ issues: - perfsprint text: "fmt.Sprintf can be replaced .*" - - linters: - - perfsprint - text: "fmt.Errorf can be replaced with errors.New" - # # Will fix, easy but some neurons required # diff --git a/cmd/crowdsec-cli/dashboard.go b/cmd/crowdsec-cli/dashboard.go index beff06d478a..96a2fa38cb7 100644 --- a/cmd/crowdsec-cli/dashboard.go +++ b/cmd/crowdsec-cli/dashboard.go @@ -3,6 +3,7 @@ package main import ( + "errors" "fmt" "math" "os" @@ -277,7 +278,7 @@ cscli dashboard remove --force return fmt.Errorf("unable to ask to force: %s", err) } if !answer { - return fmt.Errorf("user stated no to continue") + return errors.New("user stated no to continue") } } if metabase.IsContainerExist(metabaseContainerID) { @@ -289,7 +290,7 @@ cscli dashboard remove --force if err == nil { // if group exist, remove it groupDelCmd, err := exec.LookPath("groupdel") if err != nil { - return fmt.Errorf("unable to find 'groupdel' command, can't continue") + return errors.New("unable to find 'groupdel' command, can't continue") } groupDel := &exec.Cmd{Path: groupDelCmd, Args: []string{groupDelCmd, crowdsecGroup}} @@ -366,7 +367,7 @@ func checkSystemMemory(forceYes *bool) error { } if !answer { - return fmt.Errorf("user stated no to continue") + return errors.New("user stated no to continue") } return nil @@ -399,7 +400,7 @@ func disclaimer(forceYes *bool) error { } if !answer { - return fmt.Errorf("user stated no to responsibilities") + return errors.New("user stated no to responsibilities") } return nil @@ -435,7 +436,7 @@ func checkGroups(forceYes *bool) (*user.Group, error) { groupAddCmd, err := exec.LookPath("groupadd") if err != nil { - return dockerGroup, fmt.Errorf("unable to find 'groupadd' command, can't continue") + return dockerGroup, errors.New("unable to find 'groupadd' command, can't continue") } groupAdd := &exec.Cmd{Path: groupAddCmd, Args: []string{groupAddCmd, crowdsecGroup}} diff --git a/cmd/crowdsec-cli/doc.go b/cmd/crowdsec-cli/doc.go index db1e642115e..f68d535db03 100644 --- a/cmd/crowdsec-cli/doc.go +++ b/cmd/crowdsec-cli/doc.go @@ -16,20 +16,30 @@ func NewCLIDoc() *cliDoc { } func (cli cliDoc) NewCommand(rootCmd *cobra.Command) *cobra.Command { + var target string + + const defaultTarget = "./doc" + cmd := &cobra.Command{ Use: "doc", - Short: "Generate the documentation in `./doc/`. Directory must exist.", - Args: cobra.ExactArgs(0), + Short: "Generate the documentation related to cscli commands. Target directory must exist.", + Args: cobra.NoArgs, Hidden: true, DisableAutoGenTag: true, - RunE: func(_ *cobra.Command, _ []string) error { - if err := doc.GenMarkdownTreeCustom(rootCmd, "./doc/", cli.filePrepender, cli.linkHandler); err != nil { - return fmt.Errorf("failed to generate cobra doc: %w", err) + RunE: func(_ *cobra.Command, args []string) error { + if err := doc.GenMarkdownTreeCustom(rootCmd, target, cli.filePrepender, cli.linkHandler); err != nil { + return fmt.Errorf("failed to generate cscli documentation: %w", err) } + + fmt.Println("Documentation generated in", target) + return nil }, } + flags := cmd.Flags() + flags.StringVar(&target, "target", defaultTarget, "The target directory where the documentation will be generated") + return cmd } diff --git a/pkg/acquisition/modules/wineventlog/wineventlog_windows.go b/pkg/acquisition/modules/wineventlog/wineventlog_windows.go index 8adbf1e53c5..c6b10b7c38c 100644 --- a/pkg/acquisition/modules/wineventlog/wineventlog_windows.go +++ b/pkg/acquisition/modules/wineventlog/wineventlog_windows.go @@ -246,11 +246,11 @@ func (w *WinEventLogSource) UnmarshalConfig(yamlConfig []byte) error { } if w.config.EventChannel != "" && w.config.XPathQuery != "" { - return fmt.Errorf("event_channel and xpath_query are mutually exclusive") + return errors.New("event_channel and xpath_query are mutually exclusive") } if w.config.EventChannel == "" && w.config.XPathQuery == "" { - return fmt.Errorf("event_channel or xpath_query must be set") + return errors.New("event_channel or xpath_query must be set") } w.config.Mode = configuration.TAIL_MODE diff --git a/pkg/csplugin/utils_windows.go b/pkg/csplugin/utils_windows.go index 2870a2addb5..8d4956ceeeb 100644 --- a/pkg/csplugin/utils_windows.go +++ b/pkg/csplugin/utils_windows.go @@ -3,6 +3,7 @@ package csplugin import ( + "errors" "fmt" "os" "os/exec" @@ -77,14 +78,14 @@ func CheckPerms(path string) error { return fmt.Errorf("while getting owner security info: %w", err) } if !sd.IsValid() { - return fmt.Errorf("security descriptor is invalid") + return errors.New("security descriptor is invalid") } owner, _, err := sd.Owner() if err != nil { return fmt.Errorf("while getting owner: %w", err) } if !owner.IsValid() { - return fmt.Errorf("owner is invalid") + return errors.New("owner is invalid") } if !owner.Equals(systemSid) && !owner.Equals(currentUserSid) && !owner.Equals(adminSid) { diff --git a/pkg/cwhub/iteminstall.go b/pkg/cwhub/iteminstall.go index 7fe8ab59228..912897d0d7e 100644 --- a/pkg/cwhub/iteminstall.go +++ b/pkg/cwhub/iteminstall.go @@ -9,11 +9,11 @@ import ( func (i *Item) enable() error { if i.State.Installed { if i.State.Tainted { - return fmt.Errorf("%s is tainted, won't enable unless --force", i.Name) + return fmt.Errorf("%s is tainted, won't overwrite unless --force", i.Name) } if i.State.IsLocal() { - return fmt.Errorf("%s is local, won't enable", i.Name) + return fmt.Errorf("%s is local, won't overwrite", i.Name) } // if it's a collection, check sub-items even if the collection file itself is up-to-date diff --git a/test/bats/01_cscli.bats b/test/bats/01_cscli.bats index 792274cc4f4..27cfe53212b 100644 --- a/test/bats/01_cscli.bats +++ b/test/bats/01_cscli.bats @@ -366,16 +366,14 @@ teardown() { } @test "cscli doc" { - # generating documentation requires a directory named "doc" - cd "$BATS_TEST_TMPDIR" rune -1 cscli doc refute_output - assert_stderr --regexp 'failed to generate cobra doc: open doc/.*: no such file or directory' + assert_stderr --regexp 'failed to generate cscli documentation: open doc/.*: no such file or directory' mkdir -p doc rune -0 cscli doc - refute_output + assert_output "Documentation generated in ./doc" refute_stderr assert_file_exists "doc/cscli.md" assert_file_not_exist "doc/cscli_setup.md" @@ -385,6 +383,14 @@ teardown() { export CROWDSEC_FEATURE_CSCLI_SETUP="true" rune -0 cscli doc assert_file_exists "doc/cscli_setup.md" + + # specify a target directory + mkdir -p "$BATS_TEST_TMPDIR/doc2" + rune -0 cscli doc --target "$BATS_TEST_TMPDIR/doc2" + assert_output "Documentation generated in $BATS_TEST_TMPDIR/doc2" + refute_stderr + assert_file_exists "$BATS_TEST_TMPDIR/doc2/cscli_setup.md" + } @test "feature.yaml for subcommands" { diff --git a/test/bats/20_hub_collections.bats b/test/bats/20_hub_collections.bats index 1381fe8e55a..6822339ae40 100644 --- a/test/bats/20_hub_collections.bats +++ b/test/bats/20_hub_collections.bats @@ -177,7 +177,7 @@ teardown() { echo "dirty" >"$CONFIG_DIR/collections/sshd.yaml" rune -1 cscli collections install crowdsecurity/sshd - assert_stderr --partial "error while installing 'crowdsecurity/sshd': while enabling crowdsecurity/sshd: crowdsecurity/sshd is tainted, won't enable unless --force" + assert_stderr --partial "error while installing 'crowdsecurity/sshd': while enabling crowdsecurity/sshd: crowdsecurity/sshd is tainted, won't overwrite unless --force" rune -0 cscli collections install crowdsecurity/sshd --force assert_stderr --partial "Enabled crowdsecurity/sshd" diff --git a/test/bats/20_hub_parsers.bats b/test/bats/20_hub_parsers.bats index 214463f9cfc..791b1a2177f 100644 --- a/test/bats/20_hub_parsers.bats +++ b/test/bats/20_hub_parsers.bats @@ -177,7 +177,7 @@ teardown() { echo "dirty" >"$CONFIG_DIR/parsers/s02-enrich/whitelists.yaml" rune -1 cscli parsers install crowdsecurity/whitelists - assert_stderr --partial "error while installing 'crowdsecurity/whitelists': while enabling crowdsecurity/whitelists: crowdsecurity/whitelists is tainted, won't enable unless --force" + assert_stderr --partial "error while installing 'crowdsecurity/whitelists': while enabling crowdsecurity/whitelists: crowdsecurity/whitelists is tainted, won't overwrite unless --force" rune -0 cscli parsers install crowdsecurity/whitelists --force assert_stderr --partial "Enabled crowdsecurity/whitelists" diff --git a/test/bats/20_hub_postoverflows.bats b/test/bats/20_hub_postoverflows.bats index 5123966a44e..37337b08caa 100644 --- a/test/bats/20_hub_postoverflows.bats +++ b/test/bats/20_hub_postoverflows.bats @@ -177,7 +177,7 @@ teardown() { echo "dirty" >"$CONFIG_DIR/postoverflows/s00-enrich/rdns.yaml" rune -1 cscli postoverflows install crowdsecurity/rdns - assert_stderr --partial "error while installing 'crowdsecurity/rdns': while enabling crowdsecurity/rdns: crowdsecurity/rdns is tainted, won't enable unless --force" + assert_stderr --partial "error while installing 'crowdsecurity/rdns': while enabling crowdsecurity/rdns: crowdsecurity/rdns is tainted, won't overwrite unless --force" rune -0 cscli postoverflows install crowdsecurity/rdns --force assert_stderr --partial "Enabled crowdsecurity/rdns" diff --git a/test/bats/20_hub_scenarios.bats b/test/bats/20_hub_scenarios.bats index 4e4b28e7703..3ab3d944c93 100644 --- a/test/bats/20_hub_scenarios.bats +++ b/test/bats/20_hub_scenarios.bats @@ -178,7 +178,7 @@ teardown() { echo "dirty" >"$CONFIG_DIR/scenarios/ssh-bf.yaml" rune -1 cscli scenarios install crowdsecurity/ssh-bf - assert_stderr --partial "error while installing 'crowdsecurity/ssh-bf': while enabling crowdsecurity/ssh-bf: crowdsecurity/ssh-bf is tainted, won't enable unless --force" + assert_stderr --partial "error while installing 'crowdsecurity/ssh-bf': while enabling crowdsecurity/ssh-bf: crowdsecurity/ssh-bf is tainted, won't overwrite unless --force" rune -0 cscli scenarios install crowdsecurity/ssh-bf --force assert_stderr --partial "Enabled crowdsecurity/ssh-bf"