/
main.go
161 lines (136 loc) · 4.41 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Command helm allows for faster development using Docker and Docker Compose
// on Mac OS X.
package main
import (
"fmt"
"os"
"github.com/Sitback/helm/host"
log "github.com/Sirupsen/logrus"
"github.com/docker/libcompose/docker"
"github.com/docker/libcompose/project"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
Name string
Version string
Build string
)
func main() {
var err error
pp, err := docker.NewProject(&docker.Context{
Context: project.Context{
ComposeFile: "docker-compose.yml",
ProjectName: "helm",
},
})
if err != nil {
log.Fatal(err)
}
pp.Parse()
// pp.Up()
pwd, err := os.Getwd()
if err != nil {
fmt.Println("Error: could not determine your current directory:", err.Error())
os.Exit(1)
}
var (
verbose = kingpin.
Flag("verbose", "Verbose mode.").
Short('v').
Bool()
)
// Clean up help.
kingpin.UsageTemplate(kingpin.CompactUsageTemplate)
kingpin.CommandLine.Help = "Fast, Docker Compose-based development for Mac OS X."
kingpin.CommandLine.HelpFlag.Short('h')
// Setup version printing.
kingpin.Flag("version", "Show version.").PreAction(kingpin.Action(func(*kingpin.ParseContext) error {
fmt.Printf("%v version %v, build %v\n", Name, Version, Build)
os.Exit(0)
return nil
})).Bool()
up := kingpin.Command("up", "Start up services defined in a Docker Compose file.")
upFile := up.Flag("file", "Docker Compose file to use").Short('f').Default("docker-compose.yml").String()
upDetached := up.Flag("detached", "Specify a Detached mode: Run containers in the background.").Short('d').Bool()
down := kingpin.Command("down", "Stop any running services.")
downFile := down.Flag("file", "Specify a Docker Compose file to use").Short('f').Default("docker-compose.yml").String()
cmdHost := kingpin.Command("host", "Control and configure the Helm Docker Machine.")
hostInit := cmdHost.Command("init", "Initialise the Helm Docker Machine for use.")
hostInitForce := hostInit.Flag("force", "Force re-initialisation").Short('f').Bool()
hostStart := cmdHost.Command("start", "Start the Helm Docker Machine.").Alias("up")
hostStop := cmdHost.Command("stop", "Stop the Helm Docker Machine.").Alias("down")
hostRestart := cmdHost.Command("restart", "Restart the Helm Docker Machine.")
hostDestroy := cmdHost.Command("destroy", "Stop and remove the Helm Docker Machine.")
hostStatus := cmdHost.Command("status", "Show status of the Helm Docker Machine.")
switch kingpin.Parse() {
case up.FullCommand():
log.Info("Up!")
log.Info("File:", *upFile)
if *upDetached {
log.Info("Detaching...")
}
case down.FullCommand():
log.Info("Down!")
log.Info("File:", *downFile)
case hostInit.FullCommand():
_, err := host.NewHost(true, *hostInitForce)
if err != nil {
log.Fatal("The Helm host already exists, run `helm host destroy` first if you wish to recreate it. Alternatively, run init with the force flag: `helm host init --force`.")
}
log.Info("host init!")
case hostStart.FullCommand():
helmHost, err := host.NewHost(false, false)
if err != nil {
log.Fatal("Could not start host, it might not exist. Try running `helm host init`.")
}
err = helmHost.Start()
if err != nil {
log.Fatal(err)
}
log.Info("host start!")
case hostStop.FullCommand():
helmHost, err := host.NewHost(false, false)
if err != nil {
log.Fatal("Could not stop host, it might not exist. Try running `helm host init`.")
}
err = helmHost.Stop()
if err != nil {
log.Fatal(err)
}
log.Info("host stop!")
case hostRestart.FullCommand():
helmHost, err := host.NewHost(false, false)
if err != nil {
log.Fatal("Could not restart host, it might not exist. Try running `helm host init`.")
}
err = helmHost.Restart()
if err != nil {
log.Fatal(err)
}
log.Info("host restart!")
case hostDestroy.FullCommand():
helmHost, err := host.NewHost(false, false)
if err != nil {
log.Fatal("Could not destroy host, it might not exist. Try running `helm host init`.")
}
err = helmHost.Destroy()
if err != nil {
log.Fatal(err)
}
log.Info("host destory!")
case hostStatus.FullCommand():
helmHost, err := host.NewHost(false, false)
if err != nil {
log.Fatal("Host doesn't exist. Try running `helm host init`.")
}
status, err := helmHost.Host.Driver.GetState()
if err != nil {
log.Fatal(err)
}
log.Infof("Host status: %v", status.String())
}
if *verbose {
log.Info("Hello, this is verbose mode.")
log.Info("The current directory is:", pwd)
}
}