Repository navigation
Expand file tree
/
Copy pathsetup.go
More file actions
165 lines (140 loc) · 2.96 KB
/
Copy pathsetup.go
File metadata and controls
165 lines (140 loc) · 2.96 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
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
162
163
164
165
// Package setup provides up.json initialization.
package setup
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"github.com/mitchellh/go-homedir"
"github.com/tj/go/term"
"github.com/tj/survey"
"github.com/apex/up/internal/util"
"github.com/apex/up/internal/validate"
"github.com/apex/up/platform/aws/regions"
)
// ErrNoCredentials is the error returned when no AWS credential profiles are available.
var ErrNoCredentials = errors.New("no credentials")
// config saved to up.json
type config struct {
Name string `json:"name"`
Profile string `json:"profile"`
Regions []string `json:"regions"`
}
// questions for the user.
var questions = []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{
Message: "Project name:",
Default: defaultName(),
},
Validate: validateName,
},
{
Name: "profile",
Prompt: &survey.Select{
Message: "AWS profile:",
Options: awsProfiles(),
Default: os.Getenv("AWS_PROFILE"),
PageSize: 10,
},
Validate: survey.Required,
},
{
Name: "region",
Prompt: &survey.Select{
Message: "AWS region:",
Options: regions.Names,
Default: defaultRegion(),
PageSize: 15,
},
Validate: survey.Required,
},
}
// Create an up.json file for the user.
func Create() error {
var in struct {
Name string `json:"name"`
Profile string `json:"profile"`
Region string `json:"region"`
}
if len(awsProfiles()) == 0 {
return ErrNoCredentials
}
println()
// confirm
var ok bool
err := survey.AskOne(&survey.Confirm{
Message: fmt.Sprintf("No up.json found, create a new project?"),
Default: true,
}, &ok, nil)
if err != nil {
return err
}
if !ok {
return errors.New("aborted")
}
// prompt
term.MoveUp(1)
term.ClearLine()
if err := survey.Ask(questions, &in); err != nil {
return err
}
c := config{
Name: in.Name,
Profile: in.Profile,
Regions: []string{
regions.GetIdByName(in.Region),
},
}
b, _ := json.MarshalIndent(c, "", " ")
return ioutil.WriteFile("up.json", b, 0755)
}
// defaultName returns the default app name.
// The name is only inferred if it is valid.
func defaultName() string {
dir, _ := os.Getwd()
name := filepath.Base(dir)
if validate.Name(name) != nil {
return ""
}
return name
}
// defaultRegion returns the default aws region.
func defaultRegion() string {
if s := os.Getenv("AWS_DEFAULT_REGION"); s != "" {
return s
}
if s := os.Getenv("AWS_REGION"); s != "" {
return s
}
return ""
}
// validateName validates the name prompt.
func validateName(v interface{}) error {
if err := validate.Name(v.(string)); err != nil {
return err
}
return survey.Required(v)
}
// awsProfiles returns the AWS profiles found.
func awsProfiles() []string {
path, err := homedir.Expand("~/.aws/credentials")
if err != nil {
return nil
}
f, err := os.Open(path)
if err != nil {
return nil
}
defer f.Close()
s, err := util.ParseSections(f)
if err != nil {
return nil
}
sort.Strings(s)
return s
}