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
This commit is contained in:
mmetc 2024-08-07 12:45:54 +02:00 committed by GitHub
parent 6bd4096a3e
commit 1bc3b0870b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 43 additions and 37 deletions

View file

@ -60,7 +60,3 @@ jobs:
version: v1.59 version: v1.59
args: --issues-exit-code=1 --timeout 10m args: --issues-exit-code=1 --timeout 10m
only-new-issues: false 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

View file

@ -161,7 +161,3 @@ jobs:
version: v1.59 version: v1.59
args: --issues-exit-code=1 --timeout 10m args: --issues-exit-code=1 --timeout 10m
only-new-issues: false 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

View file

@ -271,7 +271,7 @@ linters:
# #
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) - 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 - exhaustive # check exhaustiveness of enum switch statements
- gci # Gci control golang package import order and make it always deterministic. - gci # Gci control golang package import order and make it always deterministic.
- godot # Check if comments end in a period - godot # Check if comments end in a period
@ -387,10 +387,6 @@ issues:
- perfsprint - perfsprint
text: "fmt.Sprintf can be replaced .*" text: "fmt.Sprintf can be replaced .*"
- linters:
- perfsprint
text: "fmt.Errorf can be replaced with errors.New"
# #
# Will fix, easy but some neurons required # Will fix, easy but some neurons required
# #

View file

@ -3,6 +3,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"math" "math"
"os" "os"
@ -277,7 +278,7 @@ cscli dashboard remove --force
return fmt.Errorf("unable to ask to force: %s", err) return fmt.Errorf("unable to ask to force: %s", err)
} }
if !answer { if !answer {
return fmt.Errorf("user stated no to continue") return errors.New("user stated no to continue")
} }
} }
if metabase.IsContainerExist(metabaseContainerID) { if metabase.IsContainerExist(metabaseContainerID) {
@ -289,7 +290,7 @@ cscli dashboard remove --force
if err == nil { // if group exist, remove it if err == nil { // if group exist, remove it
groupDelCmd, err := exec.LookPath("groupdel") groupDelCmd, err := exec.LookPath("groupdel")
if err != nil { 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}} groupDel := &exec.Cmd{Path: groupDelCmd, Args: []string{groupDelCmd, crowdsecGroup}}
@ -366,7 +367,7 @@ func checkSystemMemory(forceYes *bool) error {
} }
if !answer { if !answer {
return fmt.Errorf("user stated no to continue") return errors.New("user stated no to continue")
} }
return nil return nil
@ -399,7 +400,7 @@ func disclaimer(forceYes *bool) error {
} }
if !answer { if !answer {
return fmt.Errorf("user stated no to responsibilities") return errors.New("user stated no to responsibilities")
} }
return nil return nil
@ -435,7 +436,7 @@ func checkGroups(forceYes *bool) (*user.Group, error) {
groupAddCmd, err := exec.LookPath("groupadd") groupAddCmd, err := exec.LookPath("groupadd")
if err != nil { 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}} groupAdd := &exec.Cmd{Path: groupAddCmd, Args: []string{groupAddCmd, crowdsecGroup}}

View file

@ -16,20 +16,30 @@ func NewCLIDoc() *cliDoc {
} }
func (cli cliDoc) NewCommand(rootCmd *cobra.Command) *cobra.Command { func (cli cliDoc) NewCommand(rootCmd *cobra.Command) *cobra.Command {
var target string
const defaultTarget = "./doc"
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "doc", Use: "doc",
Short: "Generate the documentation in `./doc/`. Directory must exist.", Short: "Generate the documentation related to cscli commands. Target directory must exist.",
Args: cobra.ExactArgs(0), Args: cobra.NoArgs,
Hidden: true, Hidden: true,
DisableAutoGenTag: true, DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(_ *cobra.Command, args []string) error {
if err := doc.GenMarkdownTreeCustom(rootCmd, "./doc/", cli.filePrepender, cli.linkHandler); err != nil { if err := doc.GenMarkdownTreeCustom(rootCmd, target, cli.filePrepender, cli.linkHandler); err != nil {
return fmt.Errorf("failed to generate cobra doc: %w", err) return fmt.Errorf("failed to generate cscli documentation: %w", err)
} }
fmt.Println("Documentation generated in", target)
return nil return nil
}, },
} }
flags := cmd.Flags()
flags.StringVar(&target, "target", defaultTarget, "The target directory where the documentation will be generated")
return cmd return cmd
} }

View file

