Skip to content

Commit

Permalink
Add support for environment variables groups
Browse files Browse the repository at this point in the history
  • Loading branch information
BunnyCrlu committed Oct 2, 2024
1 parent 2a455ef commit 782d1cf
Show file tree
Hide file tree
Showing 15 changed files with 673 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bunnyshell.com/cli/cmd/template"
"bunnyshell.com/cli/cmd/utils"
"bunnyshell.com/cli/cmd/variable"
"bunnyshell.com/cli/cmd/variable_group"
"bunnyshell.com/cli/cmd/version"
"bunnyshell.com/cli/pkg/build"
"bunnyshell.com/cli/pkg/config"
Expand Down Expand Up @@ -96,6 +97,7 @@ func init() {
project_variable.GetMainCommand(),
registry_integration.GetMainCommand(),
variable.GetMainCommand(),
variable_group.GetMainCommand(),
k8sIntegration.GetMainCommand(),
pipeline.GetMainCommand(),
secret.GetMainCommand(),
Expand Down
81 changes: 81 additions & 0 deletions cmd/variable_group/action/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package action

import (
"io"
"os"

"bunnyshell.com/cli/pkg/api/variable_group"
"bunnyshell.com/cli/pkg/config"
"bunnyshell.com/cli/pkg/lib"
"bunnyshell.com/cli/pkg/util"
"github.com/spf13/cobra"
)

func init() {
options := config.GetOptions()
settings := config.GetSettings()

createOptions := variable_group.NewCreateOptions()

command := &cobra.Command{
Use: "create",

ValidArgsFunction: cobra.NoFileCompletions,

PreRunE: func(cmd *cobra.Command, args []string) error {
hasStdin, err := util.IsStdinPresent()
if err != nil {
return err
}

flags := cmd.Flags()
if !flags.Changed("value") && !hasStdin {
return errMissingValue
}

if flags.Changed("value") && hasStdin {
return errMultipleValueInputs
}

return nil
},

RunE: func(cmd *cobra.Command, args []string) error {
createOptions.Environment = settings.Profile.Context.Environment

hasStdin, err := util.IsStdinPresent()
if err != nil {
return err
}

if hasStdin {
buf, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}

createOptions.Value = string(buf)
}

model, err := variable_group.Create(createOptions)
if err != nil {
return lib.FormatCommandError(cmd, err)
}

return lib.FormatCommandData(cmd, model)
},
}

flags := command.Flags()

flags.AddFlag(options.Environment.AddFlagWithExtraHelp(
"environment",
"Environment for the variable",
"Environments contain multiple variables",
util.FlagRequired,
))

createOptions.UpdateFlagSet(flags)

mainCmd.AddCommand(command)
}
34 changes: 34 additions & 0 deletions cmd/variable_group/action/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package action

import (
"bunnyshell.com/cli/pkg/api/variable_group"
"bunnyshell.com/cli/pkg/lib"
"github.com/spf13/cobra"
)

func init() {
deleteOptions := variable_group.NewDeleteOptions()

command := &cobra.Command{
Use: "delete",

ValidArgsFunction: cobra.NoFileCompletions,

RunE: func(cmd *cobra.Command, args []string) error {
err := variable_group.Delete(deleteOptions)
if err != nil {
return lib.FormatCommandError(cmd, err)
}

cmd.Printf("\nGrouped environment variable %s successfully deleted\n", deleteOptions.ID)

return nil
},
}

flags := command.Flags()

flags.AddFlag(GetIDOption(&deleteOptions.ID).GetRequiredFlag("id"))

mainCmd.AddCommand(command)
}
70 changes: 70 additions & 0 deletions cmd/variable_group/action/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package action

import (
"io"
"os"

"bunnyshell.com/cli/pkg/api/variable_group"
"bunnyshell.com/cli/pkg/lib"
"bunnyshell.com/cli/pkg/util"
"github.com/spf13/cobra"
)

func init() {
editOptions := variable_group.NewEditOptions("")

command := &cobra.Command{
Use: "edit",

ValidArgsFunction: cobra.NoFileCompletions,

PreRunE: func(cmd *cobra.Command, _ []string) error {
hasStdin, err := util.IsStdinPresent()
if err != nil {
return err
}

flags := cmd.Flags()
if flags.Changed("value") && hasStdin {
return errMultipleValueInputs
}

return nil
},

RunE: func(cmd *cobra.Command, _ []string) error {
flags := cmd.Flags()
if flags.Changed("value") {
editOptions.EnvironItemEditAction.SetValue(flags.Lookup("value").Value.String())
}

hasStdin, err := util.IsStdinPresent()
if err != nil {
return err
}

if hasStdin {
buf, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}

editOptions.EnvironItemEditAction.SetValue(string(buf))
}

model, err := variable_group.Edit(editOptions)
if err != nil {
return lib.FormatCommandError(cmd, err)
}

return lib.FormatCommandData(cmd, model)
},
}

