Skip to content

Comments

Adds project-name as an optional argument for project initialization#1743

Merged
schnie merged 6 commits intomainfrom
feature/project-name-argument
Dec 9, 2024
Merged

Adds project-name as an optional argument for project initialization#1743
schnie merged 6 commits intomainfrom
feature/project-name-argument

Conversation

@schnie
Copy link
Member

@schnie schnie commented Nov 12, 2024

Description

This PR just allows the project name to be specified as an optional, positional argument. When used this way, we set the argument as the project name, and then additionally create a directory with the same name, and initialize a fresh project in that new directory. This change is purely additive, and existing commands and flags work the same as before.

Current

mkdir test-project
cd test-project
astro dev init --name test-project

New

astro dev init test-project
cd test-project

Example Usage

This change specifically:

❯ astro dev init etl
Initialized empty Astro project in /Users/schnie/repos/etl
To begin developing, change to your project directory with `cd etl`

Combined with #1728:

❯ astro dev init etl --from-template=etl
Initialized empty Astro project in /Users/schnie/repos/etl
To begin developing, change to your project directory with `cd etl`

🎟 Issue(s)

Resolves to https://github.com/astronomer/astro/issues/25473

🧪 Functional Testing

❯ astro dev init test-project
Initialized empty Astro project in /Users/schnie/repos/test-project
To begin developing, change to your project directory with `cd /Users/schnie/repos/test-project`

📸 Screenshots

Add screenshots to illustrate the validity of these changes.

📋 Checklist

  • Rebased from the main (or release if patching) branch (before testing)
  • Ran make test before taking out of draft
  • Ran make lint before taking out of draft
  • Added/updated applicable tests
  • Tested against Astro-API (if necessary).
  • Tested against Houston-API and Astronomer (if necessary).
  • Communicated to/tagged owners of respective clients potentially impacted by these changes.
  • Updated any related documentation

@schnie schnie force-pushed the feature/project-name-argument branch from f93f446 to 31687d3 Compare November 12, 2024 19:07
Comment on lines -549 to +570
exists := config.ProjectConfigExists()
exists, err := config.IsProjectDir(config.WorkingPath)
if err != nil {
return err
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative function we already had to check if the project config file exists in the directory or not. The prior version assumes that the process is already within the working directory, where the alternative lets us check if the file exists at the certain path.

@schnie schnie changed the title Adds project-name as an optional argument for init [WIP] Adds project-name as an optional argument for init Nov 12, 2024
@schnie schnie changed the title [WIP] Adds project-name as an optional argument for init Adds project-name as an optional argument for project initialization Dec 6, 2024
@schnie schnie force-pushed the feature/project-name-argument branch from 2beaf76 to c840b7f Compare December 6, 2024 16:57
Comment on lines -557 to +580
fmt.Printf(configReinitProjectConfigMsg+"\n", config.WorkingPath)
fmt.Printf(configReinitProjectConfigMsg, config.WorkingPath)
} else {
fmt.Printf(configInitProjectConfigMsg+"\n", config.WorkingPath)
fmt.Printf(configInitProjectConfigMsg, config.WorkingPath)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just standardized the messages and the format strings and fixed a double vs single newline inconsistency.

Comment on lines -303 to -316
s.Run("invalid args", func() {
cmd := newAirflowInitCmd()
cmd.Flag("name").Value.Set("test-project-name")
args := []string{"invalid-arg"}

r, stdin := s.mockUserInput("y")

// Restore stdin right after the test.
defer func() { os.Stdin = stdin }()
os.Stdin = r
err := airflowInit(cmd, args)
s.ErrorIs(err, errProjectNameSpaces)
})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is obsolete now that we accept a single positional argument. Cobra also will show help text if more than one positional argument is specified.

Comment on lines -520 to -522
fmt.Printf("Initializing Astro project\nPulling Airflow development files from Astronomer Certified Airflow Version %s\n", defaultImageTag)
} else {
fmt.Printf("Initializing Astro project\nPulling Airflow development files from Astro Runtime %s\n", defaultImageTag)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These messages are not necessary and duplicative. I've also updated the corresponding test for this output.

@schnie schnie force-pushed the feature/project-name-argument branch from c840b7f to f8b6b72 Compare December 6, 2024 17:04
errInvalidBothCustomImageandVersion = errors.New("you provided both a Custom image and a version. You have to provide only one of these to upgrade") //nolint

errConfigProjectName = errors.New("project name is invalid")
errProjectNameSpaces = errors.New("this project name is invalid, a project name cannot contain spaces. Try using '-' instead")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete error message. Cobra will show help text if more than one argument is specified.

@schnie schnie force-pushed the feature/project-name-argument branch 7 times, most recently from 8d649d8 to 69b2ea8 Compare December 6, 2024 19:24
@schnie schnie force-pushed the feature/project-name-argument branch from 69b2ea8 to 4e82a74 Compare December 6, 2024 19:28
@schnie schnie marked this pull request as ready for review December 6, 2024 19:35
@schnie schnie requested review from pritt20 and sam-awezec December 6, 2024 19:36
@lzdanski lzdanski removed their request for review December 6, 2024 21:06
Copy link
Contributor

@neel-astro neel-astro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine apart from one minor edge case :)

cmd/airflow.go Outdated
Comment on lines 472 to 474
if len(args) > 0 {
projectName = args[0]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might mean that if someone is trying astro dev init --name test project, would result in a project name as project when they might be looking for test project, should we at least warn and confirm if not error, when someone is setting both the flag and arg?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Updated the logic here to catch this case and throw an error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added a corresponding test.

Comment on lines +606 to +610
// If the project name is specified with the --name flag,
// it cannot be specified as a positional argument as well, so return an error.
if projectName != "" && len(args) > 0 {
return "", errConfigProjectNameSpecifiedTwice
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neel-astro added this case here to throw an error message if both the flag and positional argument are used.

I also separated all the new and existing "project name" logic into this new function to make the linter happy. :)

@schnie schnie merged commit b3db2fd into main Dec 9, 2024
@schnie schnie deleted the feature/project-name-argument branch December 9, 2024 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants