This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdeployment_create.go
More file actions
149 lines (124 loc) · 3.42 KB
/
deployment_create.go
File metadata and controls
149 lines (124 loc) · 3.42 KB
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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/urfave/cli/v2"
"github.com/gitploy-io/gitploy/model/extent"
"github.com/gitploy-io/gitploy/pkg/api"
)
var deploymentCreateCommand = &cli.Command{
Name: "create",
Usage: "Deploy a specific ref(branch, SHA, tag) to the environment.",
ArgsUsage: "<owner>/<repo>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "type",
Usage: "The type of the ref: 'commit', 'branch', or 'tag'.",
Required: true,
},
&cli.StringFlag{
Name: "env",
Usage: "The name of the environment.",
Required: true,
},
&cli.StringFlag{
Name: "ref",
Usage: "The specific ref. It can be any named branch, tag, or SHA.",
Required: true,
},
&cli.StringSliceFlag{
Name: "field",
Aliases: []string{"f"},
Usage: "The pair of key and value to add to the payload. The format must be <key>=<value>.",
},
},
Action: func(cli *cli.Context) error {
ns, n, err := splitFullName(cli.Args().First())
if err != nil {
return err
}
c := buildClient(cli)
config, err := c.Config.Get(cli.Context, ns, n)
if err != nil {
return err
}
// If the 'dynamic_payload' field is enabled,
// it creates a payload and pass it as a parameter.
var payload map[string]interface{}
if env := config.GetEnv(cli.String("env")); env.IsDynamicPayloadEnabled() {
if payload, err = buildDyanmicPayload(cli.StringSlice("field"), env); err != nil {
return err
}
}
d, err := c.Deployment.Create(cli.Context, ns, n, &api.DeploymentCreateRequest{
Type: cli.String("type"),
Ref: cli.String("ref"),
Env: cli.String("env"),
DynamicPayload: payload,
})
if err != nil {
return err
}
return printJson(cli, d)
},
}
func buildDyanmicPayload(fields []string, env *extent.Env) (map[string]interface{}, error) {
values := make(map[string]string)
for _, f := range fields {
keyAndValue := strings.SplitN(f, "=", 2)
if len(keyAndValue) != 2 {
return nil, fmt.Errorf("The field must be <key>=<value> format")
}
values[keyAndValue[0]] = keyAndValue[1]
}
payload := make(map[string]interface{})
// Build the payload, and use the default value if there is no value.
for key, input := range env.DynamicPayload.Inputs {
val, ok := values[key]
// Set the default value if the value doesn't exist.
if !ok {
if input.Default != nil {
payload[key] = *input.Default
}
continue
}
parsed, err := parseValue(input, val)
if err != nil {
return nil, fmt.Errorf("The value of the '%s' field is not %s", key, input.Type)
}
payload[key] = parsed
}
// Check that the required values are present.
for key, input := range env.DynamicPayload.Inputs {
if !(input.Required != nil && *input.Required) {
continue
}
if _, ok := payload[key]; !ok {
return nil, fmt.Errorf("The value of the '%s' field is required", key)
}
}
return payload, nil
}
func parseValue(input extent.Input, s string) (interface{}, error) {
switch input.Type {
case extent.InputTypeSelect:
return s, nil
case extent.InputTypeString:
return s, nil
case extent.InputTypeNumber:
if val, err := strconv.ParseFloat(s, 64); err != nil {
return nil, err
} else {
return val, nil
}
case extent.InputTypeBoolean:
if val, err := strconv.ParseBool(s); err != nil {
return nil, err
} else {
return val, nil
}
default:
return nil, fmt.Errorf("%s is unsupported type.", input.Type)
}
}