Skip to content

Commit

Permalink
🎨 Pipeline cli fixes (#1266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Munklinde96 authored Oct 7, 2024
1 parent 3bd0e78 commit 3b3ff9f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 94 deletions.
92 changes: 46 additions & 46 deletions cmd/common/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,9 @@ func updatePipeline(
header := []string{"Environment", "Manual Trigger", "Auto Trigger", "Field Prefixes"}
rows := [][]string{}
for _, p := range pipeline.Phases {
manualTrigger := "None"
if p.GetTriggers().GetManual() != nil {
manualTrigger = triggerToString(p.GetTriggers().GetManual())
}

autoTrigger := "None"
if p.GetTriggers().GetAutomatic() != nil {
autoTrigger = triggerToString(p.GetTriggers().GetAutomatic())
}
fmt.Println(p.GetTriggers())
manualTrigger := triggerToString(p.GetTriggers().GetManual())
autoTrigger := triggerToString(p.GetTriggers().GetAutomatic())

fieldsString := ""
if len(p.GetFieldPrefixes().GetPrefixes()) > 0 {
Expand Down Expand Up @@ -349,30 +343,9 @@ func updateTriggers(prompter Prompter, triggers *model.Triggers) (*model.Trigger
}
triggers = proto.Clone(triggers).(*model.Triggers)

triggerToLabel := func(t *model.Trigger) string {
if t == nil {
return "None"
}

var conditions []string
for _, t := range t.GetConditions() {
switch v := t.GetCondition().(type) {
case *model.Trigger_Condition_TimeAlive:
conditions = append(conditions, fmt.Sprintf("Time Alive: %s", v.TimeAlive.AsDuration().String()))
}
}

triggerType := "one-of"
if t.GetRequireAll() {
triggerType = "all-of"
}

return fmt.Sprintf("%s (%s)", triggerType, strings.Join(conditions, ", "))
}

triggerLabels := []string{
"Auto " + triggerToLabel(triggers.GetAutomatic()),
"Manual " + triggerToLabel(triggers.GetManual()),
"Auto " + triggerToString(triggers.GetAutomatic()),
"Manual " + triggerToString(triggers.GetManual()),
"Done",
}

Expand All @@ -395,7 +368,7 @@ func updateTriggers(prompter Prompter, triggers *model.Triggers) (*model.Trigger
}

triggers.Manual = t
triggerLabels[1] = "Manual " + triggerToLabel(t)
triggerLabels[1] = "Manual " + triggerToString(t)
case 0:
// Add new trigger
t, err := updateTrigger(prompter, triggers.GetAutomatic())
Expand All @@ -407,7 +380,7 @@ func updateTriggers(prompter Prompter, triggers *model.Triggers) (*model.Trigger
}

triggers.Automatic = t
triggerLabels[0] = "Auto " + triggerToLabel(t)
triggerLabels[0] = "Auto " + triggerToString(t)
}
}
}
Expand All @@ -423,9 +396,15 @@ func updateTrigger(prompter Prompter, trigger *model.Trigger) (*model.Trigger, e
require = "Require any condition"
}

enable := "Enable"
if trigger.GetEnabled() {
enable = "Disable"
}

fields := []string{
require,
"Conditions",
require,
enable,
"Clear",
"Done",
}
Expand All @@ -438,12 +417,6 @@ func updateTrigger(prompter Prompter, trigger *model.Trigger) (*model.Trigger, e

switch i {
case 0:
trigger.RequireAll = !trigger.RequireAll
fields[0] = "Require all conditions"
if trigger.GetRequireAll() {
fields[0] = "Require any condition"
}
case 1:
conditions, err := updateConditions(prompter, trigger.GetConditions())
if err != nil {
if ErrIsAborted(err) {
Expand All @@ -453,9 +426,21 @@ func updateTrigger(prompter Prompter, trigger *model.Trigger) (*model.Trigger, e
}

trigger.Conditions = conditions
case 1:
trigger.RequireAll = !trigger.RequireAll
fields[0] = "Require all conditions"
if trigger.GetRequireAll() {
fields[0] = "Require any condition"
}
case 2:
return nil, nil
trigger.Enabled = !trigger.Enabled
fields[1] = "Enable"
if trigger.GetEnabled() {
fields[1] = "Disable"
}
case 3:
return nil, nil
case 4:
return trigger, nil
}
}
Expand Down Expand Up @@ -551,19 +536,34 @@ func updateTimeAliveCondition(prompter Prompter, c *model.Trigger_Condition) (*m
}

func triggerToString(t *model.Trigger) string {
require := "one-of"
if t.GetRequireAll() {
require = "all-of"
if t == nil {
return "None"
}

conditions := []string{}
if !t.GetEnabled() {
return "Disabled"
}

conditions := []string{}
for _, c := range t.GetConditions() {
switch v := c.GetCondition().(type) {
case *model.Trigger_Condition_TimeAlive:
conditions = append(conditions, fmt.Sprintf("Time Alive (%s)", v.TimeAlive.AsDuration().String()))
}
}

if len(conditions) == 0 {
return "Instant"
}

if len(conditions) == 1 {
return conditions[0]
}

require := "one-of"
if t.GetRequireAll() {
require = "all-of"
}

return fmt.Sprintf("%s: %s)", require, strings.Join(conditions, ", "))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pipeline

import (
"context"
"fmt"
"strconv"

"connectrpc.com/connect"
Expand Down Expand Up @@ -56,19 +57,14 @@ func (c *Cmd) progress(ctx context.Context, cmd *cobra.Command, args []string) e
var envLabels []string
var outs []*pipelineDryOutput
for _, out := range resp.Msg.GetDryRunOutcomes() {
capsule := out.GetRevision().GetSpec()
req := &capsule_api.DeployRequest{
CapsuleId: capsule.GetName(),
ProjectId: capsule.GetProject(),
EnvironmentId: capsule.GetEnvironment(),
DryRun: true,
}
resp, err := c.Rig.Capsule().Deploy(ctx, connect.NewRequest(req))
if err != nil {
return err

fmt.Printf("Dry run for environment %v: %v\n", out.GetEnvironmentId(), out.GetOutcome())

if out.GetOutcome() == nil {
continue
}

out2, err := capsule_cmd.ProcessDryRunOutput(out.GetOutcome(), resp.Msg.GetOutcome())
out2, err := capsule_cmd.ProcessDryRunOutput(out.GetOutcome(), out.GetOutcome())
if err != nil {
return err
}
Expand Down
30 changes: 19 additions & 11 deletions cmd/rig/cmd/capsule/pipeline/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/rigdev/rig-go-sdk"
"github.com/rigdev/rig/cmd/common"
"github.com/rigdev/rig/cmd/rig/cmd/capsule"
capsule_cmd "github.com/rigdev/rig/cmd/rig/cmd/capsule"
"github.com/rigdev/rig/cmd/rig/cmd/completions"
"github.com/rigdev/rig/cmd/rig/services/auth"
"github.com/rigdev/rig/pkg/cli"
Expand All @@ -38,6 +39,7 @@ var (
limit int
pipelineName string
dryRun bool
force bool
)

var cmd Cmd
Expand Down Expand Up @@ -123,9 +125,9 @@ func Setup(parent *cobra.Command, s *cli.SetupContext) {
pipeline.AddCommand(pipelineAbort)

pipelineProgress := &cobra.Command{
Use: "progress [execution-id]",
Short: "Progress the pipeline to the next phase. " +
"This will only work if the pipeline is in a state that allows progression. " +
Use: "promote [execution-id]",
Short: "promote the pipeline to the next phase. " +
"This will only work if the pipeline is in a state that allows promotion. " +
"I.e. it has a manual trigger",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: common.Complete(
Expand All @@ -138,8 +140,11 @@ func Setup(parent *cobra.Command, s *cli.SetupContext) {
},
}
pipelineProgress.Flags().BoolVar(&dryRun, "dry-run", false,
"Dry run the progression. If interactive, it will interactively show the diffs. "+
"Dry run the promotion. If interactive, it will interactively show the diffs. "+
"Otherwise it will print the resulting resources.")
pipelineProgress.Flags().BoolVar(&force, "force", false,
"Force the promotion. This will bypass any ready checks and "+
"force a manual promotion no matter the configured triggers.")
pipeline.AddCommand(pipelineProgress)

parent.AddCommand(pipeline)
Expand All @@ -163,23 +168,26 @@ func (c *Cmd) promptForPipelineName(ctx context.Context) (string, error) {
resp, err := c.Rig.Project().GetEffectivePipelineSettings(ctx,
connect.NewRequest(&project_api.GetEffectivePipelineSettingsRequest{
ProjectId: c.Scope.GetCurrentContext().GetProject(),
CapsuleId: capsule_cmd.CapsuleID,
}))
if err != nil {
return "", err
}

header := []string{
"Name",
"Initial Environment",
"1stEnv",
"#Phases",
"Running",
}

var rows [][]string
for _, pipeline := range resp.Msg.GetPipelines() {
rows = append(rows, []string{
pipeline.GetName(),
pipeline.GetInitialEnvironment(),
fmt.Sprint(len(pipeline.GetPhases())),
pipeline.GetPipeline().GetName(),
pipeline.GetPipeline().GetInitialEnvironment(),
fmt.Sprint(len(pipeline.GetPipeline().GetPhases())),
fmt.Sprint(pipeline.GetAlreadyRunning()),
})
}

Expand All @@ -188,7 +196,7 @@ func (c *Cmd) promptForPipelineName(ctx context.Context) (string, error) {
return "", err
}

return resp.Msg.GetPipelines()[i].GetName(), nil
return resp.Msg.GetPipelines()[i].GetPipeline().GetName(), nil
}

func (c *Cmd) promptForPipelineID(ctx context.Context) (string, error) {
Expand Down Expand Up @@ -298,8 +306,8 @@ func (c *Cmd) pipelineNameCompletion(

var pipelineNames []string
for _, pipeline := range resp.Msg.GetPipelines() {
if strings.HasPrefix(pipeline.GetName(), toComplete) {
pipelineNames = append(pipelineNames, pipeline.GetName())
if strings.HasPrefix(pipeline.GetPipeline().GetName(), toComplete) {
pipelineNames = append(pipelineNames, pipeline.GetPipeline().GetName())
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/rig/cmd/capsule/root/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ forwarded traffic.
capsuleCmd.AddCommand(capsuleListSetProposal)

capsulePromote := &cobra.Command{
Use: "promote [capsule] [from-environment] [to-environment]",
Short: "Promote a capsule from one environment to another",
Use: "copy [capsule] [from-environment] [to-environment]",
Short: "copy a capsule from one environment to another",
Args: cobra.MaximumNArgs(3),
RunE: cli.CtxWrap(cmd.promote),
ValidArgsFunction: common.ChainCompletions(
Expand Down
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/nyaruka/phonenumbers v1.1.7
github.com/pkg/errors v0.9.1
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.70.0
github.com/rigdev/rig-go-api v0.0.0-20241003083013-6515051df8b3
github.com/rigdev/rig-go-api v0.0.0-20241007181528-92da8b148ff2
github.com/rigdev/rig-go-sdk v0.0.0-20240612092526-69df8621bc22
github.com/rivo/tview v0.0.0-20240524063012-037df494fb76
github.com/robfig/cron v1.2.0
Expand All @@ -49,10 +49,10 @@ require (
go.uber.org/fx v1.19.3
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
golang.org/x/net v0.29.0
golang.org/x/term v0.24.0
golang.org/x/net v0.30.0
golang.org/x/term v0.25.0
google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.34.2
google.golang.org/protobuf v1.35.1
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
helm.sh/helm/v3 v3.15.4
Expand Down Expand Up @@ -173,16 +173,16 @@ require (
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.6.0 // indirect
golang.org/x/tools v0.25.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
Expand Down
Loading

0 comments on commit 3b3ff9f

Please sign in to comment.