Skip to content

Commit

Permalink
feat: add the possibility to trigger environment partial actions
Browse files Browse the repository at this point in the history
Signed-off-by: Aris Buzachis <[email protected]>
  • Loading branch information
aris-bunnyshell committed Oct 11, 2023
1 parent 8c1b0e0 commit 9201a8d
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 19 deletions.
4 changes: 3 additions & 1 deletion cmd/environment/action/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func init() {
options := config.GetOptions()
settings := config.GetSettings()

deployOptions := environment.NewDeployOptions("")
deployOptions := environment.NewDeployOptions("", false, []string{})
deployData := DeployData{}

command := &cobra.Command{
Expand All @@ -29,6 +29,8 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
deployOptions.ID = settings.Profile.Context.Environment

deployOptions.ProcessCommand(cmd)

return handleDeploy(cmd, deployOptions, "", deployData.K8SIntegration)
},
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/environment/action/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func init() {
options := config.GetOptions()
settings := config.GetSettings()

startOptions := environment.NewStartOptions("")
startOptions := environment.NewStartOptions("", []string{})

command := &cobra.Command{
Use: "start",
Expand All @@ -25,6 +25,8 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
startOptions.ID = settings.Profile.Context.Environment

startOptions.ProcessCommand(cmd)

event, err := environment.Start(startOptions)
if err != nil {
return lib.FormatCommandError(cmd, err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/environment/action/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func init() {
options := config.GetOptions()
settings := config.GetSettings()

stopOptions := environment.NewStopOptions("")
stopOptions := environment.NewStopOptions("", []string{})

command := &cobra.Command{
Use: "stop",
Expand All @@ -25,6 +25,8 @@ func init() {
RunE: func(cmd *cobra.Command, args []string) error {
stopOptions.ID = settings.Profile.Context.Environment

stopOptions.ProcessCommand(cmd)

event, err := environment.Stop(stopOptions)
if err != nil {
return lib.FormatCommandError(cmd, err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.16

require (
bunnyshell.com/dev v0.5.5
bunnyshell.com/sdk v0.14.2
bunnyshell.com/sdk v0.15.0
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/briandowns/spinner v1.23.0
github.com/fatih/color v1.15.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ bunnyshell.com/dev v0.5.5 h1:dqFr6ZTPWvfqtgQGDPzHDfG0hNvNMZDqXAKS2+ekFzY=
bunnyshell.com/dev v0.5.5/go.mod h1:KlqPdOh60vqAfnuGUw9AKc0RVhXpNzWg46QKJSP/jog=
bunnyshell.com/sdk v0.14.2 h1:x6V1w+JioFu0jbkRYTCsz0A4mqg9LuSOHlawaFfZBao=
bunnyshell.com/sdk v0.14.2/go.mod h1:RfgfUzZ4WHZGCkToUfu2/hoQS6XsQc8IdPTVAlpS138=
bunnyshell.com/sdk v0.15.0 h1:zPL1OCPTrVs97Muisyz8JoqiN3XqPsE6GLkc5u7cV3k=
bunnyshell.com/sdk v0.15.0/go.mod h1:RfgfUzZ4WHZGCkToUfu2/hoQS6XsQc8IdPTVAlpS138=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down
40 changes: 40 additions & 0 deletions pkg/api/common/partial_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package common

import (
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var componentsList string

type PartialActionOptions struct {
ActionOptions

IsPartial bool
Components []string
}

func NewPartialActionOptions(id string, isPartial bool, components []string) *PartialActionOptions {
return &PartialActionOptions{
ActionOptions: *NewActionOptions(id),

IsPartial: isPartial,
Components: components,
}
}

func (pao *PartialActionOptions) UpdateFlagSet(flags *pflag.FlagSet) {
pao.ActionOptions.UpdateFlagSet(flags)

flags.StringVar(&componentsList, "components", componentsList, "Execute a partial action with the set components (comma separated names)")
}

func (pao *PartialActionOptions) ProcessCommand(cmd *cobra.Command) {
pao.IsPartial = cmd.Flags().Lookup("components").Changed

if pao.IsPartial {
pao.Components = strings.Split(componentsList, ",")
}
}
2 changes: 1 addition & 1 deletion pkg/api/environment/action_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewCreateOptions() *CreateOptions {
environmentCreateAction.SetEphemeralKubernetesIntegration("")

return &CreateOptions{
DeployOptions: *NewDeployOptions(""),
DeployOptions: *NewDeployOptions("", false, []string{}),

EnvironmentCreateAction: *environmentCreateAction,
}
Expand Down
29 changes: 25 additions & 4 deletions pkg/api/environment/action_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,34 @@ import (
"bunnyshell.com/cli/pkg/api/common"
"bunnyshell.com/cli/pkg/lib"
"bunnyshell.com/sdk"
"github.com/spf13/pflag"
)

const (
IncludedDepdendenciesNone string = "none"
IncludedDepdendenciesAll string = "all"
IncludedDepdendenciesMissing string = "missing"
)

type DeployOptions struct {
common.ActionOptions
common.PartialActionOptions

IncludedDepdendencies string
}

func NewDeployOptions(id string) *DeployOptions {
func NewDeployOptions(id string, isPartial bool, components []string) *DeployOptions {
return &DeployOptions{
ActionOptions: *common.NewActionOptions(id),
PartialActionOptions: *common.NewPartialActionOptions(id, isPartial, components),
IncludedDepdendencies: IncludedDepdendenciesNone,
}
}

func (options *DeployOptions) UpdateFlagSet(flags *pflag.FlagSet) {
options.PartialActionOptions.UpdateFlagSet(flags)

flags.StringVar(&options.IncludedDepdendencies, "included-dependencies", options.IncludedDepdendencies, "Include dependencies in the deployment (none, all, missing)")
}

func Deploy(options *DeployOptions) (*sdk.EventItem, error) {
model, resp, err := DeployRaw(options)
if err != nil {
Expand All @@ -34,7 +50,12 @@ func DeployRaw(options *DeployOptions) (*sdk.EventItem, *http.Response, error) {
ctx, cancel := lib.GetContextFromProfile(profile)
defer cancel()

request := lib.GetAPIFromProfile(profile).EnvironmentAPI.EnvironmentDeploy(ctx, options.ID)
request := lib.GetAPIFromProfile(profile).EnvironmentAPI.EnvironmentDeploy(ctx, options.ID).
EnvironmentPartialDeployAction(sdk.EnvironmentPartialDeployAction{
IsPartial: &options.IsPartial,
Components: options.Components,
IncludedDependencies: &options.IncludedDepdendencies,
})

return request.Execute()
}
2 changes: 1 addition & 1 deletion pkg/api/environment/action_edit_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type EditComponentsData struct {

func NewEditComponentOptions() *EditComponentOptions {
return &EditComponentOptions{
DeployOptions: *NewDeployOptions(""),
DeployOptions: *NewDeployOptions("", false, []string{}),
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/environment/action_edit_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewEditConfigurationOptions(environment string) *EditConfigurationOptions {
environmentEditConfiguration := sdk.NewEnvironmentEditConfiguration()

return &EditConfigurationOptions{
DeployOptions: *NewDeployOptions(environment),
DeployOptions: *NewDeployOptions(environment, false, []string{}),

EnvironmentEditConfiguration: *environmentEditConfiguration,
}
Expand Down
22 changes: 18 additions & 4 deletions pkg/api/environment/action_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@ import (
"bunnyshell.com/cli/pkg/api/common"
"bunnyshell.com/cli/pkg/lib"
"bunnyshell.com/sdk"
"github.com/spf13/pflag"
)

type StartOptions struct {
common.ActionOptions
common.PartialActionOptions

WithDependencies bool
}

func NewStartOptions(id string) *StartOptions {
func NewStartOptions(id string, components []string) *StartOptions {
return &StartOptions{
ActionOptions: *common.NewActionOptions(id),
PartialActionOptions: *common.NewPartialActionOptions(id, len(components) > 0, components),
}
}

func (options *StartOptions) UpdateFlagSet(flags *pflag.FlagSet) {
options.PartialActionOptions.UpdateFlagSet(flags)

flags.BoolVar(&options.WithDependencies, "with-dependencies", options.WithDependencies, "Start the component dependencies too.")
}

func Start(options *StartOptions) (*sdk.EventItem, error) {
model, resp, err := StartRaw(options)
if err != nil {
Expand All @@ -34,7 +43,12 @@ func StartRaw(options *StartOptions) (*sdk.EventItem, *http.Response, error) {
ctx, cancel := lib.GetContextFromProfile(profile)
defer cancel()

request := lib.GetAPIFromProfile(profile).EnvironmentAPI.EnvironmentStart(ctx, options.ID)
request := lib.GetAPIFromProfile(profile).EnvironmentAPI.EnvironmentStart(ctx, options.ID).
EnvironmentPartialStartAction(sdk.EnvironmentPartialStartAction{
IsPartial: &options.IsPartial,
Components: options.Components,
WithDependencies: &options.WithDependencies,
})

return request.Execute()
}
12 changes: 8 additions & 4 deletions pkg/api/environment/action_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
)

type StopOptions struct {
common.ActionOptions
common.PartialActionOptions
}

func NewStopOptions(id string) *StopOptions {
func NewStopOptions(id string, components []string) *StopOptions {
return &StopOptions{
ActionOptions: *common.NewActionOptions(id),
PartialActionOptions: *common.NewPartialActionOptions(id, len(components) > 0, components),
}
}

Expand All @@ -34,7 +34,11 @@ func StopRaw(options *StopOptions) (*sdk.EventItem, *http.Response, error) {
ctx, cancel := lib.GetContextFromProfile(profile)
defer cancel()

request := lib.GetAPIFromProfile(profile).EnvironmentAPI.EnvironmentStop(ctx, options.ID)
request := lib.GetAPIFromProfile(profile).EnvironmentAPI.EnvironmentStop(ctx, options.ID).
EnvironmentPartialAction(sdk.EnvironmentPartialAction{
IsPartial: &options.IsPartial,
Components: options.Components,
})

return request.Execute()
}

0 comments on commit 9201a8d

Please sign in to comment.