Skip to content

Commit

Permalink
lint (wsl) (#2692)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Jan 3, 2024
1 parent 2a2b09b commit a504113
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
)

func NewCompletionCmd() *cobra.Command {

var completionCmd = &cobra.Command{
completionCmd := &cobra.Command{
Use: "completion [bash|zsh|powershell|fish]",
Short: "Generate completion script",
Long: `To load completions:
Expand Down Expand Up @@ -82,5 +81,6 @@ func NewCompletionCmd() *cobra.Command {
}
},
}

return completionCmd
}
19 changes: 14 additions & 5 deletions cmd/crowdsec-cli/config_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import (
)

func backupHub(dirPath string) error {
var itemDirectory string
var upstreamParsers []string

hub, err := require.Hub(csConfig, nil, nil)
if err != nil {
return err
Expand All @@ -26,16 +23,20 @@ func backupHub(dirPath string) error {
clog := log.WithFields(log.Fields{
"type": itemType,
})

itemMap := hub.GetItemMap(itemType)
if itemMap == nil {
clog.Infof("No %s to backup.", itemType)
continue
}
itemDirectory = fmt.Sprintf("%s/%s/", dirPath, itemType)

itemDirectory := fmt.Sprintf("%s/%s/", dirPath, itemType)
if err = os.MkdirAll(itemDirectory, os.ModePerm); err != nil {
return fmt.Errorf("error while creating %s : %s", itemDirectory, err)
}
upstreamParsers = []string{}

upstreamParsers := []string{}

for k, v := range itemMap {
clog = clog.WithFields(log.Fields{
"file": v.Name,
Expand All @@ -54,28 +55,36 @@ func backupHub(dirPath string) error {
return fmt.Errorf("error while creating stage dir %s : %s", fstagedir, err)
}
}

clog.Debugf("[%s]: backing up file (tainted:%t local:%t up-to-date:%t)", k, v.State.Tainted, v.State.IsLocal(), v.State.UpToDate)

tfile := fmt.Sprintf("%s%s/%s", itemDirectory, v.Stage, v.FileName)
if err = CopyFile(v.State.LocalPath, tfile); err != nil {
return fmt.Errorf("failed copy %s %s to %s : %s", itemType, v.State.LocalPath, tfile, err)
}

clog.Infof("local/tainted saved %s to %s", v.State.LocalPath, tfile)

continue
}

clog.Debugf("[%s] : from hub, just backup name (up-to-date:%t)", k, v.State.UpToDate)
clog.Infof("saving, version:%s, up-to-date:%t", v.Version, v.State.UpToDate)
upstreamParsers = append(upstreamParsers, v.Name)
}
//write the upstream items
upstreamParsersFname := fmt.Sprintf("%s/upstream-%s.json", itemDirectory, itemType)

upstreamParsersContent, err := json.MarshalIndent(upstreamParsers, "", " ")
if err != nil {
return fmt.Errorf("failed marshaling upstream parsers : %s", err)
}

err = os.WriteFile(upstreamParsersFname, upstreamParsersContent, 0o644)
if err != nil {
return fmt.Errorf("unable to write to %s %s : %s", itemType, upstreamParsersFname, err)
}

clog.Infof("Wrote %d entries for %s to %s", len(upstreamParsers), itemType, upstreamParsersFname)
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/crowdsec-cli/config_feature_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func runConfigFeatureFlags(cmd *cobra.Command, args []string) error {
if feat.State == fflag.RetiredState {
fmt.Printf("\n %s %s", magenta("RETIRED"), feat.DeprecationMsg)
}

fmt.Println()
}

Expand All @@ -58,10 +59,12 @@ func runConfigFeatureFlags(cmd *cobra.Command, args []string) error {
retired = append(retired, feat)
continue
}

if feat.IsEnabled() {
enabled = append(enabled, feat)
continue
}

disabled = append(disabled, feat)
}

Expand Down
16 changes: 14 additions & 2 deletions cmd/crowdsec-cli/config_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,26 @@ func restoreHub(dirPath string) error {
}
/*restore the upstream items*/
upstreamListFN := fmt.Sprintf("%s/upstream-%s.json", itemDirectory, itype)

file, err := os.ReadFile(upstreamListFN)
if err != nil {
return fmt.Errorf("error while opening %s : %s", upstreamListFN, err)
}

var upstreamList []string

err = json.Unmarshal(file, &upstreamList)
if err != nil {
return fmt.Errorf("error unmarshaling %s : %s", upstreamListFN, err)
}

for _, toinstall := range upstreamList {
item := hub.GetItem(itype, toinstall)
if item == nil {
log.Errorf("Item %s/%s not found in hub", itype, toinstall)
continue
}

err := item.Install(false, false)
if err != nil {
log.Errorf("Error while installing %s : %s", toinstall, err)
Expand All @@ -61,23 +66,28 @@ func restoreHub(dirPath string) error {
if err != nil {
return fmt.Errorf("failed enumerating files of %s : %s", itemDirectory, err)
}

for _, file := range files {
//this was the upstream data
if file.Name() == fmt.Sprintf("upstream-%s.json", itype) {
continue
}

if itype == cwhub.PARSERS || itype == cwhub.POSTOVERFLOWS {
//we expect a stage here
if !file.IsDir() {
continue
}

stage := file.Name()
stagedir := fmt.Sprintf("%s/%s/%s/", csConfig.ConfigPaths.ConfigDir, itype, stage)
log.Debugf("Found stage %s in %s, target directory : %s", stage, itype, stagedir)

if err = os.MkdirAll(stagedir, os.ModePerm); err != nil {
return fmt.Errorf("error while creating stage directory %s : %s", stagedir, err)
}
/*find items*/

// find items
ifiles, err := os.ReadDir(itemDirectory + "/" + stage + "/")
if err != nil {
return fmt.Errorf("failed enumerating files of %s : %s", itemDirectory+"/"+stage, err)
Expand All @@ -86,10 +96,12 @@ func restoreHub(dirPath string) error {
for _, tfile := range ifiles {
log.Infof("Going to restore local/tainted [%s]", tfile.Name())
sourceFile := fmt.Sprintf("%s/%s/%s", itemDirectory, stage, tfile.Name())

destinationFile := fmt.Sprintf("%s%s", stagedir, tfile.Name())
if err = CopyFile(sourceFile, destinationFile); err != nil {
return fmt.Errorf("failed copy %s %s to %s : %s", itype, sourceFile, destinationFile, err)
}

log.Infof("restored %s to %s", sourceFile, destinationFile)
}
} else {
Expand All @@ -101,9 +113,9 @@ func restoreHub(dirPath string) error {
}
log.Infof("restored %s to %s", sourceFile, destinationFile)
}

}
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/crowdsec-cli/config_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func showConfigKey(key string) error {
opts := []expr.Option{}
opts = append(opts, exprhelpers.GetExprOptions(map[string]interface{}{})...)
opts = append(opts, expr.Env(Env{}))

program, err := expr.Compile(key, opts...)
if err != nil {
return err
Expand Down Expand Up @@ -52,6 +53,7 @@ func showConfigKey(key string) error {

fmt.Printf("%s\n", string(data))
}

return nil
}

Expand Down Expand Up @@ -211,6 +213,7 @@ func runConfigShow(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

err = tmp.Execute(os.Stdout, csConfig)
if err != nil {
return err
Expand All @@ -230,6 +233,7 @@ func runConfigShow(cmd *cobra.Command, args []string) error {

fmt.Printf("%s\n", string(data))
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/crowdsec-cli/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func SetConsoleOpts(args []string, wanted bool) error {
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
csConfig.API.Server.ConsoleConfig.ConsoleManagement = ptr.Of(wanted)
}

if csConfig.API.Server.OnlineClient.Credentials != nil {
changed := false
if wanted && csConfig.API.Server.OnlineClient.Credentials.PapiURL == "" {
Expand All @@ -271,12 +272,15 @@ func SetConsoleOpts(args []string, wanted bool) error {
changed = true
csConfig.API.Server.OnlineClient.Credentials.PapiURL = ""
}

if changed {
fileContent, err := yaml.Marshal(csConfig.API.Server.OnlineClient.Credentials)
if err != nil {
return fmt.Errorf("cannot marshal credentials: %s", err)
}

log.Infof("Updating credentials file: %s", csConfig.API.Server.OnlineClient.CredentialsFilePath)

err = os.WriteFile(csConfig.API.Server.OnlineClient.CredentialsFilePath, fileContent, 0o600)
if err != nil {
return fmt.Errorf("cannot write credentials file: %s", err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/crowdsec-cli/console_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ func cmdConsoleStatusTable(out io.Writer, csConfig csconfig.Config) {
if *csConfig.API.Server.ConsoleConfig.ShareContext {
activated = string(emoji.CheckMarkButton)
}

t.AddRow(option, activated, "Send context with alerts to the console")
case csconfig.CONSOLE_MANAGEMENT:
activated := string(emoji.CrossMark)
if *csConfig.API.Server.ConsoleConfig.ConsoleManagement {
activated = string(emoji.CheckMarkButton)
}

t.AddRow(option, activated, "Receive decisions from console")
}
}
Expand Down
10 changes: 10 additions & 0 deletions cmd/crowdsec-cli/copyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ func copyFileContents(src, dst string) (err error) {
return
}
defer in.Close()

out, err := os.Create(dst)
if err != nil {
return
}

defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()

if _, err = io.Copy(out, in); err != nil {
return
}

err = out.Sync()

return
}

Expand All @@ -40,18 +45,21 @@ func CopyFile(sourceSymLink, destinationFile string) (err error) {
sourceFile, err := filepath.EvalSymlinks(sourceSymLink)
if err != nil {
log.Infof("Not a symlink : %s", err)

sourceFile = sourceSymLink
}

sourceFileStat, err := os.Stat(sourceFile)
if err != nil {
return
}

if !sourceFileStat.Mode().IsRegular() {
// cannot copy non-regular files (e.g., directories,
// symlinks, devices, etc.)
return fmt.Errorf("copyFile: non-regular source file %s (%q)", sourceFileStat.Name(), sourceFileStat.Mode().String())
}

destinationFileStat, err := os.Stat(destinationFile)
if err != nil {
if !os.IsNotExist(err) {
Expand All @@ -65,9 +73,11 @@ func CopyFile(sourceSymLink, destinationFile string) (err error) {
return
}
}

if err = os.Link(sourceFile, destinationFile); err != nil {
err = copyFileContents(sourceFile, destinationFile)
}

return
}

Loading

0 comments on commit a504113

Please sign in to comment.