Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support score-compose init subcommand #57

Merged
merged 4 commits into from
Mar 5, 2024
Merged

Conversation

astromechza
Copy link
Member

The init subcommand makes it easier to get started with Score compose!

$ score-compose init --help
The init subcommand will prepare the current directory for working with score-compose and prepare any local
files or configuration needed to be successful.

A directory named .score-compose will be created if it doesn't exist. This file stores local state and generally should
not be checked into source control. Add it to your .gitignore file if you use Git as version control.

The project name will be used as a Docker compose project name when the final compose files are written. This name
acts as a namespace when multiple score files and containers are used.

Usage:
  score-compose init [flags]

Examples:

  # Define a score file to generate
  score-compose init --file score2.yaml

  # Or override the docker compose project name
  score-compose init --project score-compose2

Flags:
  -f, --file string      The initial score file to ensure exists (default "./score.yaml")
  -h, --help             help for init
  -p, --project string   The name of the docker compose project

Global Flags:
      --quiet           Mute any logging output
  -v, --verbose count   Increase log verbosity and detail by specifying this flag one or more times

And when run:

$ score-compose ../cmd/score-compose/main.go init
INFO: Writing new state directory '.score-compose'
INFO: Initial Score file './score.yaml' does not exist - creating it
INFO: Read more about the Score specification at https://docs.score.dev/docs/

This dumps out a score file that looks like:

image

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • New chore (expected functionality to be implemented)

Checklist:

  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • I've signed off with an email address that matches the commit author.

Base automatically changed from warn-and-reject to main March 5, 2024 08:48
// LoadStateDirectory loads the state directory for the given directory (usually PWD).
func LoadStateDirectory(directory string) (*StateDirectory, bool, error) {
d := filepath.Join(directory, DefaultRelativeStateDirectory)
if st, err := os.Stat(d); err != nil {

Choose a reason for hiding this comment

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

In general, it is not considered good practice to check for a file before opening - rather favour attempting to open the file and then check the failure there. This is to avoid race conditions for file opening i.e. https://groups.google.com/g/golang-nuts/c/Ayx-BMNdMFo/m/4rL8FFHr8v4J

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed up :) now just doing a direct read with error handling.

}
}

if st, err := os.Stat(initCmdScoreFile); err != nil {

Choose a reason for hiding this comment

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

In general, it is not considered good practice to check for a file before opening - rather favour attempting to open the file and then check the failure there. This is to avoid race conditions for file opening i.e. https://groups.google.com/g/golang-nuts/c/Ayx-BMNdMFo/m/4rL8FFHr8v4J

In this case, using os.O_CREATE with os.O_EXCL will be an error if the file already exists.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed up :) now just doing a direct read with error handling and then a write if it doesn't exist.

Copy link

@chrishumanitec chrishumanitec left a comment

Choose a reason for hiding this comment

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

LGTM

@astromechza astromechza merged commit 87f7c72 into main Mar 5, 2024
3 checks passed
@astromechza astromechza deleted the init-subcommand branch March 5, 2024 21:29
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.

2 participants