diff --git a/airflow/airflow.go b/airflow/airflow.go index 74336b592..bf7e68739 100644 --- a/airflow/airflow.go +++ b/airflow/airflow.go @@ -1,13 +1,13 @@ package airflow import ( - "errors" - "fmt" "os" "path/filepath" "regexp" "strings" + "github.com/pkg/errors" + "github.com/iancoleman/strcase" "github.com/astronomerio/astro-cli/airflow/include" @@ -15,52 +15,46 @@ import ( "github.com/astronomerio/astro-cli/pkg/fileutil" ) -func initDirs(root string, dirs []string) bool { - // Any inputs exist - exists := false - +func initDirs(root string, dirs []string) error { // Create the dirs for _, dir := range dirs { // Create full path to directory fullpath := filepath.Join(root, dir) // Move on if already exists - if fileutil.Exists(fullpath) { - exists = true - continue + _, err := fileutil.Exists(fullpath) + if err != nil { + return errors.Wrapf(err, "failed to check existence of '%s'", fullpath) } // Create directory if err := os.MkdirAll(dir, 0777); err != nil { - fmt.Println(err) + return errors.Wrapf(err, "failed to create dir '%s'", dir) } } - return exists + return nil } -func initFiles(root string, files map[string]string) bool { - // Any inputs exist - exists := false - +func initFiles(root string, files map[string]string) error { // Create the files for file, content := range files { // Create full path to file fullpath := filepath.Join(root, file) - // Move on if already exiss - if fileutil.Exists(fullpath) { - exists = true - continue + // Move on if already exists + _, err := fileutil.Exists(fullpath) + if err != nil { + return errors.Wrapf(err, "failed to check existence of '%s'", fullpath) } // Write files out if err := fileutil.WriteStringToFile(fullpath, content); err != nil { - fmt.Println(err) + return errors.Wrapf(err, "failed to create file '%s'", fullpath) } } - return exists + return nil } // Init will scaffold out a new airflow project @@ -78,10 +72,14 @@ func Init(path string) error { } // Initailize directories - initDirs(path, dirs) + if err := initDirs(path, dirs); err != nil { + return errors.Wrap(err, "failed to create project directories") + } // Initialize files - initFiles(path, files) + if err := initFiles(path, files); err != nil { + return errors.Wrap(err, "failed to create project files") + } return nil } diff --git a/airflow/docker.go b/airflow/docker.go index f750081d0..4936c3e76 100644 --- a/airflow/docker.go +++ b/airflow/docker.go @@ -70,11 +70,10 @@ func imageBuild(path, imageName string) { } // generateConfig generates the docker-compose config -func generateConfig(projectName, airflowHome string) string { +func generateConfig(projectName, airflowHome string) (string, error) { tmpl, err := template.New("yml").Parse(include.Composeyml) if err != nil { - fmt.Println(err) - os.Exit(1) + return "", errors.Wrap(err, "failed to generate config") } config := ComposeConfig{ @@ -91,11 +90,10 @@ func generateConfig(projectName, airflowHome string) string { buff := new(bytes.Buffer) err = tmpl.Execute(buff, config) if err != nil { - fmt.Println(err) - os.Exit(1) + return "", errors.Wrap(err, "failed to generate config") } - return buff.String() + return buff.String(), nil } func checkServiceState(serviceState, expectedState string) bool { @@ -111,7 +109,10 @@ func checkServiceState(serviceState, expectedState string) bool { // createProject creates project with yaml config as context func createProject(projectName, airflowHome string) (project.APIProject, error) { // Generate the docker-compose yaml - yaml := generateConfig(projectName, airflowHome) + yaml, err := generateConfig(projectName, airflowHome) + if err != nil { + return nil, errors.Wrap(err, "failed to create project") + } // Create the project project, err := dockercompose.NewProject(&ctx.Context{ @@ -146,8 +147,7 @@ func Start(airflowHome string) error { // Ensure project is not already running for _, info := range psInfo { if checkServiceState(info["State"], dockerStateUp) { - fmt.Println(messages.COMPOSE_PROJECT_RUNNING) - os.Exit(1) + return errors.New("cannot start, project already running") } } imageBuild(airflowHome, imageName(projectName, "latest")) @@ -209,8 +209,7 @@ func Logs(airflowHome string, webserver, scheduler, follow bool) error { } if len(psInfo) == 0 { - fmt.Println("Project is not running, first start the project to view logs") - os.Exit(1) + return errors.Wrap(err, "cannot view logs, project not running") } if scheduler { diff --git a/auth/auth.go b/auth/auth.go index 549729a5b..b7a906ed6 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -2,7 +2,6 @@ package auth import ( "fmt" - "os" "github.com/astronomerio/astro-cli/cluster" "github.com/astronomerio/astro-cli/docker" @@ -10,6 +9,7 @@ import ( "github.com/astronomerio/astro-cli/messages" "github.com/astronomerio/astro-cli/pkg/httputil" "github.com/astronomerio/astro-cli/pkg/input" + "github.com/pkg/errors" ) var ( @@ -18,16 +18,15 @@ var ( ) // basicAuth handles authentication with the houston api -func basicAuth(username string) string { +func basicAuth(username string) (string, error) { password, _ := input.InputPassword(messages.INPUT_PASSWORD) token, err := api.CreateBasicToken(username, password) if err != nil { - fmt.Println(err) - os.Exit(1) + return "", errors.Wrap(err, "failed to fetch local/db auth token") } - return token.Token.Value + return token.Token.Value, nil } func getWorkspaceByLabel(label string) *houston.Workspace { @@ -100,8 +99,7 @@ func Login(domain string, oAuthOnly bool) error { authConfig, err := api.GetAuthConfig() if err != nil { - fmt.Println(err) - os.Exit(1) + return errors.Wrap(err, "failed to fetch auth config") } username := "" @@ -113,12 +111,14 @@ func Login(domain string, oAuthOnly bool) error { if authConfig.GoogleEnabled || authConfig.Auth0Enabled || authConfig.GithubEnabled { token = oAuth(c.GetAppURL() + "/login?source=cli") } else { - fmt.Println(messages.HOUSTON_OAUTH_DISABLED) - os.Exit(1) + return errors.New("cannot authenticate, oauth is disabled") } } else { if authConfig.LocalEnabled { - token = basicAuth(username) + token, err = basicAuth(username) + if err != nil { + return errors.Wrap(err, "local auth login failed") + } } else { fmt.Println(messages.HOUSTON_BASIC_AUTH_DISABLED) } diff --git a/cmd/airflow.go b/cmd/airflow.go index 46e20fef4..2458f6312 100644 --- a/cmd/airflow.go +++ b/cmd/airflow.go @@ -44,52 +44,52 @@ var ( } airflowDeployCmd = &cobra.Command{ - Use: "deploy DEPLOYMENT", - Short: "Deploy an airflow project", - Long: "Deploy an airflow project to a given deployment", - Args: cobra.MaximumNArgs(1), - PreRun: ensureProjectDir, - RunE: airflowDeploy, + Use: "deploy DEPLOYMENT", + Short: "Deploy an airflow project", + Long: "Deploy an airflow project to a given deployment", + Args: cobra.MaximumNArgs(1), + PreRunE: ensureProjectDir, + RunE: airflowDeploy, } airflowStartCmd = &cobra.Command{ - Use: "start", - Short: "Start a development airflow cluster", - Long: "Start a development airflow cluster", - PreRun: ensureProjectDir, - RunE: airflowStart, + Use: "start", + Short: "Start a development airflow cluster", + Long: "Start a development airflow cluster", + PreRunE: ensureProjectDir, + RunE: airflowStart, } airflowKillCmd = &cobra.Command{ - Use: "kill", - Short: "Kill a development airflow cluster", - Long: "Kill a development airflow cluster", - PreRun: ensureProjectDir, - RunE: airflowKill, + Use: "kill", + Short: "Kill a development airflow cluster", + Long: "Kill a development airflow cluster", + PreRunE: ensureProjectDir, + RunE: airflowKill, } airflowLogsCmd = &cobra.Command{ - Use: "logs", - Short: "Output logs for a development airflow cluster", - Long: "Output logs for a development airflow cluster", - PreRun: ensureProjectDir, - RunE: airflowLogs, + Use: "logs", + Short: "Output logs for a development airflow cluster", + Long: "Output logs for a development airflow cluster", + PreRunE: ensureProjectDir, + RunE: airflowLogs, } airflowStopCmd = &cobra.Command{ - Use: "stop", - Short: "Stop a development airflow cluster", - Long: "Stop a development airflow cluster", - PreRun: ensureProjectDir, - RunE: airflowStop, + Use: "stop", + Short: "Stop a development airflow cluster", + Long: "Stop a development airflow cluster", + PreRunE: ensureProjectDir, + RunE: airflowStop, } airflowPSCmd = &cobra.Command{ - Use: "ps", - Short: "List airflow containers", - Long: "List airflow containers", - PreRun: ensureProjectDir, - RunE: airflowPS, + Use: "ps", + Short: "List airflow containers", + Long: "List airflow containers", + PreRunE: ensureProjectDir, + RunE: airflowPS, } ) @@ -127,18 +127,28 @@ func init() { airflowRootCmd.AddCommand(airflowPSCmd) } -func ensureProjectDir(cmd *cobra.Command, args []string) { - if !config.IsProjectDir(config.WorkingPath) { - fmt.Println(messages.CONFIG_PROJECT_DIR_ERROR) - os.Exit(1) +func ensureProjectDir(cmd *cobra.Command, args []string) error { + isProjectDir, err := config.IsProjectDir(config.WorkingPath) + if err != nil { + return errors.Wrap(err, "cannot ensure is a project directory") + } + + if !isProjectDir { + return errors.New("not in a project directory") } projectConfigFile := filepath.Join(config.WorkingPath, config.ConfigDir, config.ConfigFileNameWithExt) - configExists := fileutil.Exists(projectConfigFile) + + configExists, err := fileutil.Exists(projectConfigFile) + if err != nil { + return errors.Wrapf(err, "failed to check existence of '%s'", projectConfigFile) + } + if !configExists { - fmt.Println("Error: Project not initialized") - os.Exit(1) + return errors.New("project config file does not exists") } + + return nil } // Use project name for image name @@ -172,6 +182,11 @@ func airflowInit(cmd *cobra.Command, args []string) error { config.CreateProjectConfig(config.WorkingPath) } config.CFG.ProjectName.SetProjectString(projectName) + + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + + // Execute method airflow.Init(config.WorkingPath) if exists { @@ -184,7 +199,11 @@ func airflowInit(cmd *cobra.Command, args []string) error { } func airflowDeploy(cmd *cobra.Command, args []string) error { - ws := workspaceValidator() + ws, err := coalesceWorkspace() + if err != nil { + return errors.Wrap(err, "failed to find a valid workspace") + // fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag") + } releaseName := "" @@ -202,16 +221,26 @@ func airflowDeploy(cmd *cobra.Command, args []string) error { fmt.Println(messages.REGISTRY_UNCOMMITTED_CHANGES) return nil } + + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return airflow.Deploy(config.WorkingPath, releaseName, ws, forcePrompt) } // Start an airflow cluster func airflowStart(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return airflow.Start(config.WorkingPath) } // Kill an airflow cluster func airflowKill(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return airflow.Kill(config.WorkingPath) } @@ -223,15 +252,25 @@ func airflowLogs(cmd *cobra.Command, args []string) error { webserverLogs = true } + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + + return airflow.Logs(config.WorkingPath, webserverLogs, schedulerLogs, followLogs) } // Stop an airflow cluster func airflowStop(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return airflow.Stop(config.WorkingPath) } // List containers of an airflow cluster func airflowPS(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return airflow.PS(config.WorkingPath) } diff --git a/cmd/auth.go b/cmd/auth.go index 47544b1f7..6da73f589 100644 --- a/cmd/auth.go +++ b/cmd/auth.go @@ -49,6 +49,9 @@ func authLogin(cmd *cobra.Command, args []string) error { domain = args[0] } + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + err := auth.Login(domain, oAuthOnly) if err != nil { return err @@ -65,5 +68,9 @@ func authLogout(cmd *cobra.Command, args []string) { domain = c.Domain } + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + + auth.Logout(domain) } diff --git a/cmd/cluster.go b/cmd/cluster.go index e8aa8335a..7d8cce966 100644 --- a/cmd/cluster.go +++ b/cmd/cluster.go @@ -40,10 +40,15 @@ func init() { } func clusterList(cmd *cobra.Command, args []string) error { - cluster.List() - return nil + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + + return cluster.List() } func clusterSwitch(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return cluster.Switch(args[0]) } diff --git a/cmd/config.go b/cmd/config.go index 338924b4e..40041a1b4 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -50,14 +50,16 @@ func init() { } func ensureGlobalFlag(cmd *cobra.Command, args []string) { - if !config.IsProjectDir(config.WorkingPath) && !globalFlag { + isProjectDir, _ := config.IsProjectDir(config.WorkingPath) + + if !isProjectDir && !globalFlag { var c = "astro config " + cmd.Use + " " + args[0] + " -g" fmt.Printf(messages.CONFIG_USE_OUTSIDE_PROJECT_DIR, cmd.Use, cmd.Use, c) os.Exit(1) } } -func configGet(command *cobra.Command, args []string) error { +func configGet(cmd *cobra.Command, args []string) error { if len(args) != 1 { return errors.New(messages.CONFIG_PATH_KEY_MISSING_ERROR) } @@ -67,6 +69,10 @@ func configGet(command *cobra.Command, args []string) error { return errors.New(messages.CONFIG_PATH_KEY_INVALID_ERROR) } + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + + if globalFlag { fmt.Printf("%s: %s\n", cfg.Path, cfg.GetHomeString()) } else { @@ -76,7 +82,7 @@ func configGet(command *cobra.Command, args []string) error { return nil } -func configSet(command *cobra.Command, args []string) error { +func configSet(cmd *cobra.Command, args []string) error { if len(args) != 2 { return errors.New(messages.CONFIG_INVALID_SET_ARGS) } @@ -88,6 +94,9 @@ func configSet(command *cobra.Command, args []string) error { return errors.New(messages.CONFIG_PATH_KEY_INVALID_ERROR) } + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + if globalFlag { cfg.SetHomeString(args[1]) } else { diff --git a/cmd/deployment.go b/cmd/deployment.go index 2393f8a77..21f570b78 100644 --- a/cmd/deployment.go +++ b/cmd/deployment.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/astronomerio/astro-cli/deployment" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -73,16 +74,35 @@ func init() { } func deploymentCreate(cmd *cobra.Command, args []string) error { - ws := workspaceValidator() + ws, err := coalesceWorkspace() + if err != nil { + return errors.Wrap(err, "failed to find a valid workspace") + // fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag") + } + + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return deployment.Create(args[0], ws) } func deploymentDelete(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return deployment.Delete(args[0]) } func deploymentList(cmd *cobra.Command, args []string) error { - ws := workspaceValidator() + ws, err := coalesceWorkspace() + if err != nil { + return errors.Wrap(err, "failed to find a valid workspace") + // fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag") + } + + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return deployment.List(ws) } @@ -92,5 +112,8 @@ func deploymentUpdate(cmd *cobra.Command, args []string) error { return err } + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return deployment.Update(args[0], argsMap) } diff --git a/cmd/root.go b/cmd/root.go index 7e1b145a4..ce9038be2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,6 +7,7 @@ import ( // RootCmd is the astro root command. var ( + // Debug bool workspaceId string RootCmd = &cobra.Command{ Use: "astro", @@ -17,4 +18,5 @@ var ( func init() { cobra.OnInitialize(config.InitConfig) + // RootCmd.PersistentFlags().BoolVarP(&Debug, "debug", "d", false, "debug output") } diff --git a/cmd/user.go b/cmd/user.go index 975171938..606eccf80 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -30,13 +30,13 @@ var ( RunE: userCreate, } - userDeleteCmd = &cobra.Command{ - Use: "delete", - Aliases: []string{"de"}, - Short: "Delete an astronomer user", - Long: "Delete an astronomer user", - Run: userDelete, - } + // userDeleteCmd = &cobra.Command{ + // Use: "delete", + // Aliases: []string{"de"}, + // Short: "Delete an astronomer user", + // Long: "Delete an astronomer user", + // Run: userDelete, + // } ) func init() { @@ -51,16 +51,19 @@ func init() { userCreateCmd.Flags().StringVar(&userEmail, "email", "", "Supply user email at runtime") // User delete - userRootCmd.AddCommand(userDeleteCmd) + // userRootCmd.AddCommand(userDeleteCmd) } -func userList(cmd *cobra.Command, args []string) error { - return nil -} +// func userList(cmd *cobra.Command, args []string) error { +// return nil +// } func userCreate(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return user.Create(userEmail) } -func userDelete(cmd *cobra.Command, args []string) { -} +// func userDelete(cmd *cobra.Command, args []string) { +// } diff --git a/cmd/validation.go b/cmd/validation.go index 2508447a1..daf76ebc1 100644 --- a/cmd/validation.go +++ b/cmd/validation.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "os" "strings" "github.com/astronomerio/astro-cli/workspace" @@ -51,23 +50,20 @@ func updateArgValidator(args, validArgs []string) error { return nil } -func workspaceValidator() string { +func coalesceWorkspace() (string, error) { wsFlag := workspaceId wsCfg, err := workspace.GetCurrentWorkspace() if err != nil { - return "" + return "", errors.Wrap(err, "failed to get current workspace") } if len(wsFlag) != 0 { - return wsFlag + return wsFlag, nil } if len(wsCfg) != 0 { - return wsCfg + return wsCfg, nil } - fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag") - os.Exit(1) - - return "" + return "", errors.New("no valid workspace source found") } diff --git a/cmd/version.go b/cmd/version.go index 1c6bb0f78..9074c4f43 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -29,6 +29,9 @@ func init() { } func printVersion(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + err := version.PrintVersion(currVersion, currCommit) if err != nil { return err @@ -37,6 +40,9 @@ func printVersion(cmd *cobra.Command, args []string) error { } func upgradeCheck(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + err := version.CheckForUpdate(currVersion, currCommit) if err != nil { return err diff --git a/cmd/workspace.go b/cmd/workspace.go index fa21943d6..e6be287cd 100644 --- a/cmd/workspace.go +++ b/cmd/workspace.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/astronomerio/astro-cli/workspace" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -120,15 +121,24 @@ func workspaceCreate(cmd *cobra.Command, args []string) error { if len(createDesc) == 0 { createDesc = "N/A" } + + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return workspace.Create(args[0], createDesc) } func workspaceList(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return workspace.List() - } func workspaceDelete(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return workspace.Delete(args[0]) } @@ -138,19 +148,41 @@ func workspaceUpdate(cmd *cobra.Command, args []string) error { return err } + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return workspace.Update(args[0], argsMap) } func workspaceUserAdd(cmd *cobra.Command, args []string) error { - ws := workspaceValidator() + ws, err := coalesceWorkspace() + if err != nil { + return errors.Wrap(err, "failed to find a valid workspace") + // fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag") + } + + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return workspace.Add(ws, args[0]) } func workspaceUserRm(cmd *cobra.Command, args []string) error { - ws := workspaceValidator() + ws, err := coalesceWorkspace() + if err != nil { + return errors.Wrap(err, "failed to find a valid workspace") + // fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag") + } + + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return workspace.Remove(ws, args[0]) } func workspaceSwitch(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + return workspace.Switch(args[0]) } diff --git a/config/config.go b/config/config.go index 87b3fabf3..bb86f5dfc 100644 --- a/config/config.go +++ b/config/config.go @@ -22,14 +22,14 @@ var ( ConfigDir = ".astro" // HomePath is the path to a users home directory - HomePath = fileutil.GetHomeDir() + HomePath, _ = fileutil.GetHomeDir() // HomeConfigPath is the path to the users global config directory HomeConfigPath = filepath.Join(HomePath, ConfigDir) // HomeConfigFile is the global config file HomeConfigFile = filepath.Join(HomeConfigPath, ConfigFileNameWithExt) // WorkingPath is the path to the working directory - WorkingPath = fileutil.GetWorkingDir() + WorkingPath, _ = fileutil.GetWorkingDir() // CFGStrMap maintains string to cfg mapping CFGStrMap = make(map[string]cfg) @@ -79,7 +79,9 @@ func initHome() { } // If home config does not exist, create it - if !fileutil.Exists(HomeConfigFile) { + homeConfigExists, _ := fileutil.Exists(HomeConfigFile) + + if !homeConfigExists { err := CreateConfig(viperHome, HomeConfigPath, HomeConfigFile) if err != nil { fmt.Printf(messages.CONFIG_CREATE_HOME_ERROR, err) @@ -109,7 +111,8 @@ func initProject() { workingConfigFile := filepath.Join(workingConfigPath, ConfigFileNameWithExt) // If path is empty or config file does not exist, just return - if len(workingConfigPath) == 0 || workingConfigPath == HomeConfigPath || !fileutil.Exists(workingConfigFile) { + workingConfigExists, _ := fileutil.Exists(workingConfigFile) + if len(workingConfigPath) == 0 || workingConfigPath == HomeConfigPath || !workingConfigExists { return } @@ -179,13 +182,13 @@ func ProjectRoot() (string, error) { } // IsProjectDir returns a boolean depending on if path is a valid project dir -func IsProjectDir(path string) bool { +func IsProjectDir(path string) (bool, error) { configPath := filepath.Join(path, ConfigDir) configFile := filepath.Join(configPath, ConfigFileNameWithExt) // Home directory is not a project directory if HomePath == path { - return false + return false, nil } return fileutil.Exists(configFile) diff --git a/houston/houston.go b/houston/houston.go index 813e9a3f7..ca45910d7 100644 --- a/houston/houston.go +++ b/houston/houston.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io/ioutil" - "os" "strings" "github.com/pkg/errors" @@ -309,9 +308,9 @@ func (c *Client) QueryHouston(query string) (*HoustonResponse, error) { } if decode.Errors != nil { - fmt.Printf("Error: %s\n", decode.Errors[0].Message) - os.Exit(1) + return nil, errors.New("failed to successfully decode response") } + return &decode, nil } diff --git a/main.go b/main.go index d287c3226..e7eb49dcf 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,10 @@ import ( func main() { if err := cmd.RootCmd.Execute(); err != nil { + // if cmd.Debug { + // fmt.Printf("%+v", err) + // } + os.Exit(1) } } diff --git a/messages/messages.go b/messages/messages.go index c9cff7454..a1096f431 100644 --- a/messages/messages.go +++ b/messages/messages.go @@ -58,7 +58,6 @@ var ( HOUSTON_NO_DEPLOYMENTS_ERROR = "No airflow deployments found" HOUSTON_SELECT_DEPLOYMENT_PROMPT = "Select which airflow deployment you want to deploy to:" HOUSTON_OAUTH_REDIRECT = "Please visit the following URL, authenticate and paste token in next prompt\n" - HOUSTON_OAUTH_DISABLED = "OAuth is disabled, contact administrator or defer to basic auth\n" HOUSTON_INVALID_DEPLOYMENT_KEY = "Invalid deployment selection\n" HOUSTON_NO_USERS = "There are no users to list or you don't have permissions to list users that do exist" HOUSTON_WORKSPACE_CREATE_SUCCESS = "Successfully created %s (%s)\n" diff --git a/pkg/fileutil/files.go b/pkg/fileutil/files.go index f1582edaf..4275b62c5 100644 --- a/pkg/fileutil/files.go +++ b/pkg/fileutil/files.go @@ -1,30 +1,30 @@ package fileutil import ( - "fmt" "io" "os" "path/filepath" "strings" + + "github.com/pkg/errors" ) // Exists returns a boolean indicating if the given path already exists -func Exists(path string) bool { +func Exists(path string) (bool, error) { if path == "" { - return false + return false, nil } _, err := os.Stat(path) if err == nil { - return true + return true, nil } if !os.IsNotExist(err) { - fmt.Println(err) - os.Exit(1) + return false, errors.Wrap(err, "cannot determine if path exists, error ambiguous") } - return false + return false, nil } // WriteStringToFile write a string to a file diff --git a/pkg/fileutil/paths.go b/pkg/fileutil/paths.go index ee20f9298..4d5a4651e 100644 --- a/pkg/fileutil/paths.go +++ b/pkg/fileutil/paths.go @@ -8,26 +8,17 @@ import ( "path/filepath" homedir "github.com/mitchellh/go-homedir" + "github.com/pkg/errors" ) // GetWorkingDir returns the curent working directory -func GetWorkingDir() string { - work, err := os.Getwd() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - return work +func GetWorkingDir() (string, error) { + return os.Getwd() } // GetHomeDir returns the home directory -func GetHomeDir() string { - home, err := homedir.Dir() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - return home +func GetHomeDir() (string, error) { + return homedir.Dir() } // FindDirInPath walks up the current directory looking for the .astro folder @@ -35,7 +26,10 @@ func GetHomeDir() string { // https://github.com/astronomerio/astro-cli/issues/103 func FindDirInPath(search string) (string, error) { // Start in our current directory - workingDir := GetWorkingDir() + workingDir, err := GetWorkingDir() + if err != nil { + return "", err + } // Recursively walk up the filesystem tree for true { @@ -45,12 +39,20 @@ func FindDirInPath(search string) (string, error) { } // If searching home path, stop at home root - if workingDir == GetHomeDir() { - return "", nil + homeDir, err := GetHomeDir() + if err != nil { + return "", err + } + + if workingDir == homeDir { + return "", errors.New("current working directory is a home directory") } // Check if our file exists - exists := Exists(filepath.Join(workingDir, search)) + exists, err := Exists(filepath.Join(workingDir, search)) + if err != nil { + return "", errors.Wrapf(err, "failed to check existence of '%s'", filepath.Join(workingDir, search)) + } // Return where we found it if exists {