-
Notifications
You must be signed in to change notification settings - Fork 1
/
standalone.go
127 lines (104 loc) · 3.6 KB
/
standalone.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
// Standalone is a package that defines behaviour which relies on a single top
// level flag, and not nested flags or sub commands.
package standalone
import (
"fmt"
"strconv"
"github.com/integralist/go-fastly-cli/common"
"github.com/sethvargo/go-fastly/fastly"
)
// ActivateVersion activates the specified Fastly service version
func ActivateVersion(version, service string, client *fastly.Client) {
v, err := strconv.Atoi(version)
if err != nil {
fmt.Println(err)
common.Failure()
}
_, err = client.ActivateVersion(&fastly.ActivateVersionInput{
Service: service,
Version: v,
})
if err != nil {
fmt.Printf("\nThere was a problem activating version %s\n\n%s", common.Yellow(version), common.Red(err))
common.Failure()
}
fmt.Printf("\nService '%s' now has version '%s' activated\n\n", common.Yellow(service), common.Green(version))
}
// ValidateVersion validates the specified Fastly service version
func ValidateVersion(version, service string, client *fastly.Client) {
v, err := strconv.Atoi(version)
if err != nil {
fmt.Println(err)
common.Failure()
}
valid, msg, err := client.ValidateVersion(&fastly.ValidateVersionInput{
Service: service,
Version: v,
})
if err != nil {
fmt.Printf("\nThere was a problem validating version %s\n\n%s", common.Yellow(version), common.Red(err))
common.Failure()
}
var validColour, details string
validColour = common.Green(valid)
if valid == false {
validColour = common.Red(valid)
details = common.Red(msg)
}
fmt.Printf("\nService '%s' valid? %s\n\n%s", common.Yellow(service), validColour, details)
}
// PrintLatestSettings sends the sepecified service version settings to stdout
func PrintLatestSettings(serviceID string, client *fastly.Client) {
latestVersion, err := common.GetLatestVCLVersion(serviceID, client)
if err != nil {
fmt.Println(err)
common.Failure()
}
PrintSettingsFor(serviceID, latestVersion, client)
}
// PrintSettingsFor sends the sepecified service version settings to stdout
func PrintSettingsFor(serviceID string, serviceVersion int, client *fastly.Client) {
settings, err := client.GetSettings(&fastly.GetSettingsInput{
Service: serviceID,
Version: serviceVersion,
})
if err != nil {
fmt.Printf("\nThere was a problem getting the settings for version %s\n\n%s", common.Yellow(serviceVersion), common.Red(err))
common.Failure()
}
fmt.Printf(
"\nDefault Host: %s\nDefault TTL: %d (seconds)\n\n",
settings.DefaultHost,
settings.DefaultTTL,
)
}
// GetLatestServiceVersionStatus returns the latest Fastly service version and its status
func GetLatestServiceVersionStatus(serviceID string, client *fastly.Client) (string, error) {
latestVersion, err := common.GetLatestVCLVersion(serviceID, client)
if err != nil {
return "", err
}
status, err := GetStatusForVersion(serviceID, latestVersion, client)
if err != nil {
return "", err
}
return status, nil
}
// GetStatusForVersion returns the status of the specified Fastly service version
func GetStatusForVersion(serviceID string, statusVersion int, client *fastly.Client) (string, error) {
versionStatus, err := client.GetVersion(&fastly.GetVersionInput{
Service: serviceID,
Version: statusVersion,
})
if err != nil {
msg := "\nThere was a problem getting the status for version %s\n\n%s\n\n"
return "", fmt.Errorf(msg, common.Yellow(statusVersion), common.Red(err))
}
activated := common.Green("not activated")
if versionStatus.Active {
activated = common.Red("already activated")
}
msg := "\nService '%s' version '%s' is '%s'\n\n"
status := fmt.Sprintf(msg, common.Yellow(serviceID), common.Yellow(statusVersion), activated)
return status, nil
}