Skip to content

Commit

Permalink
hide --use-astronomer-certified flag behind context (#554)
Browse files Browse the repository at this point in the history
* hide --use-astronomer-certified flag behind context

* fix tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
neel-astro and pre-commit-ci[bot] authored May 27, 2022
1 parent afb3b95 commit 9824a66
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
28 changes: 25 additions & 3 deletions cmd/airflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
airflowversions "github.com/astronomer/astro-cli/airflow_versions"
"github.com/astronomer/astro-cli/cmd/utils"
"github.com/astronomer/astro-cli/config"
"github.com/astronomer/astro-cli/context"
"github.com/astronomer/astro-cli/pkg/fileutil"
"github.com/astronomer/astro-cli/pkg/httputil"
"github.com/astronomer/astro-cli/pkg/input"
Expand All @@ -35,7 +36,7 @@ var (
# Create default admin user.
astro dev run users create -r Admin -u admin -e [email protected] -f admin -l user -p admin
`
initExample = `
initSoftwareExample = `
# Initialize a new Astro project with the latest version of Astro Runtime
astro dev init
Expand All @@ -50,6 +51,17 @@ astro dev init --use-astronomer-certified
# Initialize a new Astro project with the latest version of Astronomer Certified based on Airflow 2.2.3
astro dev init --use-astronomer-certified --airflow-version 2.2.3
`

initCloudExample = `
# Initialize a new Astro project with the latest version of Astro Runtime
astro dev init
# Initialize a new Astro project with Astro Runtime 4.1.0
astro dev init --runtime-version 4.1.0
# Initialize a new Astro project with the latest Astro Runtime version based on Airflow 2.2.3
astro dev init --airflow-version 2.2.3
`
dockerfile = "Dockerfile"

Expand Down Expand Up @@ -93,7 +105,7 @@ func newAirflowInitCmd() *cobra.Command {
Use: "init",
Short: "Create a new Astro project in your working directory",
Long: "Create a new Astro project in your working directory. This generates the files you need to start an Airflow environment on your local machine and deploy your project to a Deployment on Astro or Astronomer Software.",
Example: initExample,
Example: initCloudExample,
Args: cobra.MaximumNArgs(1),
// ignore PersistentPreRunE of root command
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -103,8 +115,18 @@ func newAirflowInitCmd() *cobra.Command {
}
cmd.Flags().StringVarP(&projectName, "name", "n", "", "Name of Astro project")
cmd.Flags().StringVarP(&runtimeVersion, "runtime-version", "v", "", "Specify a version of Astro Runtime that you want to create an Astro project with. If not specified, the latest is assumed. You can change this version in your Dockerfile at any time.")
cmd.Flags().BoolVarP(&useAstronomerCertified, "use-astronomer-certified", "", false, "If specified, initializes a project using Astronomer Certified Airflow image instead of Astro Runtime.")
cmd.Flags().StringVarP(&airflowVersion, "airflow-version", "a", "", "Version of Airflow you want to create an Astro project with. If not specified, latest is assumed. You can change this version in your Dockerfile at any time.")

if !context.IsCloudContext() {
cmd.Example = initSoftwareExample
cmd.Flags().BoolVarP(&useAstronomerCertified, "use-astronomer-certified", "", false, "If specified, initializes a project using Astronomer Certified Airflow image instead of Astro Runtime.")
}

_, err := context.GetCurrentContext()
if err != nil { // Case when user is not logged in to any platform
cmd.Flags().BoolVarP(&useAstronomerCertified, "use-astronomer-certified", "", false, "If specified, initializes a project using Astronomer Certified Airflow image instead of Astro Runtime.")
cmd.Flags().MarkHidden("use-astronomer-certified")
}
return cmd
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/airflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ func TestDevRootCommand(t *testing.T) {
assert.Contains(t, output, "astro dev", output)
}

func TestDevInitCommand(t *testing.T) {
testUtil.InitTestConfig(testUtil.CloudPlatform)
output, err := executeCommand("dev", "init", "--help")
assert.NoError(t, err)
assert.Contains(t, output, "astro dev", output)
assert.NotContains(t, output, "--use-astronomer-certified")

testUtil.InitTestConfig(testUtil.SoftwarePlatform)
output, err = executeCommand("dev", "init", "--help")
assert.NoError(t, err)
assert.Contains(t, output, "astro dev", output)
assert.Contains(t, output, "--use-astronomer-certified")
}

func TestNewAirflowInitCmd(t *testing.T) {
cmd := newAirflowInitCmd()
assert.Nil(t, cmd.PersistentPreRunE(new(cobra.Command), []string{}))
Expand Down Expand Up @@ -211,6 +225,7 @@ func TestAirflowInit(t *testing.T) {
assert.ErrorIs(t, err, errInvalidBothAirflowAndRuntimeVersions)
})

testUtil.InitTestConfig(testUtil.SoftwarePlatform)
t.Run("runtime version passed alongside AC flag", func(t *testing.T) {
cmd := newAirflowInitCmd()
cmd.Flag("name").Value.Set("test-project-name")
Expand Down

0 comments on commit 9824a66

Please sign in to comment.