Skip to content

Commit

Permalink
Merge pull request #45 from aris-bunnyshell/feat/partial-actions
Browse files Browse the repository at this point in the history
feat: add the possibility to trigger environment partial actions
  • Loading branch information
aris-bunnyshell authored Oct 12, 2023
2 parents 8c1b0e0 + c4b89e4 commit aa56722
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 10 deletions.
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
52 changes: 52 additions & 0 deletions pkg/api/common/partial_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package common

import (
"fmt"

"github.com/spf13/pflag"
)

const componentVarName = "component"

type PartialActionOptions struct {
ActionOptions

components []string

flags *pflag.FlagSet
}

func NewPartialActionOptions(id string) *PartialActionOptions {
return &PartialActionOptions{
ActionOptions: *NewActionOptions(id),

components: []string{},
}
}

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

// We'll need this later to check if the flag was provided at all - in order to determine if it was a partial action or not.
pao.flags = flags

usage := fmt.Sprintf("Execute a partial action with the set components. Provide \"--%s ''\" for a no-components operation.", componentVarName)
flags.StringArrayVar(&pao.components, componentVarName, pao.components, usage)
}

// Handle the "--component ”" case.
func (pao *PartialActionOptions) GetActionComponents() []string {
if len(pao.components) == 1 && pao.components[0] == "" {
return []string{}
}

return pao.components
}

func (pao *PartialActionOptions) IsPartial() bool {
if pao.flags == nil {
return false
}

return pao.flags.Lookup(componentVarName).Changed
}
29 changes: 26 additions & 3 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 {
return &DeployOptions{
ActionOptions: *common.NewActionOptions(id),
PartialActionOptions: *common.NewPartialActionOptions(id),
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,14 @@ 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)
isPartialAction := options.IsPartial()

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

return request.Execute()
}
22 changes: 19 additions & 3 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 {
return &StartOptions{
ActionOptions: *common.NewActionOptions(id),
PartialActionOptions: *common.NewPartialActionOptions(id),
}
}

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,14 @@ 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)
isPartialAction := options.IsPartial()

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

return request.Execute()
}
12 changes: 9 additions & 3 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 {
return &StopOptions{
ActionOptions: *common.NewActionOptions(id),
PartialActionOptions: *common.NewPartialActionOptions(id),
}
}

Expand All @@ -34,7 +34,13 @@ 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)
isPartialAction := options.IsPartial()

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

return request.Execute()
}

0 comments on commit aa56722

Please sign in to comment.