Skip to content

Commit

Permalink
add clean-ouput flag to inspect command (#1124)
Browse files Browse the repository at this point in the history
* add clean-ouput flag

* add flag to other commands

* fix test
  • Loading branch information
sunkickr authored Mar 28, 2023
1 parent 63a0700 commit 87f4c20
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
12 changes: 8 additions & 4 deletions cloud/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/astronomer/astro-cli/pkg/input"
"github.com/astronomer/astro-cli/pkg/printutil"
"github.com/astronomer/astro-cli/pkg/util"

"github.com/pkg/errors"
)

Expand All @@ -27,6 +28,7 @@ var (
errTimedOut = errors.New("timed out waiting for the deployment to become healthy")
// Monkey patched to write unit tests
createDeployment = Create
CleanOutput = false
)

const (
Expand Down Expand Up @@ -579,9 +581,11 @@ var SelectDeployment = func(deployments []astro.Deployment, message string) (ast
}

if len(deployments) == 1 {
fmt.Println("Only one Deployment was found. Using the following Deployment by default: \n" +
fmt.Sprintf("\n Deployment Name: %s", ansi.Bold(deployments[0].Label)) +
fmt.Sprintf("\n Deployment ID: %s\n", ansi.Bold(deployments[0].ID)))
if !CleanOutput {
fmt.Println("Only one Deployment was found. Using the following Deployment by default: \n" +
fmt.Sprintf("\n Deployment Name: %s", ansi.Bold(deployments[0].Label)) +
fmt.Sprintf("\n Deployment ID: %s\n", ansi.Bold(deployments[0].ID)))
}

return deployments[0], nil
}
Expand Down Expand Up @@ -621,7 +625,7 @@ func GetDeployment(ws, deploymentID, deploymentName string, client astro.Client)
return astro.Deployment{}, errors.Wrap(err, errInvalidDeployment.Error())
}

if deploymentID != "" && deploymentName != "" {
if deploymentID != "" && deploymentName != "" && !CleanOutput {
fmt.Printf("Both a Deployment ID and Deployment name have been supplied. The Deployment ID %s will be used\n", deploymentID)
}
// find deployment by name
Expand Down
8 changes: 8 additions & 0 deletions cmd/cloud/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func newDeploymentCreateCmd(out io.Writer) *cobra.Command {
cmd.Flags().IntVarP(&schedulerAU, "scheduler-au", "s", deployment.SchedulerAuMin, "The Deployment's Scheduler resources in AUs")
cmd.Flags().IntVarP(&schedulerReplicas, "scheduler-replicas", "r", deployment.SchedulerReplicasMin, "The number of Scheduler replicas for the Deployment")
cmd.Flags().BoolVarP(&waitForStatus, "wait", "i", false, "Wait for the Deployment to become healthy before ending the command")
cmd.Flags().BoolVarP(&cleanOutput, "clean-output", "", false, "clean output to only include inspect yaml or json file in any situation.")
return cmd
}

Expand All @@ -162,6 +163,7 @@ func newDeploymentUpdateCmd(out io.Writer) *cobra.Command {
cmd.Flags().BoolVarP(&forceUpdate, "force", "f", false, "Force update: Don't prompt a user before Deployment update")
cmd.Flags().StringVarP(&deploymentName, "deployment-name", "", "", "Name of the deployment to update")
cmd.Flags().StringVarP(&dagDeploy, "dag-deploy", "", "", "Enables DAG-only deploys for the deployment")
cmd.Flags().BoolVarP(&cleanOutput, "clean-output", "c", false, "clean output to only include inspect yaml or json file in any situation.")
return cmd
}

Expand Down Expand Up @@ -304,6 +306,9 @@ func deploymentCreate(cmd *cobra.Command, _ []string, out io.Writer) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true

// clean output
deployment.CleanOutput = cleanOutput

// set default executor if none was specified
if executor == "" {
executor = deployment.CeleryExecutor
Expand Down Expand Up @@ -348,6 +353,9 @@ func deploymentUpdate(cmd *cobra.Command, args []string, out io.Writer) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true

// clean output
deployment.CleanOutput = cleanOutput

// check if executor is valid
if !isValidExecutor(executor) {
return fmt.Errorf("%s is %w", executor, errInvalidExecutor)
Expand Down
7 changes: 7 additions & 0 deletions cmd/cloud/deployment_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloud
import (
"io"

"github.com/astronomer/astro-cli/cloud/deployment"
"github.com/astronomer/astro-cli/cloud/deployment/inspect"

"github.com/spf13/cobra"
Expand All @@ -11,6 +12,7 @@ import (
var (
outputFormat, requestedField string
template bool
cleanOutput bool
)

func newDeploymentInspectCmd(out io.Writer) *cobra.Command {
Expand All @@ -27,6 +29,7 @@ func newDeploymentInspectCmd(out io.Writer) *cobra.Command {
cmd.Flags().StringVarP(&outputFormat, "output", "o", "yaml", "Output format can be one of: yaml or json. By default the inspected deployment will be in YAML format.")
cmd.Flags().BoolVarP(&template, "template", "t", false, "Create a template from the deployment being inspected.")
cmd.Flags().StringVarP(&requestedField, "key", "k", "", "A specific key for the deployment. Use --key configuration.cluster_id to get a deployment's cluster id.")
cmd.Flags().BoolVarP(&cleanOutput, "clean-output", "c", false, "clean output to only include inspect yaml or json file in any situation.")
return cmd
}

Expand All @@ -41,5 +44,9 @@ func deploymentInspect(cmd *cobra.Command, args []string, out io.Writer) error {
if len(args) > 0 {
deploymentID = args[0]
}

// clean output
deployment.CleanOutput = cleanOutput

return inspect.Inspect(wsID, deploymentName, deploymentID, outputFormat, astroClient, out, requestedField, template)
}
8 changes: 5 additions & 3 deletions cmd/cloud/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func Setup(cmd *cobra.Command, args []string, client astro.Client, coreClient as
}

// run auth setup for any command that requires auth
apiKey, err := checkAPIKeys(client, coreClient, args)
apiKey, err := checkAPIKeys(client, coreClient, isDeploymentFile, args)
if err != nil {
return err
}
Expand Down Expand Up @@ -226,14 +226,16 @@ func refresh(refreshToken string, authConfig astro.AuthConfig) (TokenResponse, e
return tokenRes, nil
}

func checkAPIKeys(astroClient astro.Client, coreClient astrocore.CoreClient, args []string) (bool, error) {
func checkAPIKeys(astroClient astro.Client, coreClient astrocore.CoreClient, isDeploymentFile bool, args []string) (bool, error) {
// check os variables
astronomerKeyID := os.Getenv("ASTRONOMER_KEY_ID")
astronomerKeySecret := os.Getenv("ASTRONOMER_KEY_SECRET")
if astronomerKeyID == "" || astronomerKeySecret == "" {
return false, nil
}
fmt.Println("Using an Astro API key")
if !isDeploymentFile {
fmt.Println("Using an Astro API key")
}

// get authConfig
c, err := context.GetCurrentContext() // get current context
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloud/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestCheckAPIKeys(t *testing.T) {
assert.NoError(t, err)

// run CheckAPIKeys
_, err = checkAPIKeys(mockClient, mockCoreClient, []string{})
_, err = checkAPIKeys(mockClient, mockCoreClient, false, []string{})
assert.NoError(t, err)
})
}
Expand Down

0 comments on commit 87f4c20

Please sign in to comment.