Skip to content

Commit

Permalink
Refactor to always use errors.Wrap()
Browse files Browse the repository at this point in the history
  • Loading branch information
andscoop committed Sep 4, 2018
1 parent e8c0d34 commit ad85406
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 132 deletions.
44 changes: 21 additions & 23 deletions airflow/airflow.go
@@ -1,66 +1,60 @@
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"
"github.com/astronomerio/astro-cli/messages"
"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
Expand All @@ -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
}
Expand Down
21 changes: 10 additions & 11 deletions airflow/docker.go
Expand Up @@ -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{
Expand All @@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -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.Wrap(err, "cannot start, project already running")
}
}
imageBuild(airflowHome, imageName(projectName, "latest"))
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions auth/auth.go
Expand Up @@ -2,14 +2,14 @@ package auth

import (
"fmt"
"os"

"github.com/astronomerio/astro-cli/cluster"
"github.com/astronomerio/astro-cli/docker"
"github.com/astronomerio/astro-cli/houston"
"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 (
Expand All @@ -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 {
Expand Down Expand Up @@ -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 := ""
Expand All @@ -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)
}
Expand Down
92 changes: 53 additions & 39 deletions cmd/airflow.go
Expand Up @@ -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,
}
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -184,7 +194,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 := ""

Expand Down
4 changes: 3 additions & 1 deletion cmd/config.go
Expand Up @@ -50,7 +50,9 @@ 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)
Expand Down

0 comments on commit ad85406

Please sign in to comment.