Skip to content

Commit

Permalink
dynamic vars
Browse files Browse the repository at this point in the history
  • Loading branch information
o11yguru authored and arcanez committed Oct 2, 2024
1 parent 8464117 commit 083dccc
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 1 deletion.
159 changes: 159 additions & 0 deletions cmd/dynamic_vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package cmd

import (
"errors"
"fmt"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/spf13/cobra"
)

func NewDynamicVariablesCommand() *cobra.Command {
cmd := &cobra.Command{
GroupID: "configuration",
Use: "dynamic_variables",
Short: "manage dynamic variables",
Long: `manage dynamic variables`,
}

cmd.DisableFlagsInUseLine = true
cmd.SilenceUsage = true

cmd.AddCommand(
NewDynamicVariablesAddCommand(),
NewDynamicVariablesListCommand(),
NewDynamicVariablesRemoveCommand(),
)

return cmd
}

// Define DynamicVariables as a list of key-value pairs (map[string]string)
type DynamicVariables []map[string]string

func GetMockDynamicVars() DynamicVariables {
// Initialize DynamicVariables with some key-value pairs
vars := DynamicVariables{
{"key": "environment", "value": "production"},
{"key": "version", "value": "1.0.0"},
{"key": "region", "value": "us-west-1"},
}
return vars
}

func NewDynamicVariablesListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list dynamic variables",
Long: `list dynamic variables`,
RunE: func(cmd *cobra.Command, _ []string) error {
dynamicVars := GetMockDynamicVars()

var rows [][]string
headers := []string{"Key", "Value", "Status"}

for _, dynamicVar := range dynamicVars {
row := []string{dynamicVar["key"], dynamicVar["value"], "Enabled"}
rows = append(rows, row)
}

printDynamicVariables := func(headers []string, rows [][]string) {
if len(rows) == 0 {
return
}
dynamicVariablesOutput := table.New().
BorderHeader(false).
Border(lipgloss.HiddenBorder()).
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == 0:
return HeaderStyle
default:
return lipgloss.NewStyle()
}
}).
Headers(headers...).
Rows(rows...)
fmt.Println(dynamicVariablesOutput)
}

printDynamicVariables(headers, rows)
return nil
},
}

cmd.DisableFlagsInUseLine = true
cmd.SilenceUsage = true

return cmd
}

type dynamicVariableAddFlags struct {
key string
value string
}


func (f dynamicVariableAddFlags) successString() string {
var sb strings.Builder
_, _ = fmt.Fprintf(&sb, `dynamic variable added successfully as: "%s" (%s).`, f.key, f.value)
_, _ = fmt.Fprintln(&sb)
return sb.String()
}

func NewDynamicVariablesAddCommand() *cobra.Command {
f := dynamicVariableAddFlags{}

cmd := &cobra.Command{
Use: "add",
Short: "add a dynamic variable",
Long: `add a dynamic variable`,
Example: ` add --key some_key --value s0m3v@lu3@5@`,
PreRunE: func(cmd *cobra.Command, _ []string) error {
cmd.MarkFlagsRequiredTogether("key", "value")

return nil
},
RunE: func(cmd *cobra.Command, _ []string) error {
if cmd.Flags().NFlag() == 0 {
return errors.New(cmd.UsageString())
}

fmt.Println(f.successString())
return nil
},
}

cmd.Flags().StringVarP(&f.key, "key", "k", "", "key of the dynamic variable")
cmd.Flags().StringVarP(&f.value, "value", "v", "", "value of the dynamic variable")

cmd.DisableFlagsInUseLine = true
cmd.SilenceUsage = true

return cmd
}

func NewDynamicVariablesRemoveCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "remove",
Short: "remove a dynamic variable",
Long: `remove a dynamic variable`,
Example: ` remove --key some_key`,
RunE: func(cmd *cobra.Command, _ []string) error {
dynamicVariableKey, _ := cmd.Flags().GetString("key")

fmt.Printf(`"%s" dynamic variable removed successfully.`, dynamicVariableKey)
fmt.Println()
return nil
},
}

