Skip to content

Commit cf06763

Browse files
feat: Add APPPACK_ACCOUNT env variable (#69)
1 parent b1c030b commit cf06763

9 files changed

Lines changed: 57 additions & 31 deletions

File tree

cmd/access.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/apppackio/apppack/stacks"
2525
"github.com/apppackio/apppack/stringslice"
2626
"github.com/apppackio/apppack/ui"
27+
"github.com/apppackio/apppack/utils"
2728
"github.com/aws/aws-sdk-go/aws"
2829
"github.com/aws/aws-sdk-go/aws/session"
2930
"github.com/spf13/cobra"
@@ -107,7 +108,7 @@ var accessCmd = &cobra.Command{
107108
Short: "list users with access to the app",
108109
Args: cobra.NoArgs,
109110
DisableFlagsInUseLine: true,
110-
Run: func(cmd *cobra.Command, args []string) {
111+
Run: func(_ *cobra.Command, _ []string) {
111112
ui.StartSpinner()
112113
var err error
113114
sess, err := adminSession(SessionDurationSeconds)
@@ -131,7 +132,7 @@ Updates the application Cloudformation stack to add access for the user.`,
131132
DisableFlagsInUseLine: true,
132133
Args: cobra.MinimumNArgs(1),
133134
Example: "apppack -a my-app access add user1@example.com user2@example.com",
134-
Run: func(cmd *cobra.Command, args []string) {
135+
Run: func(_ *cobra.Command, args []string) {
135136
for _, email := range args {
136137
if !validateEmail(email) {
137138
checkErr(fmt.Errorf("%s does not appear to be a valid email address", email))
@@ -163,7 +164,7 @@ Updates the application Cloudformation stack to remove access for the user.`,
163164
DisableFlagsInUseLine: true,
164165
Args: cobra.MinimumNArgs(1),
165166
Example: "apppack -a my-app access remove user1@example.com user2@example.com",
166-
Run: func(cmd *cobra.Command, args []string) {
167+
Run: func(_ *cobra.Command, args []string) {
167168
for _, email := range args {
168169
if !validateEmail(email) {
169170
checkErr(fmt.Errorf("%s does not appear to be a valid email address", email))
@@ -195,7 +196,7 @@ func init() {
195196
"account",
196197
"c",
197198
"",
198-
"AWS account ID or alias (not needed if you are only the administrator of one account)",
199+
utils.AccountFlagHelpText,
199200
)
200201
accessCmd.PersistentFlags().BoolVar(
201202
&UseAWSCredentials,

cmd/admins.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/apppackio/apppack/stacks"
2323
"github.com/apppackio/apppack/stringslice"
2424
"github.com/apppackio/apppack/ui"
25+
"github.com/apppackio/apppack/utils"
2526
"github.com/aws/aws-sdk-go/aws/session"
2627
"github.com/spf13/cobra"
2728
)
@@ -57,7 +58,7 @@ var adminsCmd = &cobra.Command{
5758
Long: "*Requires admin permissions.*",
5859
Args: cobra.NoArgs,
5960
DisableFlagsInUseLine: true,
60-
Run: func(cmd *cobra.Command, args []string) {
61+
Run: func(_ *cobra.Command, _ []string) {
6162
ui.StartSpinner()
6263
sess, err := adminSession(SessionDurationSeconds)
6364
checkErr(err)
@@ -78,7 +79,7 @@ var adminsAddCmd = &cobra.Command{
7879
DisableFlagsInUseLine: true,
7980
Args: cobra.MinimumNArgs(1),
8081
Example: "apppack admins add user1@example.com user2@example.com",
81-
Run: func(cmd *cobra.Command, args []string) {
82+
Run: func(_ *cobra.Command, args []string) {
8283
for _, email := range args {
8384
if !validateEmail(email) {
8485
checkErr(fmt.Errorf("%s does not appear to be a valid email address", email))
@@ -108,7 +109,7 @@ var adminsRemoveCmd = &cobra.Command{
108109
Updates the application Cloudformation stack to remove an administrators.`,
109110
DisableFlagsInUseLine: true,
110111
Args: cobra.MinimumNArgs(1),
111-
Run: func(cmd *cobra.Command, args []string) {
112+
Run: func(_ *cobra.Command, args []string) {
112113
for _, email := range args {
113114
if !validateEmail(email) {
114115
checkErr(fmt.Errorf("%s does not appear to be a valid email address", email))
@@ -137,7 +138,7 @@ func init() {
137138
"account",
138139
"c",
139140
"",
140-
"AWS account ID or alias (not needed if you are only the administrator of one account)",
141+
utils.AccountFlagHelpText,
141142
)
142143
adminsCmd.PersistentFlags().BoolVar(
143144
&UseAWSCredentials,

cmd/create.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/apppackio/apppack/bridge"
2323
"github.com/apppackio/apppack/stacks"
2424
"github.com/apppackio/apppack/ui"
25+
"github.com/apppackio/apppack/utils"
2526
"github.com/aws/aws-sdk-go/aws/session"
2627
"github.com/logrusorgru/aurora"
2728
"github.com/spf13/cobra"
@@ -293,7 +294,7 @@ var createRegionCmd = &cobra.Command{
293294
Short: "setup AppPack resources for an AWS region",
294295
Long: "*Requires admin permissions.*",
295296
DisableFlagsInUseLine: true,
296-
Run: func(cmd *cobra.Command, args []string) {
297+
Run: func(cmd *cobra.Command, _ []string) {
297298
sess, err := adminSession(MaxSessionDurationSeconds)
298299
checkErr(err)
299300
region := sess.Config.Region
@@ -310,7 +311,7 @@ var createRegionCmd = &cobra.Command{
310311

311312
func init() {
312313
rootCmd.AddCommand(createCmd)
313-
createCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", "AWS account ID or alias (not needed if you are only the administrator of one account)")
314+
createCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", utils.AccountFlagHelpText)
314315
createCmd.PersistentFlags().BoolVar(&UseAWSCredentials, "aws-credentials", false, "use AWS credentials instead of AppPack.io federation")
315316
createCmd.PersistentFlags().BoolVar(&createChangeSet, "check", false, "check stack in Cloudformation before creating")
316317
createCmd.PersistentFlags().BoolVar(&nonInteractive, "non-interactive", false, "do not prompt for missing flags")

cmd/destroy.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/apppackio/apppack/stacks"
2222
"github.com/apppackio/apppack/ui"
23+
"github.com/apppackio/apppack/utils"
2324
"github.com/aws/aws-sdk-go/aws/session"
2425
"github.com/sirupsen/logrus"
2526
"github.com/spf13/cobra"
@@ -80,7 +81,7 @@ var destroyAccountCmd = &cobra.Command{
8081
Short: "destroy AWS resources used by your AppPack account",
8182
Long: "*Requires admin permissions.*",
8283
DisableFlagsInUseLine: true,
83-
Run: func(cmd *cobra.Command, args []string) {
84+
Run: func(_ *cobra.Command, _ []string) {
8485
ui.StartSpinner()
8586
sess, err := adminSession(MaxSessionDurationSeconds)
8687
checkErr(err)
@@ -94,7 +95,7 @@ var destroyRegionCmd = &cobra.Command{
9495
Short: "destroy AWS resources used by an AppPack region",
9596
Long: "*Requires admin permissions.*",
9697
DisableFlagsInUseLine: true,
97-
Run: func(cmd *cobra.Command, args []string) {
98+
Run: func(_ *cobra.Command, _ []string) {
9899
ui.StartSpinner()
99100
sess, err := adminSession(MaxSessionDurationSeconds)
100101
checkErr(err)
@@ -109,7 +110,7 @@ var destroyRedisCmd = &cobra.Command{
109110
Long: "*Requires admin permissions.*",
110111
Args: cobra.ExactArgs(1),
111112
DisableFlagsInUseLine: true,
112-
Run: func(cmd *cobra.Command, args []string) {
113+
Run: func(_ *cobra.Command, args []string) {
113114
ui.StartSpinner()
114115
sess, err := adminSession(MaxSessionDurationSeconds)
115116
checkErr(err)
@@ -124,7 +125,7 @@ var destroyDatabaseCmd = &cobra.Command{
124125
Long: "*Requires admin permissions.*",
125126
Args: cobra.ExactArgs(1),
126127
DisableFlagsInUseLine: true,
127-
Run: func(cmd *cobra.Command, args []string) {
128+
Run: func(_ *cobra.Command, args []string) {
128129
ui.StartSpinner()
129130
sess, err := adminSession(MaxSessionDurationSeconds)
130131
checkErr(err)
@@ -139,7 +140,7 @@ var destroyClusterCmd = &cobra.Command{
139140
Long: "*Requires admin permissions.*",
140141
DisableFlagsInUseLine: true,
141142
Args: cobra.ExactArgs(1),
142-
Run: func(cmd *cobra.Command, args []string) {
143+
Run: func(_ *cobra.Command, args []string) {
143144
ui.StartSpinner()
144145
sess, err := adminSession(MaxSessionDurationSeconds)
145146
checkErr(err)
@@ -154,7 +155,7 @@ var destroyAppCmd = &cobra.Command{
154155
Long: "*Requires admin permissions.*",
155156
DisableFlagsInUseLine: true,
156157
Args: cobra.ExactArgs(1),
157-
Run: func(cmd *cobra.Command, args []string) {
158+
Run: func(_ *cobra.Command, args []string) {
158159
ui.StartSpinner()
159160
sess, err := adminSession(MaxSessionDurationSeconds)
160161
checkErr(err)
@@ -169,7 +170,7 @@ var destroyPipelineCmd = &cobra.Command{
169170
Long: "*Requires admin permissions.*",
170171
DisableFlagsInUseLine: true,
171172
Args: cobra.ExactArgs(1),
172-
Run: func(cmd *cobra.Command, args []string) {
173+
Run: func(_ *cobra.Command, args []string) {
173174
ui.StartSpinner()
174175
sess, err := adminSession(MaxSessionDurationSeconds)
175176
checkErr(err)
@@ -184,7 +185,7 @@ var destroyCustomDomainCmd = &cobra.Command{
184185
Long: "*Requires admin permissions.*",
185186
DisableFlagsInUseLine: true,
186187
Args: cobra.ExactArgs(1),
187-
Run: func(cmd *cobra.Command, args []string) {
188+
Run: func(_ *cobra.Command, args []string) {
188189
ui.StartSpinner()
189190
sess, err := adminSession(MaxSessionDurationSeconds)
190191
checkErr(err)
@@ -194,7 +195,7 @@ var destroyCustomDomainCmd = &cobra.Command{
194195

195196
func init() {
196197
rootCmd.AddCommand(destroyCmd)
197-
destroyCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", "AWS account ID or alias (not needed if you are only the administrator of one account)")
198+
destroyCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", utils.AccountFlagHelpText)
198199
destroyCmd.PersistentFlags().BoolVar(&UseAWSCredentials, "aws-credentials", false, "use AWS credentials instead of AppPack.io federation")
199200
destroyCmd.PersistentFlags().StringVar(&region, "region", "", "AWS region to destroy resources in")
200201

cmd/modify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ package cmd
9090

9191
// func init() {
9292
// rootCmd.AddCommand(modifyCmd)
93-
// modifyCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", "AWS account ID or alias (not needed if you are only the administrator of one account)")
93+
// modifyCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", utils.AccountFlagHelpText)
9494
// modifyCmd.PersistentFlags().BoolVar(&UseAWSCredentials, "aws-credentials", false, "use AWS credentials instead of AppPack.io federation")
9595
// modifyCmd.PersistentFlags().BoolVar(&createChangeSet, "check", false, "check stack in Cloudformation before creating")
9696
// modifyCmd.PersistentFlags().BoolVar(&nonInteractive, "non-interactive", false, "do not prompt for user input")

cmd/root.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,37 @@ var (
5050
MaxSessionDurationSeconds = 3600
5151
)
5252

53+
func userHasMultipleAccounts() bool {
54+
ui.StartSpinner()
55+
admins, err := auth.AdminList()
56+
checkErr(err)
57+
ui.Spinner.Stop()
58+
return len(admins) > 1
59+
}
60+
5361
// rootCmd represents the base command when called without any subcommands
5462
var rootCmd = &cobra.Command{
5563
Use: "apppack",
5664
Short: "the CLI interface to AppPack.io",
5765
Long: `AppPack is a tool to manage applications deployed on AWS via AppPack.io`,
5866
DisableAutoGenTag: true,
5967
DisableFlagsInUseLine: true,
60-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
68+
PersistentPreRun: func(_ *cobra.Command, _ []string) {
6169
if debug {
6270
logrus.SetOutput(os.Stdout)
6371
logrus.SetLevel(logrus.DebugLevel)
6472
} else {
6573
logrus.SetLevel(logrus.ErrorLevel)
6674
}
75+
// Check for account flag or environment variable
76+
if AccountIDorAlias == "" {
77+
AccountIDorAlias = os.Getenv("APPPACK_ACCOUNT")
78+
}
79+
80+
// If neither is set and the user has multiple accounts, throw an error
81+
if AccountIDorAlias == "" && userHasMultipleAccounts() {
82+
checkErr(fmt.Errorf("you must specify an account using the -c flag or the APPPACK_ACCOUNT environment variable"))
83+
}
6784
},
6885
}
6986

cmd/stacks.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/apppackio/apppack/bridge"
2525
"github.com/apppackio/apppack/ui"
26+
"github.com/apppackio/apppack/utils"
2627
"github.com/aws/aws-sdk-go/service/cloudformation"
2728
"github.com/juju/ansiterm/tabwriter"
2829
"github.com/logrusorgru/aurora"
@@ -67,7 +68,7 @@ var stacksCmd = &cobra.Command{
6768
Long: "*Requires admin permissions.*",
6869
DisableFlagsInUseLine: true,
6970
Args: cobra.NoArgs,
70-
Run: func(cmd *cobra.Command, args []string) {
71+
Run: func(_ *cobra.Command, _ []string) {
7172
ui.StartSpinner()
7273
sess, err := adminSession(SessionDurationSeconds)
7374
checkErr(err)
@@ -113,6 +114,6 @@ var stacksCmd = &cobra.Command{
113114

114115
func init() {
115116
rootCmd.AddCommand(stacksCmd)
116-
stacksCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", "AWS account ID or alias (not needed if you are only the administrator of one account)")
117+
stacksCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", utils.AccountFlagHelpText)
117118
stacksCmd.PersistentFlags().BoolVar(&UseAWSCredentials, "aws-credentials", false, "use AWS credentials instead of AppPack.io federation")
118119
}

cmd/upgrade.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/apppackio/apppack/stacks"
2222
"github.com/apppackio/apppack/ui"
23+
"github.com/apppackio/apppack/utils"
2324
"github.com/aws/aws-sdk-go/aws/session"
2425
"github.com/logrusorgru/aurora"
2526
"github.com/spf13/cobra"
@@ -75,7 +76,7 @@ var upgradeAccountCmd = &cobra.Command{
7576
Short: "upgrade your AppPack account stack",
7677
Long: "*Requires admin permissions.*",
7778
DisableFlagsInUseLine: true,
78-
Run: func(cmd *cobra.Command, args []string) {
79+
Run: func(_ *cobra.Command, _ []string) {
7980
ui.StartSpinner()
8081
sess, err := adminSession(MaxSessionDurationSeconds)
8182
checkErr(err)
@@ -89,7 +90,7 @@ var upgradeRegionCmd = &cobra.Command{
8990
Short: "upgrade your AppPack region stack",
9091
Long: "*Requires admin permissions.*",
9192
DisableFlagsInUseLine: true,
92-
Run: func(cmd *cobra.Command, args []string) {
93+
Run: func(_ *cobra.Command, _ []string) {
9394
ui.StartSpinner()
9495
sess, err := adminSession(MaxSessionDurationSeconds)
9596
checkErr(err)
@@ -104,7 +105,7 @@ var upgradeAppCmd = &cobra.Command{
104105
Long: "*Requires admin permissions.*",
105106
DisableFlagsInUseLine: true,
106107
Args: cobra.ExactArgs(1),
107-
Run: func(cmd *cobra.Command, args []string) {
108+
Run: func(_ *cobra.Command, args []string) {
108109
ui.StartSpinner()
109110
sess, err := adminSession(MaxSessionDurationSeconds)
110111
checkErr(err)
@@ -119,7 +120,7 @@ var upgradePipelineCmd = &cobra.Command{
119120
Long: "*Requires admin permissions.*",
120121
DisableFlagsInUseLine: true,
121122
Args: cobra.ExactArgs(1),
122-
Run: func(cmd *cobra.Command, args []string) {
123+
Run: func(_ *cobra.Command, args []string) {
123124
ui.StartSpinner()
124125
sess, err := adminSession(MaxSessionDurationSeconds)
125126
checkErr(err)
@@ -134,7 +135,7 @@ var upgradeClusterCmd = &cobra.Command{
134135
Long: "*Requires admin permissions.*",
135136
DisableFlagsInUseLine: true,
136137
Args: cobra.ExactArgs(1),
137-
Run: func(cmd *cobra.Command, args []string) {
138+
Run: func(_ *cobra.Command, args []string) {
138139
ui.StartSpinner()
139140
sess, err := adminSession(MaxSessionDurationSeconds)
140141
checkErr(err)
@@ -149,7 +150,7 @@ var upgradeRedisCmd = &cobra.Command{
149150
Long: "*Requires admin permissions.*",
150151
DisableFlagsInUseLine: true,
151152
Args: cobra.ExactArgs(1),
152-
Run: func(cmd *cobra.Command, args []string) {
153+
Run: func(_ *cobra.Command, args []string) {
153154
ui.StartSpinner()
154155
sess, err := adminSession(MaxSessionDurationSeconds)
155156
checkErr(err)
@@ -164,7 +165,7 @@ var upgradeDatabaseCmd = &cobra.Command{
164165
Long: "*Requires admin permissions.*",
165166
DisableFlagsInUseLine: true,
166167
Args: cobra.ExactArgs(1),
167-
Run: func(cmd *cobra.Command, args []string) {
168+
Run: func(_ *cobra.Command, args []string) {
168169
ui.StartSpinner()
169170
sess, err := adminSession(MaxSessionDurationSeconds)
170171
checkErr(err)
@@ -174,7 +175,7 @@ var upgradeDatabaseCmd = &cobra.Command{
174175

175176
func init() {
176177
rootCmd.AddCommand(upgradeCmd)
177-
upgradeCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", "AWS account ID or alias (not needed if you are only the administrator of one account)")
178+
upgradeCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", utils.AccountFlagHelpText)
178179
upgradeCmd.PersistentFlags().BoolVar(&UseAWSCredentials, "aws-credentials", false, "use AWS credentials instead of AppPack.io federation")
179180
upgradeCmd.PersistentFlags().BoolVar(&createChangeSet, "check", false, "check stack in Cloudformation before creating")
180181
upgradeCmd.PersistentFlags().StringVar(&region, "region", "", "AWS region to upgrade resources in")

utils/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package utils
2+
3+
var AccountFlagHelpText string = "AWS account ID or alias. Use this flag to override the APPPACK_ACCOUNT environment variable (not needed if you are the administrator of only one account)."

0 commit comments

Comments
 (0)