-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipelineRun.go
85 lines (70 loc) · 2.31 KB
/
pipelineRun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package pipeline
import (
"context"
"fmt"
"strings"
"github.com/clintjedwards/gofer/internal/cli/cl"
proto "github.com/clintjedwards/gofer/proto/go"
"github.com/fatih/color"
"github.com/spf13/cobra"
"google.golang.org/grpc/metadata"
)
var cmdPipelineRun = &cobra.Command{
Use: "run <pipeline_id>",
Short: "Start a new run",
Example: `$ gofer pipeline run simple_test_pipeline`,
RunE: pipelineRun,
Args: cobra.ExactArgs(1),
}
func init() {
cmdPipelineRun.Flags().StringSliceP("variable", "v", []string{}, "optional environment variables to pass to your run. Format: Key=Value")
CmdPipeline.AddCommand(cmdPipelineRun)
}
func pipelineRun(cmd *cobra.Command, args []string) error {
pipelineID := args[0]
variableList, err := cmd.Flags().GetStringSlice("variable")
if err != nil {
cl.State.Fmt.PrintErr(err)
cl.State.Fmt.Finish()
return err
}
variables := map[string]string{}
for _, variable := range variableList {
key, value, found := strings.Cut(variable, "=")
if !found {
cl.State.Fmt.PrintErr(fmt.Sprintf("malformed variable %q; must be in format <KEY>=<VALUE>", variable))
cl.State.Fmt.Finish()
return fmt.Errorf("malformed variable")
}
variables[key] = value
}
cl.State.Fmt.Print("Starting run")
conn, err := cl.State.Connect()
if err != nil {
cl.State.Fmt.PrintErr(err)
cl.State.Fmt.Finish()
return err
}
client := proto.NewGoferClient(conn)
md := metadata.Pairs("Authorization", "Bearer "+cl.State.Config.Token)
ctx := metadata.NewOutgoingContext(context.Background(), md)
resp, err := client.StartRun(ctx, &proto.StartRunRequest{
NamespaceId: cl.State.Config.Namespace,
PipelineId: pipelineID,
Variables: variables,
Initiator: &proto.Initiator{
Type: proto.Initiator_HUMAN,
Name: "cli",
},
})
if err != nil {
cl.State.Fmt.PrintErr(fmt.Sprintf("could not start run: %v", err))
cl.State.Fmt.Finish()
return err
}
cl.State.Fmt.PrintSuccess(fmt.Sprintf("Started new run (%d) for pipeline %s", resp.Run.Id, pipelineID))
cl.State.Fmt.Println(fmt.Sprintf("\n View details of your new run: %s", color.YellowString("gofer run get %s %d", resp.Run.Pipeline, resp.Run.Id)))
cl.State.Fmt.Println(fmt.Sprintf(" List all task runs: %s", color.YellowString("gofer taskrun list %s %d", resp.Run.Pipeline, resp.Run.Id)))
cl.State.Fmt.Finish()
return nil
}