This repository has been archived by the owner on Feb 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
main.go
129 lines (116 loc) · 2.88 KB
/
main.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"
"io/ioutil"
"net/http"
"os"
)
// The following vars will be injected during the build process.
var (
Version string
GitSHA string
)
const (
hardcodedWarning = `
WARNING: The 'boot2docker' command line interface is officially deprecated.
Please switch to Docker Machine (https://docs.docker.com/machine/) ASAP.
Docker Toolbox (https://docker.com/toolbox) is the recommended install method.
`
warningURL = "https://raw.githubusercontent.com/boot2docker/boot2docker-cli/master/DEPRECATION_WARNING"
)
type unknownCommandError struct {
cmd string
}
func (e unknownCommandError) Error() string {
return fmt.Sprintf("Unknown command: %s", e.cmd)
}
func main() {
// os.Exit will terminate the program at the place of call without running
// any deferred cleanup statements. It might cause unintended effects. To
// be safe, we wrap the program in run() and only os.Exit() outside the
// wrapper. Be careful not to indirectly trigger os.Exit() in the program,
// notably via log.Fatal() and on flag.Parse() where the default behavior
// is ExitOnError.
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error in run: %v\n", err)
if _, ok := err.(unknownCommandError); ok {
usageShort()
}
os.Exit(1)
}
}
// Run the program and return exit code.
func run() error {
flags, err := config()
if err != nil {
return fmt.Errorf("config error: %v\n", err)
}
switch cmd := flags.Arg(0); cmd {
case "download":
return cmdDownload()
case "config", "cfg":
return cmdConfig()
case "init":
printDeprecationWarning()
return cmdInit()
case "up", "start", "boot", "resume":
printDeprecationWarning()
return cmdUp()
case "save", "suspend":
return cmdSave()
case "down", "halt", "stop":
return cmdStop()
case "poweroff":
return cmdPoweroff()
case "restart":
return cmdRestart()
case "reset":
return cmdReset()
case "delete", "destroy":
return cmdDelete()
case "info":
return cmdInfo()
case "shellinit", "socket":
return cmdShellInit()
case "status":
return cmdStatus()
case "ssh":
return cmdSSH()
case "ip":
return cmdIP()
case "upgrade":
return cmdUpgrade()
case "version":
// Version is now printed by the call to config()
return nil
case "help":
flags.Usage()
return nil
case "":
usageShort()
return nil
default:
return unknownCommandError{cmd: cmd}
}
}
func printDeprecationWarning() {
var (
warning string
)
// Try to get the warning from the Github raw URL. If there's any
// failure along the way, e.g. network, just fall back to the default
// warning hardcoded in the source.
resp, err := http.Get(warningURL)
if err != nil || resp.StatusCode != http.StatusOK {
warning = hardcodedWarning
} else {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
warning = hardcodedWarning
} else {
warning = string(body)
}
}
fmt.Fprintln(os.Stderr, warning)
}