-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Support selecting compose files with -f
#323
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package compose | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/arm/topo/internal/output/logger" | ||
| ) | ||
|
|
||
| var errFileNotFound = errors.New("compose file not found") | ||
|
|
||
| var defaultFileNames = []string{"compose.yaml", "compose.yml"} | ||
|
|
||
| // RequireFile returns the path when it exists and is not a directory. | ||
| func RequireFile(composeFile string) (string, error) { | ||
| info, err := os.Stat(composeFile) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| return "", fmt.Errorf("%w: %s", errFileNotFound, composeFile) | ||
| } | ||
| return "", fmt.Errorf("failed to access compose file %s: %w", composeFile, err) | ||
| } | ||
| if info.IsDir() { | ||
| return "", fmt.Errorf("compose file path is a directory: %s", composeFile) | ||
| } | ||
| return composeFile, nil | ||
| } | ||
|
|
||
| // FindDefaultFile returns the first existing default compose file. | ||
| func FindDefaultFile() (string, error) { | ||
| type defaultFileCandidate struct { | ||
| name string | ||
| info os.FileInfo | ||
| } | ||
| candidates := []defaultFileCandidate{} | ||
|
|
||
| for _, fileName := range defaultFileNames { | ||
| info, err := os.Stat(fileName) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| continue | ||
| } | ||
| return "", fmt.Errorf("failed to access compose file %s: %w", fileName, err) | ||
| } | ||
|
|
||
| candidates = append(candidates, defaultFileCandidate{ | ||
| name: fileName, | ||
| info: info, | ||
| }) | ||
| } | ||
|
|
||
| if len(candidates) == 0 { | ||
| return "", fmt.Errorf( | ||
| "compose file not found in current working directory: looking for %s", | ||
| strings.Join(defaultFileNames, " or "), | ||
| ) | ||
| } | ||
|
|
||
| defaultCandidate := candidates[0] | ||
| if len(candidates) > 1 { | ||
| logger.Warn(fmt.Sprintf( | ||
| "found multiple compose files: %s; using %s", | ||
| strings.Join(defaultFileNames, ", "), | ||
| defaultCandidate.name, | ||
| )) | ||
| } | ||
| if defaultCandidate.info.IsDir() { | ||
| return "", fmt.Errorf("compose file path is a directory: %s", defaultCandidate.name) | ||
| } | ||
| return defaultCandidate.name, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package compose_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/arm/topo/internal/compose" | ||
| "github.com/arm/topo/internal/output/logger" | ||
| "github.com/arm/topo/internal/output/term" | ||
| "github.com/arm/topo/internal/testutil" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRequireFile(t *testing.T) { | ||
| t.Run("returns path when compose file exists", func(t *testing.T) { | ||
| composeFile := filepath.Join(t.TempDir(), "compose.yaml") | ||
| testutil.RequireWriteFile(t, composeFile, "") | ||
|
|
||
| got, err := compose.RequireFile(composeFile) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, composeFile, got) | ||
| }) | ||
|
|
||
| t.Run("returns error when compose file does not exist", func(t *testing.T) { | ||
| composeFile := filepath.Join(t.TempDir(), "compose.yaml") | ||
|
|
||
| got, err := compose.RequireFile(composeFile) | ||
|
|
||
| require.Error(t, err) | ||
| assert.Empty(t, got) | ||
| assert.ErrorContains(t, err, "compose file not found") | ||
| assert.ErrorContains(t, err, composeFile) | ||
| }) | ||
|
|
||
| t.Run("returns error when compose file path is a directory", func(t *testing.T) { | ||
| composeFile := filepath.Join(t.TempDir(), "compose.yaml") | ||
| require.NoError(t, os.Mkdir(composeFile, 0o755)) | ||
|
|
||
| got, err := compose.RequireFile(composeFile) | ||
|
|
||
| require.Error(t, err) | ||
| assert.Empty(t, got) | ||
| assert.ErrorContains(t, err, "compose file path is a directory") | ||
| assert.ErrorContains(t, err, composeFile) | ||
| }) | ||
| } | ||
|
|
||
| func TestFindDefaultFile(t *testing.T) { | ||
| t.Run("returns compose.yaml when it exists", func(t *testing.T) { | ||
| t.Chdir(t.TempDir()) | ||
| testutil.RequireWriteFile(t, "compose.yaml", "") | ||
|
|
||
| got, err := compose.FindDefaultFile() | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, "compose.yaml", got) | ||
| }) | ||
|
|
||
| t.Run("falls back to compose.yml", func(t *testing.T) { | ||
| t.Chdir(t.TempDir()) | ||
| testutil.RequireWriteFile(t, "compose.yml", "") | ||
|
|
||
| got, err := compose.FindDefaultFile() | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, "compose.yml", got) | ||
| }) | ||
|
|
||
| t.Run("warns when multiple default compose files exist", func(t *testing.T) { | ||
| t.Chdir(t.TempDir()) | ||
| testutil.RequireWriteFile(t, "compose.yaml", "") | ||
| testutil.RequireWriteFile(t, "compose.yml", "") | ||
| var logOutput bytes.Buffer | ||
| logger.SetOptions(logger.Options{Output: &logOutput, Format: term.Plain}) | ||
| t.Cleanup(func() { | ||
| logger.SetOptions(logger.Options{}) | ||
| }) | ||
|
|
||
| got, err := compose.FindDefaultFile() | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, "compose.yaml", got) | ||
| assert.Contains(t, logOutput.String(), "found multiple compose files: compose.yaml, compose.yml; using compose.yaml") | ||
| }) | ||
|
|
||
| t.Run("returns error when no default compose file exists", func(t *testing.T) { | ||
| t.Chdir(t.TempDir()) | ||
|
|
||
| got, err := compose.FindDefaultFile() | ||
|
|
||
| require.Error(t, err) | ||
| assert.Empty(t, got) | ||
| assert.EqualError(t, err, "compose file not found in current working directory: looking for compose.yaml or compose.yml") | ||
| }) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL.
flag.Changed is a bool; where if it's set (to anything other than it's default) it's true?