This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcreate.go
More file actions
98 lines (79 loc) · 2.29 KB
/
Copy pathcreate.go
File metadata and controls
98 lines (79 loc) · 2.29 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
package stack
import (
"errors"
"fmt"
"io/ioutil"
"koding/klientctl/commands/cli"
"koding/klientctl/endpoint/kloud"
"koding/klientctl/endpoint/stack"
"github.com/spf13/cobra"
)
type createOptions struct {
team string
title string
file string
creds []string
jsonOutput bool
}
// NewCreateCommand creates a command that can create stacks.
func NewCreateCommand(c *cli.CLI) *cobra.Command {
opts := &createOptions{}
cmd := &cobra.Command{
Use: "create",
Short: "Create a new stack",
RunE: createCommand(c, opts),
}
// Flags.
flags := cmd.Flags()
flags.StringVar(&opts.team, "team", "", "owner of the stack")
flags.StringVar(&opts.title, "title", "", "stack title")
flags.StringVarP(&opts.file, "file", "f", "", "read stack template from a file")
flags.StringSliceVarP(&opts.creds, "credential", "c", nil, "stack credentials")
flags.BoolVar(&opts.jsonOutput, "json", false, "output in JSON format")
// Middlewares.
cli.MultiCobraCmdMiddleware(
cli.DaemonRequired, // Deamon service is required.
cli.NoArgs, // No custom arguments are accepted.
)(c, cmd)
return cmd
}
func createCommand(c *cli.CLI, opts *createOptions) cli.CobraFuncE {
return func(cmd *cobra.Command, args []string) error {
var p []byte
var err error
switch opts.file {
case "":
return errors.New("no template file was provided")
case "-":
p, err = ioutil.ReadAll(c.In())
default:
p, err = ioutil.ReadFile(opts.file)
}
if err != nil {
return errors.New("error reading template file: " + err.Error())
}
fmt.Fprintln(c.Err(), "Creating stack... ")
createOpts := &stack.CreateOptions{
Team: opts.team,
Title: opts.title,
Credentials: opts.creds,
Template: p,
}
resp, err := stack.Create(createOpts)
if err != nil {
return errors.New("error creating stack: " + err.Error())
}
if opts.jsonOutput {
cli.PrintJSON(c.Out(), resp)
return nil
}
fmt.Fprintf(c.Err(), "\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 fmt.Errorf("building %q stack failed: %s", resp.Title, e.Error)
}
fmt.Fprintf(c.Out(), "[%d%%] %s\n", e.Event.Percentage, e.Event.Message)
}
return nil
}
}