cmd.Flags().StringP("key", "k", "", "name of the dynamic variable")
_ = cmd.MarkFlagRequired("key")

cmd.DisableFlagsInUseLine = true
cmd.SilenceUsage = true

return cmd
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ func addCommands(cmd *cobra.Command) {
NewStatusCommand(),
NewUninstallCommand(),
NewUpdateCommand(),
// THIS IS A FAKE COMMAND
NewDynamicVariablesCommand(),
)
}

Expand Down
3 changes: 2 additions & 1 deletion docs/md/mdai.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ MyDecisive.ai CLI
### SEE ALSO

* [mdai disable](mdai_disable.md) - disable a module
* [mdai enable](mdai_enable.md) - enable a module
* [mdai enable](mdai_enable.md) - enable a module
* [mdai dynamic_variables](mdai_dynamic_variables.md) - manage dynamic variables
* [mdai filter](mdai_filter.md) - telemetry filtering
* [mdai get](mdai_get.md) - get a configuration
* [mdai install](mdai_install.md) - install MyDecisive Cluster
Expand Down
32 changes: 32 additions & 0 deletions docs/md/mdai_dynamic_variable_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## mdai dynamic variables list

list telemetry dynamic variables

### Synopsis

list telemetry dynamic variables

```
mdai dynamic variables list
```

### Options

```
-h, --help help for list
--pipeline
--service
```

### Options inherited from parent commands

```
--kubeconfig sts Path to a kubeconfig
--kubecontext string Kubernetes context to use
```

### SEE ALSO

* [mdai dynamic_variables](mdai_dynamic_variables.md) - telemetry dynamic variables

###### Auto generated by spf13/cobra on 23-Aug-2024
29 changes: 29 additions & 0 deletions docs/md/mdai_dynamic_variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## mdai dynamic_variables

Manage dynamic variables

### Synopsis

Manage dynamic variables

### Options

```
-h, --help help for filter
```

### Options inherited from parent commands

```
--kubeconfig string Path to a kubeconfig
--kubecontext string Kubernetes context to use
```

### SEE ALSO

* [mdai](mdai.md) - MyDecisive.ai CLI
* [mdai dynamic_variables add](mdai_dynamic_variables_add.md) - add dynamic variables
* [mdai dynamic_variables list](mdai_dynamic_variables_list.md) - list dynamic variables
* [mdai dynamic_variables remove](mdai_dynamic_variables_remove.md) - remove a dynamic variables

###### Auto generated by spf13/cobra on 23-Aug-2024
34 changes: 34 additions & 0 deletions docs/md/mdai_dynamic_variables_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## mdai dynamic variables add

add dynamic variables

```
mdai dynamic_variables add
```

### Examples

```
add --key some_key --description some_value
```

### Options

```
-v, --value string value of the dynamic variables
-h, --help help for add
-k, --key string key of the dynamic variables
```

### Options inherited from parent commands

```
--kubeconfig string Path to a kubeconfig
--kubecontext string Kubernetes context to use
```

### SEE ALSO

* [mdai dynamic_variables](mdai_dynamic variables.md) - dynamic variables

###### Auto generated by spf13/cobra on 23-Aug-2024
37 changes: 37 additions & 0 deletions docs/md/mdai_dynamic_variables_remove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## mdai filter remove

remove a dynamic variable

### Synopsis

remove a dynamic variable

```
mdai dynamic_variable remove
```

### Examples

```
remove --key some_key
```

### Options

```
-h, --help help for remove
-k, --key string key of the dynamic variable
```

### Options inherited from parent commands

```
--kubeconfig string Path to a kubeconfig
--kubecontext string Kubernetes context to use
```

### SEE ALSO

* [mdai dynamic_variable](mdai_dynamic_variable.md) - dynamic variables

###### Auto generated by spf13/cobra on 23-Aug-2024
Loading

0 comments on commit 083dccc

Please sign in to comment.