forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
92 lines (74 loc) · 1.76 KB
/
app.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
package main
import (
"fmt"
"log"
"os/exec"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-docopt"
"github.com/flynn/flynn/controller/client"
ct "github.com/flynn/flynn/controller/types"
)
func init() {
register("create", runCreate, `
usage: flynn create [<name>]
Create an application in Flynn.
`)
register("delete", runDelete, `
usage: flynn delete
Delete Flynn app.
`)
register("apps", runApps, `
usage: flynn apps
List flynn apps.
`)
}
func runCreate(args *docopt.Args, client *controller.Client) error {
app := &ct.App{}
app.Name = args.String["<name>"]
if err := client.CreateApp(app); err != nil {
return err
}
exec.Command("git", "remote", "remove", "flynn").Run()
exec.Command("git", "remote", "add", "flynn", gitURLPre(clusterConf.GitHost)+app.Name+gitURLSuf).Run()
log.Printf("Created %s", app.Name)
return nil
}
func runDelete(args *docopt.Args, client *controller.Client) error {
appName := mustApp()
fmt.Printf("Are you sure you want to delete the app %q? (yes/no): ", appName)
loop:
for {
var answer string
fmt.Scanln(&answer)
switch answer {
case "y", "yes":
break loop
case "n", "no":
return nil
default:
fmt.Print("Please type 'yes' or 'no': ")
}
}
if err := client.DeleteApp(appName); err != nil {
return err
}
if remotes, err := gitRemotes(); err == nil {
if app, ok := remotes["flynn"]; ok && app.Name == appName {
exec.Command("git", "remote", "remove", "flynn").Run()
}
}
log.Printf("Deleted %s", appName)
return nil
}
func runApps(args *docopt.Args, client *controller.Client) error {
apps, err := client.AppList()
if err != nil {
return err
}
w := tabWriter()
defer w.Flush()
listRec(w, "ID", "NAME")
for _, a := range apps {
listRec(w, a.ID, a.Name)
}
return nil
}