This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathstack.go
More file actions
124 lines (97 loc) · 2.64 KB
/
stack.go
File metadata and controls
124 lines (97 loc) · 2.64 KB
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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"text/tabwriter"
"koding/klientctl/endpoint/kloud"
"koding/klientctl/endpoint/remoteapi"
"koding/klientctl/endpoint/stack"
"koding/klientctl/endpoint/team"
"koding/remoteapi/models"
"github.com/codegangsta/cli"
"github.com/koding/logging"
)
func StackCreate(c *cli.Context, log logging.Logger, _ string) (int, error) {
var p []byte
var err error
switch file := c.String("file"); file {
case "":
return 1, errors.New("no template file was provided")
case "-":
p, err = ioutil.ReadAll(os.Stdin)
default:
p, err = ioutil.ReadFile(file)
}
if err != nil {
return 1, errors.New("error reading template file: " + err.Error())
}
fmt.Fprintln(os.Stderr, "Creating stack... ")
opts := &stack.CreateOptions{
Team: c.String("team"),
Title: c.String("title"),
Credentials: c.StringSlice("credential"),
Template: p,
}
resp, err := stack.Create(opts)
if err != nil {
return 1, errors.New("error creating stack: " + err.Error())
}
if c.Bool("json") {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "\t")
enc.Encode(resp)
return 0, nil
}
fmt.Fprintf(os.Stderr, "\nCreatad %q stack with %s ID.\nWaiting for the stack to finish building...\n\n", resp.Title, resp.StackID)
for e := range kloud.Wait(resp.EventID) {
if e.Error != nil {
return 1, fmt.Errorf("\nBuilding %q stack failed:\n%s\n", resp.Title, e.Error)
}
fmt.Printf("[%d%%] %s\n", e.Event.Percentage, e.Event.Message)
}
return 0, nil
}
func StackList(c *cli.Context, log logging.Logger, _ string) (int, error) {
f := &remoteapi.Filter{
Team: c.String("team"),
}
if f.Team == "" {
f.Team = team.Used().Name
}
stacks, err := remoteapi.ListStacks(f)
if err != nil {
return 1, err
}
if c.Bool("json") {
enc := json.NewEncoder(os.Stdout)
enc.SetEscapeHTML(false)
enc.SetIndent("", "\t")
enc.Encode(stacks)
return 0, nil
}
printStacks(stacks)
return 0, nil
}
func printStacks(stacks []*models.JComputeStack) {
w := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
defer w.Flush()
fmt.Fprintln(w, "ID\tTITLE\tOWNER\tTEAM\tSTATE\tREVISION")
for _, stack := range stacks {
owner := *stack.OriginID
if owner != "" {
if account, err := remoteapi.Account(&models.JAccount{ID: owner}); err == nil && account != nil && account.Profile != nil {
owner = account.Profile.Nickname
}
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n", stack.ID, str(stack.Title), owner, str(stack.Group), state(stack.Status), stack.StackRevision)
}
}
func state(status *models.JComputeStackStatus) string {
if status == nil {
return "-"
}
return status.State
}