Skip to content

Commit

Permalink
Merge pull request #117 from astronomerio/feature/astro-cli-97-cleanu…
Browse files Browse the repository at this point in the history
…p-error-handling

Feature/astro cli 97 cleanup error handling
  • Loading branch information
andscoop committed Sep 5, 2018
2 parents e8c0d34 + 017926b commit bbcce3f
Show file tree
Hide file tree
Showing 19 changed files with 276 additions and 150 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.New("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

0 comments on commit bbcce3f

Please sign in to comment.