Skip to content

Commit

Permalink
command "cscli doc --target /path/to/dir" (#3169)
Browse files Browse the repository at this point in the history
* command "cscli doc --target /path/to/dir"

* typos and improved messages

* CI: remove obsolete parameters for golangi-lint action

* lint
  • Loading branch information
mmetc authored Aug 7, 2024
1 parent 6bd4096 commit 1bc3b08
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 37 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/go-tests-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions .github/workflows/go-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 1 addition & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
#
Expand Down
11 changes: 6 additions & 5 deletions cmd/crowdsec-cli/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"errors"
"fmt"
"math"
"os"
Expand Down Expand Up @@ -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) {
Expand All @@ -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}}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}}
Expand Down
20 changes: 15 additions & 5 deletions cmd/crowdsec-cli/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/acquisition/modules/wineventlog/wineventlog_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pkg/csplugin/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package csplugin

import (
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cwhub/iteminstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions test/bats/01_cscli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion test/bats/20_hub_collections.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/bats/20_hub_parsers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/bats/20_hub_postoverflows.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/bats/20_hub_scenarios.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 1bc3b08

Please sign in to comment.