flags := command.Flags()

flags.AddFlag(GetIDOption(&editOptions.ID).GetRequiredFlag("id"))
editOptions.UpdateFlagSet(flags)

mainCmd.AddCommand(command)
}
34 changes: 34 additions & 0 deletions cmd/variable_group/action/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package action

import (
"errors"
"fmt"

"bunnyshell.com/cli/pkg/build"
"bunnyshell.com/cli/pkg/config/option"
"github.com/spf13/cobra"
)

var (
errMissingValue = errors.New("the plain value must be provided")
errMultipleValueInputs = errors.New("the value must be provided either by argument or by stdin, not both")
)

var mainCmd = &cobra.Command{}

func GetMainCommand() *cobra.Command {
return mainCmd
}

func GetIDOption(value *string) *option.String {
help := fmt.Sprintf(
`Find available environment variables with "%s variables list"`,
build.Name,
)

idOption := option.NewStringOption(value)

idOption.AddFlagWithExtraHelp("id", "Environment Variable Id", help)

return idOption
}
39 changes: 39 additions & 0 deletions cmd/variable_group/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package variable_group

import (
"bunnyshell.com/cli/pkg/api/variable_group"
"bunnyshell.com/cli/pkg/config"
"bunnyshell.com/cli/pkg/lib"
"github.com/spf13/cobra"
)

func init() {
options := config.GetOptions()
settings := config.GetSettings()

listOptions := variable_group.NewListOptions()

command := &cobra.Command{
Use: "list",

ValidArgsFunction: cobra.NoFileCompletions,

RunE: func(cmd *cobra.Command, _ []string) error {
listOptions.Organization = settings.Profile.Context.Organization
listOptions.Environment = settings.Profile.Context.Environment

return lib.ShowCollection(cmd, listOptions, func() (lib.ModelWithPagination, error) {
return variable_group.List(listOptions)
})
},
}

flags := command.Flags()

flags.AddFlag(options.Organization.GetFlag("organization"))
flags.AddFlag(options.Environment.GetFlag("environment"))

listOptions.UpdateFlagSet(flags)

mainCmd.AddCommand(command)
}
33 changes: 33 additions & 0 deletions cmd/variable_group/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package variable_group

import (
"bunnyshell.com/cli/cmd/variable_group/action"
"bunnyshell.com/cli/pkg/config"
"bunnyshell.com/cli/pkg/util"
"github.com/spf13/cobra"
)

var mainCmd = &cobra.Command{
Use: "variables-groups",
Aliases: []string{"variables-group", "var-groups", "var-group", "var-g"},

Short: "Grouped Environment Variables",
Long: "Bunnyshell Environment Variables in Groups",
}

func init() {
config.MainManager.CommandWithAPI(mainCmd)

util.AddGroupedCommands(
mainCmd,
cobra.Group{
ID: "actions",
Title: "Commands for Environment Variables:",
},
action.GetMainCommand().Commands(),
)
}

func GetMainCommand() *cobra.Command {
return mainCmd
}
33 changes: 33 additions & 0 deletions cmd/variable_group/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package variable_group

import (
"bunnyshell.com/cli/cmd/variable/action"
"bunnyshell.com/cli/pkg/api/variable_group"
"bunnyshell.com/cli/pkg/lib"
"github.com/spf13/cobra"
)

func init() {
itemOptions := variable_group.NewItemOptions("")

command := &cobra.Command{
Use: "show",

ValidArgsFunction: cobra.NoFileCompletions,

RunE: func(cmd *cobra.Command, _ []string) error {
model, err := variable_group.Get(itemOptions)
if err != nil {
return lib.FormatCommandError(cmd, err)
}

return lib.FormatCommandData(cmd, model)
},
}

flags := command.Flags()

flags.AddFlag(action.GetIDOption(&itemOptions.ID).GetRequiredFlag("id"))

mainCmd.AddCommand(command)
}
Loading

0 comments on commit 782d1cf

Please sign in to comment.