-
Notifications
You must be signed in to change notification settings - Fork 23
/
release_update.go
103 lines (85 loc) · 3.06 KB
/
release_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
package cmd
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func (r *runners) InitReleaseUpdate(parent *cobra.Command) {
cmd := &cobra.Command{
Use: "update SEQUENCE",
Short: "Updated a release's yaml config",
Long: "Updated a release's yaml config",
}
parent.AddCommand(cmd)
cmd.Flags().StringVar(&r.args.updateReleaseYaml, "yaml", "", "The new YAML config for this release. Use '-' to read from stdin. Cannot be used with the --yaml-file flag.")
cmd.Flags().StringVar(&r.args.updateReleaseYamlFile, "yaml-file", "", "The file name with YAML config for this release. Cannot be used with the --yaml flag.")
cmd.Flags().StringVar(&r.args.updateReleaseYamlDir, "yaml-dir", "", "The directory containing multiple yamls for a Kots release. Cannot be used with the --yaml flag.")
cmd.Flags().StringVar(&r.args.updateReleaseChart, "chart", "", "Helm chart to create the release from. Cannot be used with the --yaml, --yaml-file, or --yaml-dir flags.")
cmd.RunE = r.releaseUpdate
}
func (r *runners) releaseUpdate(cmd *cobra.Command, args []string) error {
specSources := []string{
r.args.updateReleaseYaml,
r.args.updateReleaseYamlFile,
r.args.updateReleaseYamlDir,
r.args.updateReleaseChart,
}
numSources := 0
for _, specSource := range specSources {
if specSource != "" {
numSources++
}
}
if numSources == 0 {
return errors.New("one of --yaml, --yaml-file, --yaml-dir, or --chart is required")
}
if numSources > 1 {
return errors.New("only one of --yaml, --yaml-file, --yaml-dir, or --chart may be specified")
}
if (strings.HasSuffix(r.args.updateReleaseYaml, ".yaml") || strings.HasSuffix(r.args.updateReleaseYaml, ".yml")) &&
len(strings.Split(r.args.updateReleaseYaml, " ")) == 1 {
return errors.New("use the --yaml-file flag when passing a yaml filename")
}
if r.args.updateReleaseYaml == "-" {
bytes, err := ioutil.ReadAll(r.stdin)
if err != nil {
return err
}
r.args.updateReleaseYaml = string(bytes)
}
if r.args.updateReleaseYamlFile != "" {
bytes, err := ioutil.ReadFile(r.args.updateReleaseYamlFile)
if err != nil {
return err
}
r.args.updateReleaseYaml = string(bytes)
}
if len(args) < 1 {
return errors.New("release sequence is required")
}
seq, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return errors.Errorf("invalid release sequence: %s", args[0])
}
if r.args.updateReleaseYamlDir != "" {
r.args.updateReleaseYaml, err = makeReleaseFromDir(r.args.updateReleaseYamlDir)
if err != nil {
return errors.Wrap(err, "make release from dir")
}
} else if r.args.updateReleaseChart != "" {
r.args.updateReleaseYaml, err = makeReleaseFromChart(r.args.updateReleaseChart)
if err != nil {
return errors.Wrap(err, "make release from chart")
}
}
if err := r.api.UpdateRelease(r.appID, r.appType, seq, r.args.updateReleaseYaml); err != nil {
return errors.Wrap(err, "failure setting new yaml config for release")
}
// ignore the error since operation was successful
fmt.Fprintf(r.w, "Release %d updated\n", seq)
r.w.Flush()
return nil
}