Skip to content

Commit

Permalink
Add functionality to import scenarios from a local directory
Browse files Browse the repository at this point in the history
- Introduce `import-scenarios` subcommand to import scenarios from a specified local directory.
- Implement the `ImportScenarios` method in `registry.go`.
  • Loading branch information
noamsdahan committed Aug 20, 2023
1 parent 8d07778 commit 9c142cc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmd/commands/commands/maintenance/commands/import_scenarios.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package commands

import (
"fmt"
cnappgoat "github.com/ermetic-research/CNAPPgoat"
"github.com/ermetic-research/CNAPPgoat/cmd/commands/common"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

var ImportScenariosCommand = &cli.Command{
Name: "import-scenarios",
Usage: "maintenance subcommand to import scenarios from a local directory to the CNAPPgoat registry",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "directory",
Usage: "Directory to import scenarios from",
Aliases: []string{"d"},
Value: "",
},
},
Before: func(c *cli.Context) error {
if err := common.CommandBefore(c); err != nil {
return err
}

if c.String("directory") == "" {
return fmt.Errorf("directory flag is required in import-scenarios subcommand")
}

return nil
},
Action: func(c *cli.Context) error {
registry := c.Context.Value("CNAPPgoatModuleRegistry").(*cnappgoat.Registry)
logrus.Infof("importing scenarios from %s", c.String("directory"))
if _, err := registry.ImportScenarios(c.String("directory")); err != nil {
return err
}
return nil
},
}
1 change: 1 addition & 0 deletions cmd/commands/commands/maintenance/maintenanace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var MaintenanceCommand = &cli.Command{
plugins.PluginsCommand,
stacks.StacksCommand,
commands.GetScenariosCommand,
commands.ImportScenariosCommand,
},
Hidden: true,
}
4 changes: 4 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func (r *Registry) SetState(scenario *Scenario, state State) error {
return r.storage.WriteStateToFile(r.scenarios[scenario.ScenarioParams.ID], state)
}

func (r *Registry) ImportScenarios(path string) (map[string]*Scenario, error) {
return r.storage.updateScenariosFromFolder(path)
}

func sortScenarios(scenarios []*Scenario) {
sort.Slice(scenarios, func(i, j int) bool {
return scenarios[i].ScenarioParams.ID < scenarios[j].ScenarioParams.ID
Expand Down

0 comments on commit 9c142cc

Please sign in to comment.