@ -246,11 +246,11 @@ func (w *WinEventLogSource) UnmarshalConfig(yamlConfig []byte) error {
} }
if w.config.EventChannel != "" && w.config.XPathQuery != "" { 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 == "" { 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 w.config.Mode = configuration.TAIL_MODE

View file

@ -3,6 +3,7 @@
package csplugin package csplugin
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
@ -77,14 +78,14 @@ func CheckPerms(path string) error {
return fmt.Errorf("while getting owner security info: %w", err) return fmt.Errorf("while getting owner security info: %w", err)
} }
if !sd.IsValid() { if !sd.IsValid() {
return fmt.Errorf("security descriptor is invalid") return errors.New("security descriptor is invalid")
} }
owner, _, err := sd.Owner() owner, _, err := sd.Owner()
if err != nil { if err != nil {
return fmt.Errorf("while getting owner: %w", err) return fmt.Errorf("while getting owner: %w", err)
} }
if !owner.IsValid() { 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) { if !owner.Equals(systemSid) && !owner.Equals(currentUserSid) && !owner.Equals(adminSid) {

View file

@ -9,11 +9,11 @@ import (
func (i *Item) enable() error { func (i *Item) enable() error {
if i.State.Installed { if i.State.Installed {
if i.State.Tainted { 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() { 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 // if it's a collection, check sub-items even if the collection file itself is up-to-date

View file

@ -366,16 +366,14 @@ teardown() {
} }
@test "cscli doc" { @test "cscli doc" {
# generating documentation requires a directory named "doc"
cd "$BATS_TEST_TMPDIR" cd "$BATS_TEST_TMPDIR"
rune -1 cscli doc rune -1 cscli doc
refute_output 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 mkdir -p doc
rune -0 cscli doc rune -0 cscli doc
refute_output assert_output "Documentation generated in ./doc"
refute_stderr refute_stderr
assert_file_exists "doc/cscli.md" assert_file_exists "doc/cscli.md"
assert_file_not_exist "doc/cscli_setup.md" assert_file_not_exist "doc/cscli_setup.md"
@ -385,6 +383,14 @@ teardown() {
export CROWDSEC_FEATURE_CSCLI_SETUP="true" export CROWDSEC_FEATURE_CSCLI_SETUP="true"
rune -0 cscli doc rune -0 cscli doc
assert_file_exists "doc/cscli_setup.md" 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" { @test "feature.yaml for subcommands" {

View file

@ -177,7 +177,7 @@ teardown() {
echo "dirty" >"$CONFIG_DIR/collections/sshd.yaml" echo "dirty" >"$CONFIG_DIR/collections/sshd.yaml"
rune -1 cscli collections install crowdsecurity/sshd 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 rune -0 cscli collections install crowdsecurity/sshd --force
assert_stderr --partial "Enabled crowdsecurity/sshd" assert_stderr --partial "Enabled crowdsecurity/sshd"

View file

@ -177,7 +177,7 @@ teardown() {
echo "dirty" >"$CONFIG_DIR/parsers/s02-enrich/whitelists.yaml" echo "dirty" >"$CONFIG_DIR/parsers/s02-enrich/whitelists.yaml"
rune -1 cscli parsers install crowdsecurity/whitelists 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 rune -0 cscli parsers install crowdsecurity/whitelists --force
assert_stderr --partial "Enabled crowdsecurity/whitelists" assert_stderr --partial "Enabled crowdsecurity/whitelists"

View file

@ -177,7 +177,7 @@ teardown() {
echo "dirty" >"$CONFIG_DIR/postoverflows/s00-enrich/rdns.yaml" echo "dirty" >"$CONFIG_DIR/postoverflows/s00-enrich/rdns.yaml"
rune -1 cscli postoverflows install crowdsecurity/rdns 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 rune -0 cscli postoverflows install crowdsecurity/rdns --force
assert_stderr --partial "Enabled crowdsecurity/rdns" assert_stderr --partial "Enabled crowdsecurity/rdns"

View file

@ -178,7 +178,7 @@ teardown() {
echo "dirty" >"$CONFIG_DIR/scenarios/ssh-bf.yaml" echo "dirty" >"$CONFIG_DIR/scenarios/ssh-bf.yaml"
rune -1 cscli scenarios install crowdsecurity/ssh-bf 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 rune -0 cscli scenarios install crowdsecurity/ssh-bf --force
assert_stderr --partial "Enabled crowdsecurity/ssh-bf" assert_stderr --partial "Enabled crowdsecurity/ssh-bf"