Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environ groups #64

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/audit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22
go-version: 1.23

- name: Download dependencies
run: go mod download
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22
go-version: 1.23
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand Down
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 @@
"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 @@ -74,7 +75,7 @@
},
}

func Execute() {

Check failure on line 78 in cmd/root.go

View workflow job for this annotation

GitHub Actions / audit

exported function Execute should have comment or be unexported
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
Expand All @@ -96,6 +97,7 @@
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)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module bunnyshell.com/cli

go 1.22.0
go 1.23

toolchain go1.22.3
toolchain go1.23.2

replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.16

require (
bunnyshell.com/dev v0.6.0
bunnyshell.com/sdk v0.19.2
bunnyshell.com/sdk v0.20.0
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/MakeNowJust/heredoc v1.0.0
github.com/avast/retry-go/v4 v4.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
bunnyshell.com/dev v0.6.0 h1:JywvLrzxYKgnxegPByh+ET7CwFme6JVeJpWzRuqQTWc=
bunnyshell.com/dev v0.6.0/go.mod h1:+Xk46UXX9AW0nHrFMdO/IwpUPfALrck1/qI+LIXsDmE=
bunnyshell.com/sdk v0.19.2 h1:HcwwD4Pv8itkHtZCxowQMIx+9bIzQedTnlZ9WpBwaTQ=
bunnyshell.com/sdk v0.19.2/go.mod h1:RfgfUzZ4WHZGCkToUfu2/hoQS6XsQc8IdPTVAlpS138=
bunnyshell.com/sdk v0.20.0 h1:xcoDn0x1JqexMy5vYGqTBK4DGdY3juc+5DU7vb1yFeA=
bunnyshell.com/sdk v0.20.0/go.mod h1:RfgfUzZ4WHZGCkToUfu2/hoQS6XsQc8IdPTVAlpS138=
github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ=
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
Expand Down
Loading
Loading