-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
123 lines (105 loc) · 2.93 KB
/
update.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package pipelines
import (
"io/ioutil"
"os"
strfmt "github.com/go-openapi/strfmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/cycloidio/cycloid-cli/cmd/cycloid/common"
"github.com/cycloidio/cycloid-cli/cmd/cycloid/internal"
"github.com/cycloidio/cycloid-cli/cmd/cycloid/middleware"
"github.com/cycloidio/cycloid-cli/printer"
"github.com/cycloidio/cycloid-cli/printer/factory"
)
func NewUpdateCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "update",
Short: "update a running pipeline",
Example: `
# update a running pipeline
cy --org my-org pp update --project my-project --env my-env --vars /path/to/vars.yml --pipeline /path/to/pipeline.yml --config /path/config.tf
`,
RunE: update,
PreRunE: internal.CheckAPIAndCLIVersion,
}
common.RequiredPersistentFlag(common.WithFlagProject, cmd)
common.RequiredPersistentFlag(common.WithFlagEnv, cmd)
common.RequiredFlag(WithFlagPipeline, cmd)
common.RequiredFlag(WithFlagVars, cmd)
WithFlagConfig(cmd)
return cmd
}
func update(cmd *cobra.Command, args []string) error {
api := common.NewAPI()
m := middleware.NewMiddleware(api)
var err error
org, err := cmd.Flags().GetString("org")
if err != nil {
return err
}
project, err := cmd.Flags().GetString("project")
if err != nil {
return err
}
env, err := cmd.Flags().GetString("env")
if err != nil {
return err
}
varsPath, err := cmd.Flags().GetString("vars")
if err != nil {
return err
}
pipelinePath, err := cmd.Flags().GetString("pipeline")
if err != nil {
return err
}
configs, err := cmd.Flags().GetStringToString("config")
if err != nil {
return err
}
output, err := cmd.Flags().GetString("output")
if err != nil {
return errors.Wrap(err, "unable to get output flag")
}
rawPipeline, err := ioutil.ReadFile(pipelinePath)
if err != nil {
return errors.Wrap(err, "unable to read pipeline file")
}
pipeline := string(rawPipeline)
rawVars, err := ioutil.ReadFile(varsPath)
if err != nil {
return errors.Wrap(err, "unable to read variables file")
}
variables := string(rawVars)
resp, err := m.UpdatePipeline(org, project, env, pipeline, variables)
if err != nil {
return errors.Wrap(err, "unable to update pipeline")
}
// fetch the printer from the factory
p, err := factory.GetPrinter(output)
if err != nil {
return errors.Wrap(err, "unable to get printer")
}
// print the result on the standard output
if err := p.Print(resp, printer.Options{}, os.Stdout); err != nil {
return errors.Wrap(err, "unable to print result")
}
//
// PUSH CONFIG If pipeline update succeeded
//
if len(configs) > 0 {
cfs := make(map[string]strfmt.Base64)
for fp, dest := range configs {
var c strfmt.Base64
c, err = ioutil.ReadFile(fp)
if err != nil {
return errors.Wrap(err, "unable to read config file")
}
cfs[dest] = c
}
if err := m.PushConfig(org, project, env, cfs); err != nil {
return errors.Wrap(err, "unable to push config")
}
}
return nil
}