Skip to content

Commit

Permalink
Relocate astro airflow deploy command (#233)
Browse files Browse the repository at this point in the history
* deploy subcommand moved (copied for now) to the root

* Improved code a little bit

* Add deprication message
  • Loading branch information
andriisoldatenko authored and schnie committed Jul 10, 2019
1 parent 3a2ee29 commit 7c6a5f5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 33 deletions.
38 changes: 5 additions & 33 deletions cmd/airflow.go
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/astronomer/astro-cli/airflow"
"github.com/astronomer/astro-cli/config"
"github.com/astronomer/astro-cli/pkg/fileutil"
"github.com/astronomer/astro-cli/pkg/git"
)

var (
Expand Down Expand Up @@ -57,7 +56,10 @@ astro airflow run create_user -r Admin -u admin -e admin@example.com -f admin -l
Long: "Deploy an airflow project to a given deployment",
Args: cobra.MaximumNArgs(1),
PreRunE: ensureProjectDir,
RunE: airflowDeploy,
Run: func(cmd *cobra.Command, args []string) {
deployCmd.Run(cmd, args)
},
Deprecated: "Please use new command instead `astro deploy DEPLOYMENT [flags]`",
}

airflowStartCmd = &cobra.Command{
Expand Down Expand Up @@ -123,7 +125,7 @@ func init() {

// Airflow deploy
airflowRootCmd.AddCommand(airflowDeployCmd)
airflowDeployCmd.Flags().BoolVarP(&forceDeploy, "force", "f", false, "Force deploy if uncommited changes")
airflowDeployCmd.Flags().BoolVarP(&forceDeploy, "force", "f", false, "Force deploy if uncommitted changes")
airflowDeployCmd.Flags().BoolVarP(&forcePrompt, "prompt", "p", false, "Force prompt to choose target deployment")
airflowDeployCmd.Flags().BoolVarP(&saveDeployConfig, "save", "s", false, "Save deployment in config for future deploys")
airflowDeployCmd.Flags().StringVar(&workspaceId, "workspace-id", "", "workspace assigned to deployment")
Expand Down Expand Up @@ -228,36 +230,6 @@ func airflowInit(cmd *cobra.Command, args []string) error {
return nil
}

func airflowDeploy(cmd *cobra.Command, args []string) error {
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 := ""

// Get release name from args, if passed
if len(args) > 0 {
releaseName = args[0]
}

// Save releasename in config if specified
if len(releaseName) > 0 && saveDeployConfig {
config.CFG.ProjectDeployment.SetProjectString(releaseName)
}

if git.HasUncommitedChanges() && !forceDeploy {
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
Expand Down
62 changes: 62 additions & 0 deletions cmd/deploy.go
@@ -0,0 +1,62 @@
package cmd

import (
"fmt"

"github.com/astronomer/astro-cli/airflow"
"github.com/astronomer/astro-cli/config"
"github.com/astronomer/astro-cli/messages"
"github.com/astronomer/astro-cli/pkg/git"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var (
deployCmd = &cobra.Command{
Use: "deploy DEPLOYMENT",
Short: "Deploy an airflow project",
Long: "Deploy an airflow project to a given deployment",
Args: cobra.MaximumNArgs(1),
PreRunE: ensureProjectDir,
RunE: deploy,
Aliases: []string{"airflow deploy"},
}
)

func init() {
RootCmd.AddCommand(deployCmd)
deployCmd.Flags().BoolVarP(&forceDeploy, "force", "f", false, "Force deploy if uncommitted changes")
deployCmd.Flags().BoolVarP(&forcePrompt, "prompt", "p", false, "Force prompt to choose target deployment")
deployCmd.Flags().BoolVarP(&saveDeployConfig, "save", "s", false, "Save deployment in config for future deploys")
deployCmd.Flags().StringVar(&workspaceId, "workspace-id", "", "workspace assigned to deployment")
}

func deploy(cmd *cobra.Command, args []string) error {
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 := ""

// Get release name from args, if passed
if len(args) > 0 {
releaseName = args[0]
}

// Save release name in config if specified
if len(releaseName) > 0 && saveDeployConfig {
config.CFG.ProjectDeployment.SetProjectString(releaseName)
}

if git.HasUncommitedChanges() && !forceDeploy {
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)
}

0 comments on commit 7c6a5f5

Please sign in to comment.