forked from projectatomic/atomic-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
117 lines (96 loc) · 3.38 KB
/
process.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
package cmd
import (
"errors"
"io"
"strings"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/template"
"github.com/openshift/origin/pkg/template/api"
)
// injectUserVars injects user specified variables into the Template
func injectUserVars(cmd *cobra.Command, t *api.Template) {
values := util.StringList{}
values.Set(cmdutil.GetFlagString(cmd, "value"))
for _, keypair := range values {
p := strings.SplitN(keypair, "=", 2)
if len(p) != 2 {
glog.Errorf("Invalid parameter assignment '%s'", keypair)
continue
}
if v := template.GetParameterByName(t, p[0]); v != nil {
v.Value = p[1]
v.Generate = ""
template.AddParameter(t, *v)
} else {
glog.Errorf("Unknown parameter name '%s'", p[0])
}
}
}
// NewCmdProcess returns a 'process' command
func NewCmdProcess(f *clientcmd.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "process -f filename",
Short: "Process template into list of resources",
Long: `Process template into a list of resources specified in filename or stdin
JSON and YAML formats are accepted.
Examples:
# Convert template.json into resource list
$ osc process -f template.json
# Convert template.json into resource list
$ cat template.json | osc process -f -
`,
Run: func(cmd *cobra.Command, args []string) {
filename := cmdutil.GetFlagString(cmd, "filename")
if len(filename) == 0 {
usageError(cmd, "Must pass a filename to update")
}
schema, err := f.Validator(cmd)
checkErr(err)
cfg, err := f.ClientConfig(cmd)
checkErr(err)
namespace, err := f.DefaultNamespace(cmd)
checkErr(err)
mapper, typer := f.Object(cmd)
mapping, _, _, data := cmdutil.ResourceFromFile(filename, typer, mapper, schema, cfg.Version)
obj, err := mapping.Codec.Decode(data)
checkErr(err)
templateObj, ok := obj.(*api.Template)
if !ok {
checkErr(errors.New("Unable to the convert input to the Template"))
}
client, _, err := f.Clients(cmd)
checkErr(err)
if cmd.Flag("value").Changed {
injectUserVars(cmd, templateObj)
}
printer, err := f.Factory.PrinterForMapping(cmd, mapping)
checkErr(err)
// If 'parameters' flag is set it does not do processing but only print
// the template parameters to console for inspection.
if cmdutil.GetFlagBool(cmd, "parameters") == true {
err = printer.PrintObj(templateObj, out)
checkErr(err)
return
}
result, err := client.TemplateConfigs(namespace).Create(templateObj)
checkErr(err)
// We need to override the default output format to JSON so we can return
// processed template. Users might still be able to change the output
// format using the 'output' flag.
if !cmd.Flag("output").Changed {
cmd.Flags().Set("output", "json")
printer, _ = f.PrinterForMapping(cmd, mapping)
}
printer.PrintObj(result, out)
},
}
cmdutil.AddPrinterFlags(cmd)
cmd.Flags().StringP("filename", "f", "", "Filename or URL to file to use to update the resource")
cmd.Flags().StringP("value", "v", "", "Specify a list of key-value pairs (eg. -v FOO=BAR,BAR=FOO) to set/override parameter values")
cmd.Flags().BoolP("parameters", "", false, "Do not process but only print available parameters")
return cmd
}