From 9c142cc89ea999260fc7f53ba43e459e98174521 Mon Sep 17 00:00:00 2001 From: NoamSDahan Date: Thu, 17 Aug 2023 18:00:45 +0300 Subject: [PATCH] Add functionality to import scenarios from a local directory - Introduce `import-scenarios` subcommand to import scenarios from a specified local directory. - Implement the `ImportScenarios` method in `registry.go`. --- .../maintenance/commands/import_scenarios.go | 41 +++++++++++++++++++ .../commands/maintenance/maintenanace.go | 1 + registry.go | 4 ++ 3 files changed, 46 insertions(+) create mode 100644 cmd/commands/commands/maintenance/commands/import_scenarios.go diff --git a/cmd/commands/commands/maintenance/commands/import_scenarios.go b/cmd/commands/commands/maintenance/commands/import_scenarios.go new file mode 100644 index 0000000..04da99d --- /dev/null +++ b/cmd/commands/commands/maintenance/commands/import_scenarios.go @@ -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 + }, +} diff --git a/cmd/commands/commands/maintenance/maintenanace.go b/cmd/commands/commands/maintenance/maintenanace.go index c680d2b..c7a9cd0 100644 --- a/cmd/commands/commands/maintenance/maintenanace.go +++ b/cmd/commands/commands/maintenance/maintenanace.go @@ -20,6 +20,7 @@ var MaintenanceCommand = &cli.Command{ plugins.PluginsCommand, stacks.StacksCommand, commands.GetScenariosCommand, + commands.ImportScenariosCommand, }, Hidden: true, } diff --git a/registry.go b/registry.go index 67d6bbb..070c728 100644 --- a/registry.go +++ b/registry.go @@ -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