Skip to content

Commit

Permalink
add PreRun hooks to check for project dir
Browse files Browse the repository at this point in the history
  • Loading branch information
andscoop committed Mar 6, 2018
1 parent a842c99 commit 417c3da
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 38 deletions.
60 changes: 31 additions & 29 deletions cmd/airflow.go
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -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,
}
)

Expand Down Expand Up @@ -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)
}
}

Expand Down
39 changes: 30 additions & 9 deletions cmd/config.go
Expand Up @@ -2,35 +2,37 @@ package cmd

import (
"fmt"
"os"

"github.com/astronomerio/astro-cli/config"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

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{
Use: "get",
Short: "Get astro project configuration",
Long: "Get astro project configuration",
RunE: configGet,
// TODO add arg validator
// TODO check project specific config
}

configSetCmd = &cobra.Command{
Use: "set",
Short: "Set astro project configuration",
Long: "Set astro project configuration",
RunE: configSet,
// TODO add arg validator
// TODO check project specific config
}
)

Expand All @@ -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")
Expand All @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 417c3da

Please sign in to comment.