Skip to content

Commit

Permalink
feat: Add APPPACK_ACCOUNT env variable (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
CuriousLearner authored Aug 28, 2024
1 parent b1c030b commit cf06763
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 31 deletions.
9 changes: 5 additions & 4 deletions cmd/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/apppackio/apppack/stacks"
"github.com/apppackio/apppack/stringslice"
"github.com/apppackio/apppack/ui"
"github.com/apppackio/apppack/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -107,7 +108,7 @@ var accessCmd = &cobra.Command{
Short: "list users with access to the app",
Args: cobra.NoArgs,
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
ui.StartSpinner()
var err error
sess, err := adminSession(SessionDurationSeconds)
Expand All @@ -131,7 +132,7 @@ Updates the application Cloudformation stack to add access for the user.`,
DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(1),
Example: "apppack -a my-app access add [email protected] [email protected]",
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
for _, email := range args {
if !validateEmail(email) {
checkErr(fmt.Errorf("%s does not appear to be a valid email address", email))
Expand Down Expand Up @@ -163,7 +164,7 @@ Updates the application Cloudformation stack to remove access for the user.`,
DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(1),
Example: "apppack -a my-app access remove [email protected] [email protected]",
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
for _, email := range args {
if !validateEmail(email) {
checkErr(fmt.Errorf("%s does not appear to be a valid email address", email))
Expand Down Expand Up @@ -195,7 +196,7 @@ func init() {
"account",
"c",
"",
"AWS account ID or alias (not needed if you are only the administrator of one account)",
utils.AccountFlagHelpText,
)
accessCmd.PersistentFlags().BoolVar(
&UseAWSCredentials,
Expand Down
9 changes: 5 additions & 4 deletions cmd/admins.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/apppackio/apppack/stacks"
"github.com/apppackio/apppack/stringslice"
"github.com/apppackio/apppack/ui"
"github.com/apppackio/apppack/utils"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -57,7 +58,7 @@ var adminsCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
Args: cobra.NoArgs,
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
ui.StartSpinner()
sess, err := adminSession(SessionDurationSeconds)
checkErr(err)
Expand All @@ -78,7 +79,7 @@ var adminsAddCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(1),
Example: "apppack admins add [email protected] [email protected]",
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
for _, email := range args {
if !validateEmail(email) {
checkErr(fmt.Errorf("%s does not appear to be a valid email address", email))
Expand Down Expand Up @@ -108,7 +109,7 @@ var adminsRemoveCmd = &cobra.Command{
Updates the application Cloudformation stack to remove an administrators.`,
DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
for _, email := range args {
if !validateEmail(email) {
checkErr(fmt.Errorf("%s does not appear to be a valid email address", email))
Expand Down Expand Up @@ -137,7 +138,7 @@ func init() {
"account",
"c",
"",
"AWS account ID or alias (not needed if you are only the administrator of one account)",
utils.AccountFlagHelpText,
)
adminsCmd.PersistentFlags().BoolVar(
&UseAWSCredentials,
Expand Down
5 changes: 3 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/apppackio/apppack/bridge"
"github.com/apppackio/apppack/stacks"
"github.com/apppackio/apppack/ui"
"github.com/apppackio/apppack/utils"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -293,7 +294,7 @@ var createRegionCmd = &cobra.Command{
Short: "setup AppPack resources for an AWS region",
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, _ []string) {
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
region := sess.Config.Region
Expand All @@ -310,7 +311,7 @@ var createRegionCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(createCmd)
createCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", "AWS account ID or alias (not needed if you are only the administrator of one account)")
createCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", utils.AccountFlagHelpText)
createCmd.PersistentFlags().BoolVar(&UseAWSCredentials, "aws-credentials", false, "use AWS credentials instead of AppPack.io federation")
createCmd.PersistentFlags().BoolVar(&createChangeSet, "check", false, "check stack in Cloudformation before creating")
createCmd.PersistentFlags().BoolVar(&nonInteractive, "non-interactive", false, "do not prompt for missing flags")
Expand Down
19 changes: 10 additions & 9 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/apppackio/apppack/stacks"
"github.com/apppackio/apppack/ui"
"github.com/apppackio/apppack/utils"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -80,7 +81,7 @@ var destroyAccountCmd = &cobra.Command{
Short: "destroy AWS resources used by your AppPack account",
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -94,7 +95,7 @@ var destroyRegionCmd = &cobra.Command{
Short: "destroy AWS resources used by an AppPack region",
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -109,7 +110,7 @@ var destroyRedisCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
Args: cobra.ExactArgs(1),
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -124,7 +125,7 @@ var destroyDatabaseCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
Args: cobra.ExactArgs(1),
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -139,7 +140,7 @@ var destroyClusterCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -154,7 +155,7 @@ var destroyAppCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -169,7 +170,7 @@ var destroyPipelineCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -184,7 +185,7 @@ var destroyCustomDomainCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -194,7 +195,7 @@ var destroyCustomDomainCmd = &cobra.Command{

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ package cmd

// func init() {
// rootCmd.AddCommand(modifyCmd)
// modifyCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", "AWS account ID or alias (not needed if you are only the administrator of one account)")
// modifyCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", utils.AccountFlagHelpText)
// modifyCmd.PersistentFlags().BoolVar(&UseAWSCredentials, "aws-credentials", false, "use AWS credentials instead of AppPack.io federation")
// modifyCmd.PersistentFlags().BoolVar(&createChangeSet, "check", false, "check stack in Cloudformation before creating")
// modifyCmd.PersistentFlags().BoolVar(&nonInteractive, "non-interactive", false, "do not prompt for user input")
Expand Down
19 changes: 18 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,37 @@ var (
MaxSessionDurationSeconds = 3600
)

func userHasMultipleAccounts() bool {
ui.StartSpinner()
admins, err := auth.AdminList()
checkErr(err)
ui.Spinner.Stop()
return len(admins) > 1
}

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "apppack",
Short: "the CLI interface to AppPack.io",
Long: `AppPack is a tool to manage applications deployed on AWS via AppPack.io`,
DisableAutoGenTag: true,
DisableFlagsInUseLine: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
PersistentPreRun: func(_ *cobra.Command, _ []string) {
if debug {
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.ErrorLevel)
}
// Check for account flag or environment variable
if AccountIDorAlias == "" {
AccountIDorAlias = os.Getenv("APPPACK_ACCOUNT")
}

// If neither is set and the user has multiple accounts, throw an error
if AccountIDorAlias == "" && userHasMultipleAccounts() {
checkErr(fmt.Errorf("you must specify an account using the -c flag or the APPPACK_ACCOUNT environment variable"))
}
},
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/apppackio/apppack/bridge"
"github.com/apppackio/apppack/ui"
"github.com/apppackio/apppack/utils"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/juju/ansiterm/tabwriter"
"github.com/logrusorgru/aurora"
Expand Down Expand Up @@ -67,7 +68,7 @@ var stacksCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
ui.StartSpinner()
sess, err := adminSession(SessionDurationSeconds)
checkErr(err)
Expand Down Expand Up @@ -113,6 +114,6 @@ var stacksCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(stacksCmd)
stacksCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", "AWS account ID or alias (not needed if you are only the administrator of one account)")
stacksCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", utils.AccountFlagHelpText)
stacksCmd.PersistentFlags().BoolVar(&UseAWSCredentials, "aws-credentials", false, "use AWS credentials instead of AppPack.io federation")
}
17 changes: 9 additions & 8 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/apppackio/apppack/stacks"
"github.com/apppackio/apppack/ui"
"github.com/apppackio/apppack/utils"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -75,7 +76,7 @@ var upgradeAccountCmd = &cobra.Command{
Short: "upgrade your AppPack account stack",
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -89,7 +90,7 @@ var upgradeRegionCmd = &cobra.Command{
Short: "upgrade your AppPack region stack",
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -104,7 +105,7 @@ var upgradeAppCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -119,7 +120,7 @@ var upgradePipelineCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -134,7 +135,7 @@ var upgradeClusterCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -149,7 +150,7 @@ var upgradeRedisCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -164,7 +165,7 @@ var upgradeDatabaseCmd = &cobra.Command{
Long: "*Requires admin permissions.*",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
ui.StartSpinner()
sess, err := adminSession(MaxSessionDurationSeconds)
checkErr(err)
Expand All @@ -174,7 +175,7 @@ var upgradeDatabaseCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(upgradeCmd)
upgradeCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", "AWS account ID or alias (not needed if you are only the administrator of one account)")
upgradeCmd.PersistentFlags().StringVarP(&AccountIDorAlias, "account", "c", "", utils.AccountFlagHelpText)
upgradeCmd.PersistentFlags().BoolVar(&UseAWSCredentials, "aws-credentials", false, "use AWS credentials instead of AppPack.io federation")
upgradeCmd.PersistentFlags().BoolVar(&createChangeSet, "check", false, "check stack in Cloudformation before creating")
upgradeCmd.PersistentFlags().StringVar(&region, "region", "", "AWS region to upgrade resources in")
Expand Down
3 changes: 3 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package utils

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 comments on commit cf06763

Please sign in to comment.