forked from screwdriver-cd/sd-cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd-cmd.go
90 lines (76 loc) · 1.72 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
package main
import (
"fmt"
"os"
"runtime/debug"
"github.com/screwdriver-cd/sd-cmd/config"
"github.com/screwdriver-cd/sd-cmd/executor"
"github.com/screwdriver-cd/sd-cmd/logger"
"github.com/screwdriver-cd/sd-cmd/screwdriver/api"
)
const minArgLength = 2
func cleanExit() {
logger.CloseAll()
}
func successExit() {
cleanExit()
os.Exit(0)
}
// failureExit exits process with 1
func failureExit(err error) {
cleanExit()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
}
os.Exit(1)
}
// 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) error {
exec, err := executor.New(sdAPI, args)
if err != nil {
return err
}
err = exec.Run()
if err != nil {
return err
}
return nil
}
func runCommand(sdAPI api.API, args []string) error {
if len(os.Args) < minArgLength {
return fmt.Errorf("The number of arguments is not enough")
}
switch args[1] {
case "exec":
return runExecutor(sdAPI, args)
case "publish":
return fmt.Errorf("publish is not implemented yet")
case "promote":
return fmt.Errorf("promote is not implemented yet")
default:
return runExecutor(sdAPI, args)
}
}
func main() {
defer finalRecover()
sdAPI, err := api.New(config.SDAPIURL, config.SDToken)
if err != nil {
failureExit(err)
}
err = runCommand(sdAPI, os.Args)
if err != nil {
failureExit(err)
}
}