-
Notifications
You must be signed in to change notification settings - Fork 9
/
sd-cmd.go
129 lines (111 loc) · 2.81 KB
/
sd-cmd.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
package main
import (
"fmt"
"os"
"os/exec"
"runtime/debug"
"syscall"
"github.com/screwdriver-cd/sd-cmd/config"
"github.com/screwdriver-cd/sd-cmd/executor"
"github.com/screwdriver-cd/sd-cmd/promoter"
"github.com/screwdriver-cd/sd-cmd/publisher"
"github.com/screwdriver-cd/sd-cmd/removeTag"
"github.com/screwdriver-cd/sd-cmd/screwdriver/api"
"github.com/screwdriver-cd/sd-cmd/validator"
)
const (
minArgLength = 2
defaultFailureExitCode = 1
)
func successExit() {
os.Exit(0)
}
// failureExit exits process with 1
func failureExit(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
if exitError, ok := err.(*exec.ExitError); ok {
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
os.Exit(status.ExitStatus())
}
}
}
os.Exit(defaultFailureExitCode)
}
// finalRecover makes one last attempt to recover from a panic.
// This should only happen if the previous recovery caused a panic.
func finalRecover() {
if p := recover(); p != nil {
fmt.Fprintln(os.Stderr, "ERROR: Something terrible has happened. Please file a ticket with this info:")
fmt.Fprintf(os.Stderr, "ERROR: %v\n%v\n", p, string(debug.Stack()))
failureExit(nil)
}
successExit()
}
func init() {
config.LoadConfig()
}
func runExecutor(sdAPI api.API, args []string) (err error) {
exec, err := executor.New(sdAPI, args)
if err != nil {
return
}
defer executor.CleanUp()
err = exec.Run()
return
}
func runPublisher(sdAPI api.API, args []string) error {
pub, err := publisher.New(sdAPI, args)
if err != nil {
return fmt.Errorf("Fail to get publisher: %v", err)
}
return pub.Run()
}
func runPromoter(sdAPI api.API, args []string) error {
pro, err := promoter.New(sdAPI, args)
if err != nil {
return fmt.Errorf("Fail to get promoter: %v", err)
}
return pro.Run()
}
func runValidator(sdAPI api.API, args []string) error {
val, err := validator.New(sdAPI, args)
if err != nil {
return fmt.Errorf("Fail to get validator: %v", err)
}
return val.Run()
}
func runRemoveTag(sdAPI api.API, args []string) error {
val, err := removeTag.New(sdAPI, args)
if err != nil {
return fmt.Errorf("Fail to get removeTag: %v", err)
}
return val.Run()
}
func runCommand(sdAPI api.API, args []string) error {
if len(args) < minArgLength {
return fmt.Errorf("The number of arguments is not enough")
}
switch args[1] {
case "exec":
return runExecutor(sdAPI, args[2:])
case "publish":
return runPublisher(sdAPI, args[2:])
case "promote":
return runPromoter(sdAPI, args[2:])
case "validate":
return runValidator(sdAPI, args[2:])
case "removeTag":
return runRemoveTag(sdAPI, args[2:])
default:
return runExecutor(sdAPI, args[1:])
}
}
func main() {
defer finalRecover()
sdAPI := api.New(config.SDAPIURL, config.SDToken)
err := runCommand(sdAPI, os.Args)
if err != nil {
failureExit(err)
}
}