|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/deis/deis/pkg/prettyprint" |
| 10 | + |
| 11 | + "github.com/deis/deis/client-go/controller/client" |
| 12 | + "github.com/deis/deis/client-go/controller/models/apps" |
| 13 | + "github.com/deis/deis/client-go/controller/models/config" |
| 14 | +) |
| 15 | + |
| 16 | +// AppCreate creates an app. |
| 17 | +func AppCreate(id string, buildpack string, remote string, noRemote bool) error { |
| 18 | + c, err := client.New() |
| 19 | + |
| 20 | + fmt.Print("Creating Application... ") |
| 21 | + quit := progress() |
| 22 | + app, err := apps.New(c, id) |
| 23 | + |
| 24 | + quit <- true |
| 25 | + <-quit |
| 26 | + |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + fmt.Printf("done, created %s\n", app.ID) |
| 32 | + |
| 33 | + if buildpack != "" { |
| 34 | + configValues := map[string]string{ |
| 35 | + "BUILDPACK_URL": buildpack, |
| 36 | + } |
| 37 | + if err = config.Set(c, app.ID, configValues); err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if !noRemote { |
| 43 | + return c.CreateRemote(remote, app.ID) |
| 44 | + } |
| 45 | + |
| 46 | + fmt.Println("remote available at", c.RemoteURL(app.ID)) |
| 47 | + |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +// AppsList lists apps on the Deis controller. |
| 52 | +func AppsList() error { |
| 53 | + c, err := client.New() |
| 54 | + |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + apps, err := apps.List(c) |
| 60 | + |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + fmt.Println("=== Apps") |
| 66 | + |
| 67 | + for _, app := range apps { |
| 68 | + fmt.Println(app.ID) |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// AppInfo prints info about app. |
| 74 | +func AppInfo(appID string) error { |
| 75 | + c, appID, err := load(appID) |
| 76 | + |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + app, err := apps.Get(c, appID) |
| 82 | + |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + fmt.Printf("=== %s Application\n", app.ID) |
| 88 | + fmt.Println("updated: ", app.Updated) |
| 89 | + fmt.Println("uuid: ", app.UUID) |
| 90 | + fmt.Println("created: ", app.Created) |
| 91 | + fmt.Println("url: ", app.URL) |
| 92 | + fmt.Println("owner: ", app.Owner) |
| 93 | + fmt.Println("id: ", app.ID) |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +// AppOpen opens an app in the default webbrowser. |
| 99 | +func AppOpen(appID string) error { |
| 100 | + c, appID, err := load(appID) |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + app, err := apps.Get(c, appID) |
| 107 | + |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + u, err := url.Parse(app.URL) |
| 113 | + |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + u.Scheme = "http" |
| 119 | + |
| 120 | + return client.Webbrowser(u.String()) |
| 121 | +} |
| 122 | + |
| 123 | +// AppLogs returns the logs from an app. |
| 124 | +func AppLogs(appID string, lines int) error { |
| 125 | + c, appID, err := load(appID) |
| 126 | + |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + logs, err := apps.Logs(c, appID, lines) |
| 132 | + |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + for _, log := range strings.Split(strings.Trim(logs, `\n`), `\n`) { |
| 138 | + catagory := strings.Split(strings.Split(log, ": ")[0], " ")[1] |
| 139 | + colorVars := map[string]string{ |
| 140 | + "Color": chooseColor(catagory), |
| 141 | + "Log": log, |
| 142 | + } |
| 143 | + fmt.Println(prettyprint.ColorizeVars("{{.V.Color}}{{.V.Log}}{{.C.Default}}", colorVars)) |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +// AppRun runs a one time command in the app. |
| 150 | +func AppRun(appID, command string) error { |
| 151 | + c, appID, err := load(appID) |
| 152 | + |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + fmt.Printf("Running '%s'...\n", command) |
| 158 | + |
| 159 | + out, err := apps.Run(c, appID, command) |
| 160 | + |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + fmt.Print(out.Output) |
| 166 | + os.Exit(out.ReturnCode) |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +// AppDestroy destroys an app. |
| 171 | +func AppDestroy(appID, confirm string) error { |
| 172 | + gitSession := false |
| 173 | + |
| 174 | + c, err := client.New() |
| 175 | + |
| 176 | + if err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + |
| 180 | + if appID == "" { |
| 181 | + appID, err = c.DetectApp() |
| 182 | + |
| 183 | + if err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + |
| 187 | + gitSession = true |
| 188 | + } |
| 189 | + |
| 190 | + if confirm == "" { |
| 191 | + fmt.Printf(` ! WARNING: Potentially Destructive Action |
| 192 | + ! This command will destroy the application: %s |
| 193 | + ! To proceed, type "%s" or re-run this command with --confirm=%s |
| 194 | +
|
| 195 | +>`, appID, appID, appID) |
| 196 | + |
| 197 | + fmt.Scanln(&confirm) |
| 198 | + } |
| 199 | + |
| 200 | + if confirm != appID { |
| 201 | + return fmt.Errorf("App %s does not match confirm %s, aborting.", appID, confirm) |
| 202 | + } |
| 203 | + |
| 204 | + fmt.Printf("Destroying %s...", appID) |
| 205 | + |
| 206 | + if err = apps.Delete(c, appID); err != nil { |
| 207 | + return err |
| 208 | + } |
| 209 | + |
| 210 | + if gitSession { |
| 211 | + return c.DeleteRemote(appID) |
| 212 | + } |
| 213 | + |
| 214 | + return nil |
| 215 | +} |
0 commit comments