diff --git a/cmd/airflow.go b/cmd/airflow.go index 33de45976..f8ccad589 100644 --- a/cmd/airflow.go +++ b/cmd/airflow.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "os" "path/filepath" "regexp" "strings" @@ -39,39 +40,44 @@ var ( } airflowDeployCmd = &cobra.Command{ - Use: "deploy", - Short: "Deploy an airflow project", - Long: "Deploy an airflow project to a given deployment", - Args: cobra.ExactArgs(1), - RunE: checkForProject(airflowDeploy), + Use: "deploy", + Short: "Deploy an airflow project", + Long: "Deploy an airflow project to a given deployment", + Args: cobra.ExactArgs(1), + PreRun: ensureProjectDir, + RunE: airflowDeploy, } airflowStartCmd = &cobra.Command{ - Use: "start", - Short: "Start a development airflow cluster", - Long: "Start a development airflow cluster", - RunE: checkForProject(airflowStart), + Use: "start", + Short: "Start a development airflow cluster", + Long: "Start a development airflow cluster", + PreRun: ensureProjectDir, + RunE: airflowStart, } airflowKillCmd = &cobra.Command{ - Use: "kill", - Short: "Kill a development airflow cluster", - Long: "Kill a development airflow cluster", - RunE: checkForProject(airflowKill), + Use: "kill", + Short: "Kill a development airflow cluster", + Long: "Kill a development airflow cluster", + PreRun: ensureProjectDir, + RunE: airflowKill, } airflowStopCmd = &cobra.Command{ - Use: "stop", - Short: "Stop a development airflow cluster", - Long: "Stop a development airflow cluster", - RunE: checkForProject(airflowStop), + Use: "stop", + Short: "Stop a development airflow cluster", + Long: "Stop a development airflow cluster", + PreRun: ensureProjectDir, + RunE: airflowStop, } airflowPSCmd = &cobra.Command{ - Use: "ps", - Short: "List airflow containers", - Long: "List airflow containers", - RunE: checkForProject(airflowPS), + Use: "ps", + Short: "List airflow containers", + Long: "List airflow containers", + PreRun: ensureProjectDir, + RunE: airflowPS, } ) @@ -105,14 +111,10 @@ func init() { airflowRootCmd.AddCommand(airflowPSCmd) } -// Check for project wraps functions that can only be run within a project directory -// and will return an error otherwise. -func checkForProject(f func(*cobra.Command, []string) error) func(*cobra.Command, []string) error { - return func(cmd *cobra.Command, args []string) error { - if len(projectRoot) > 0 { - return f(cmd, args) - } - return errors.New("Not in an astronomer project directory") +func ensureProjectDir(cmd *cobra.Command, args []string) { + if !(len(projectRoot) > 0) { + fmt.Println("Error: Not in an astronomer project directory") + os.Exit(1) } } diff --git a/cmd/config.go b/cmd/config.go index 52e8a7f13..7d3f2f8fb 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "os" "github.com/astronomerio/astro-cli/config" "github.com/pkg/errors" @@ -9,10 +10,15 @@ import ( ) var ( + // Flags + globalFlag bool + + //Commands configRootCmd = &cobra.Command{ - Use: "config", - Short: "Manage astro project configurations", - Long: "Manage astro project configurations", + Use: "config", + Short: "Manage astro project configurations", + Long: "Manage astro project configurations", + PersistentPreRun: ensureGlobalFlag, } configGetCmd = &cobra.Command{ @@ -20,8 +26,6 @@ var ( Short: "Get astro project configuration", Long: "Get astro project configuration", RunE: configGet, - // TODO add arg validator - // TODO check project specific config } configSetCmd = &cobra.Command{ @@ -29,8 +33,6 @@ var ( Short: "Set astro project configuration", Long: "Set astro project configuration", RunE: configSet, - // TODO add arg validator - // TODO check project specific config } ) @@ -40,13 +42,24 @@ func init() { // Config root RootCmd.AddCommand(configRootCmd) + RootCmd.PersistentFlags().BoolVarP(&globalFlag, "global", "g", false, "view or modify global config") // Config get configRootCmd.AddCommand(configGetCmd) + // Config set configRootCmd.AddCommand(configSetCmd) } +func ensureGlobalFlag(cmd *cobra.Command, args []string) { + if !(len(projectRoot) > 0) && !globalFlag { + var c = "astro config " + cmd.Use + " " + args[0] + " -g" + fmt.Println("You are attempting to " + cmd.Use + " a project config outside of a project directory\n" + + "To " + cmd.Use + " a global config try\n" + c) + os.Exit(1) + } +} + func configGet(command *cobra.Command, args []string) error { if len(args) != 1 { return errors.New("Must specify config key") @@ -62,7 +75,11 @@ func configGet(command *cobra.Command, args []string) error { return errors.New(errMsg) } - fmt.Printf("%s: %s\n", cfg.Path, cfg.GetString()) + if globalFlag { + fmt.Printf("%s: %s\n", cfg.Path, cfg.GetHomeString()) + } else { + fmt.Printf("%s: %s\n", cfg.Path, cfg.GetProjectString()) + } return nil } @@ -84,7 +101,11 @@ func configSet(command *cobra.Command, args []string) error { return errors.New(errMsg) } - cfg.SetProjectString(args[1]) + if globalFlag { + cfg.SetHomeString(args[1]) + } else { + cfg.SetProjectString(args[1]) + } fmt.Printf("Setting %s to %s successfully\n", cfg.Path, args[1]) return nil