/
projectvariablecreate.go
151 lines (130 loc) · 4.23 KB
/
projectvariablecreate.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"io/ioutil"
"os"
config "agola.io/agola/internal/config"
gwapitypes "agola.io/agola/services/gateway/api/types"
gwclient "agola.io/agola/services/gateway/client"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
errors "golang.org/x/xerrors"
)
var cmdProjectVariableCreate = &cobra.Command{
Use: "create",
Short: "create a project variable",
Long: `create a project variable
The variable values should be provided by a yaml document. Examples:
- secret_name: secret01
secret_var: var01
when:
branch: master
tag:
- v1.x
- v2.x
- secret_name: secret02
secret_var: data02
when:
ref:
include:
- '#/refs/pull/.*#'
- '#/refs/heads/devel.*#'
exclude: /refs/heads/develop
The above yaml document defines a variable that can have two different values depending on the first matching condition.
`,
Run: func(cmd *cobra.Command, args []string) {
if err := variableCreate(cmd, "project", args); err != nil {
log.Fatalf("err: %v", err)
}
},
}
type variableCreateOptions struct {
parentRef string
name string
file string
}
var variableCreateOpts variableCreateOptions
func init() {
flags := cmdProjectVariableCreate.Flags()
flags.StringVar(&variableCreateOpts.parentRef, "project", "", "project id or full path")
flags.StringVarP(&variableCreateOpts.name, "name", "n", "", "variable name")
flags.StringVarP(&variableCreateOpts.file, "file", "f", "", `yaml file containing the variable definition (use "-" to read from stdin)`)
if err := cmdProjectVariableCreate.MarkFlagRequired("project"); err != nil {
log.Fatal(err)
}
if err := cmdProjectVariableCreate.MarkFlagRequired("name"); err != nil {
log.Fatal(err)
}
if err := cmdProjectVariableCreate.MarkFlagRequired("file"); err != nil {
log.Fatal(err)
}
cmdProjectVariable.AddCommand(cmdProjectVariableCreate)
}
type VariableValue struct {
SecretName string `json:"secret_name,omitempty"`
SecretVar string `json:"secret_var,omitempty"`
When *config.When `json:"when,omitempty"`
}
func variableCreate(cmd *cobra.Command, ownertype string, args []string) error {
gwclient := gwclient.NewClient(gatewayURL, token)
// "github.com/ghodss/yaml" doesn't provide a streaming decoder
var data []byte
var err error
if variableCreateOpts.file == "-" {
data, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
} else {
data, err = ioutil.ReadFile(variableCreateOpts.file)
if err != nil {
return err
}
}
var values []VariableValue
if err := yaml.Unmarshal(data, &values); err != nil {
log.Fatalf("failed to unmarshal values: %v", err)
}
rvalues := []gwapitypes.VariableValueRequest{}
for _, value := range values {
rvalues = append(rvalues, gwapitypes.VariableValueRequest{
SecretName: value.SecretName,
SecretVar: value.SecretVar,
When: value.When.ToWhen(),
})
}
req := &gwapitypes.CreateVariableRequest{
Name: variableCreateOpts.name,
Values: rvalues,
}
switch ownertype {
case "project":
log.Infof("creating project variable")
variable, _, err := gwclient.CreateProjectVariable(context.TODO(), variableCreateOpts.parentRef, req)
if err != nil {
return errors.Errorf("failed to create project variable: %w", err)
}
log.Infof("project variable %q created, ID: %q", variable.Name, variable.ID)
case "projectgroup":
log.Infof("creating project group variable")
variable, _, err := gwclient.CreateProjectGroupVariable(context.TODO(), variableCreateOpts.parentRef, req)
if err != nil {
return errors.Errorf("failed to create project group variable: %w", err)
}
log.Infof("project group variable %q created, ID: %q", variable.Name, variable.ID)
}
return nil
}