-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95237af
commit fc16493
Showing
9 changed files
with
277 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/krkn-chaos/krknctl/pkg/provider/factory" | ||
"github.com/spf13/cobra" | ||
"log" | ||
"strings" | ||
) | ||
|
||
func NewRunCommand(factory *factory.ProviderFactory) *cobra.Command { | ||
collectedFlags := make(map[string]*string) | ||
var runCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "runs a scenario", | ||
Long: `runs a scenario`, | ||
DisableFlagParsing: false, | ||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
offline, err := cmd.Flags().GetBool("offline") | ||
//offlineRepo, err := cmd.Flags().GetString("offline-repo-config") | ||
if err != nil { | ||
return []string{}, cobra.ShellCompDirectiveError | ||
} | ||
provider := GetProvider(offline, factory) | ||
scenarios, err := FetchScenarios(provider) | ||
if err != nil { | ||
log.Fatalf("Error fetching scenarios: %v", err) | ||
return []string{}, cobra.ShellCompDirectiveError | ||
} | ||
return *scenarios, cobra.ShellCompDirectiveNoFileComp | ||
}, | ||
|
||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
offline, err := cmd.Flags().GetBool("offline") | ||
if err != nil { | ||
return err | ||
} | ||
provider := GetProvider(offline, factory) | ||
scenarioDetail, err := provider.GetScenarioDetail(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
if scenarioDetail == nil { | ||
return fmt.Errorf("%s scenario not found", args[0]) | ||
} | ||
|
||
for _, field := range scenarioDetail.Fields { | ||
var defaultValue string = "" | ||
if field.Default != nil { | ||
defaultValue = *field.Default | ||
} | ||
collectedFlags[*field.Name] = cmd.LocalFlags().String(*field.Name, defaultValue, *field.Description) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
} | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
spinner := NewSpinnerWithSuffix("validating input...") | ||
offline, err := cmd.Flags().GetBool("offline") | ||
if err != nil { | ||
return err | ||
} | ||
spinner.Start() | ||
|
||
provider := GetProvider(offline, factory) | ||
scenarioDetail, err := provider.GetScenarioDetail(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
spinner.Stop() | ||
// default | ||
for k, _ := range collectedFlags { | ||
field := scenarioDetail.GetFieldByName(k) | ||
var foundArg *string = nil | ||
for i, a := range args { | ||
if a == fmt.Sprintf("--%s", k) { | ||
if len(args) < i+2 || strings.HasPrefix(args[i+1], "--") { | ||
return fmt.Errorf("%s has no value", args[i]) | ||
} | ||
foundArg = &args[i+1] | ||
} | ||
} | ||
if field != nil { | ||
value, err := field.Validate(foundArg) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(fmt.Sprintf("%s: valid", *value)) | ||
} | ||
|
||
} | ||
return nil | ||
}, | ||
} | ||
return runCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,30 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/fatih/color" | ||
"github.com/krkn-chaos/krknctl/pkg/provider/models" | ||
"github.com/krkn-chaos/krknctl/pkg/typing" | ||
) | ||
import "github.com/rodaine/table" | ||
|
||
func NewScenarioTable(scenarios *[]models.ScenarioTag) *table.Table { | ||
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc() | ||
columnFmt := color.New(color.FgYellow).SprintfFunc() | ||
var headerFmt = color.New(color.FgGreen, color.Underline).SprintfFunc() | ||
var columnFmt = color.New(color.FgYellow).SprintfFunc() | ||
|
||
func NewScenarioTable(scenarios *[]models.ScenarioTag) table.Table { | ||
tbl := table.New("Name", "Size", "Digest", "Last Modified") | ||
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt) | ||
for _, scenario := range *scenarios { | ||
tbl.AddRow(scenario.Name, scenario.Size, scenario.Digest, scenario.LastModified) | ||
} | ||
return &tbl | ||
return tbl | ||
} | ||
|
||
func NewArgumentTable(inputFields []typing.InputField) table.Table { | ||
tbl := table.New("Name", "Type", "Description", "Required") | ||
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt) | ||
for _, inputField := range inputFields { | ||
tbl.AddRow(fmt.Sprintf("--%s", *inputField.Name), inputField.Type.String(), *inputField.ShortDescription, inputField.Default == nil) | ||
} | ||
return tbl | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/briandowns/spinner" | ||
"github.com/krkn-chaos/krknctl/pkg/provider/factory" | ||
"github.com/spf13/cobra" | ||
"time" | ||
) | ||
|
||
func NewSpinnerWithSuffix(suffix string) *spinner.Spinner { | ||
s := spinner.New(spinner.CharSets[39], 100*time.Millisecond) | ||
s.Suffix = suffix | ||
return s | ||
} | ||
|
||
func NewRootCommand(factory *factory.ProviderFactory) *cobra.Command { | ||
var rootCmd = &cobra.Command{ | ||
Use: "krknctl", | ||
Short: "krkn CLI", | ||
Long: `krkn Command Line Interface`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cmd.Help() | ||
}, | ||
} | ||
return rootCmd | ||
} | ||
|
||
func GetProvider(offline bool, providerFactory *factory.ProviderFactory) factory.ScenarioDataProvider { | ||
var provider factory.ScenarioDataProvider | ||
if offline { | ||
provider = providerFactory.NewInstance(factory.Offline) | ||
} else { | ||
provider = providerFactory.NewInstance(factory.Online) | ||
} | ||
return provider | ||
} | ||
|
||
func FetchScenarios(provider factory.ScenarioDataProvider) (*[]string, error) { | ||
scenarios, err := provider.GetScenarios() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var foundScenarios []string | ||
for _, scenario := range *scenarios { | ||
foundScenarios = append(foundScenarios, scenario.Name) | ||
} | ||
return &foundScenarios, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.