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

Allow to disregard the gitignore file during local preview file packing #166

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/cmd/module/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ var flagVersion = &cli.StringFlag{
Usage: "Semver `version` for the module version. If not provided, the version " +
"from the configuration file will be used",
}

var flagDisregardGitignore = &cli.BoolFlag{
Name: "disregard-gitignore",
Usage: "[Optional] Disregard the .gitignore file when reading files in a directory",
}
7 changes: 6 additions & 1 deletion internal/cmd/module/local_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ func localPreview() cli.ActionFunc {

fp := filepath.Join(os.TempDir(), "spacectl", "local-workspace", fmt.Sprintf("%s.tar.gz", uploadMutation.UploadLocalWorkspace.ID))

matchFn, err := internal.GetIgnoreMatcherFn(ctx, nil)
ignoreFiles := []string{".terraformignore"}
if !cliCtx.IsSet(flagDisregardGitignore.Name) {
ignoreFiles = append(ignoreFiles, ".gitignore")
}

matchFn, err := internal.GetIgnoreMatcherFn(ctx, nil, ignoreFiles)
if err != nil {
return fmt.Errorf("couldn't analyze .gitignore and .terraformignore files")
}
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func Command() *cli.Command {
flagNoFindRepositoryRoot,
flagNoUpload,
flagRunMetadata,
flagDisregardGitignore,
},
Action: localPreview(),
Before: authenticated.Ensure,
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/stack/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@ var flagOverrideEnvVars = &cli.StringSliceFlag{
Name: "env-var-override",
Usage: "[Optional] Environment variables injected into the run at runtime, example: --env-var-override 'foo=bar,bar=baz'",
}

var flagDisregardGitignore = &cli.BoolFlag{
Name: "disregard-gitignore",
Usage: "[Optional] Disregard the .gitignore file when reading files in a directory",
}
7 changes: 6 additions & 1 deletion internal/cmd/stack/local_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ func localPreview() cli.ActionFunc {

fp := filepath.Join(os.TempDir(), "spacectl", "local-workspace", fmt.Sprintf("%s.tar.gz", uploadMutation.UploadLocalWorkspace.ID))

matchFn, err := internal.GetIgnoreMatcherFn(ctx, packagePath)
ignoreFiles := []string{".terraformignore"}
if !cliCtx.IsSet(flagDisregardGitignore.Name) {
ignoreFiles = append(ignoreFiles, ".gitignore")
}

matchFn, err := internal.GetIgnoreMatcherFn(ctx, nil, ignoreFiles)
if err != nil {
return fmt.Errorf("couldn't analyze .gitignore and .terraformignore files")
}
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func Command() *cli.Command {
flagNoUpload,
flagOverrideEnvVars,
flagOverrideEnvVarsTF,
flagDisregardGitignore,
},
Action: localPreview(),
Before: authenticated.Ensure,
Expand Down
28 changes: 15 additions & 13 deletions internal/local_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,27 @@ func MoveToRepositoryRoot() error {
// GetIgnoreMatcherFn creates an ignore-matcher for archiving purposes
// This function respects gitignore and terraformignore, and
// optionally if a projectRoot is provided it only include files from this root
func GetIgnoreMatcherFn(ctx context.Context, projectRoot *string) (func(filePath string) bool, error) {
gitignore, err := ignore.CompileIgnoreFile(".gitignore")
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("couldn't compile .gitignore file: %w", err)
}
terraformignore, err := ignore.CompileIgnoreFile(".terraformignore")
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("couldn't compile .terraformignore file: %w", err)
func GetIgnoreMatcherFn(ctx context.Context, projectRoot *string, ignoreFiles []string) (func(filePath string) bool, error) {
ignoreList := make([]*ignore.GitIgnore, 0)
for _, f := range ignoreFiles {
ignoreFile, err := ignore.CompileIgnoreFile(f)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("couldn't compile %s file: %w", f, err)
}

ignoreList = append(ignoreList, ignoreFile)
}

customignore := ignore.CompileIgnoreLines(".git", ".terraform")
return func(filePath string) bool {
if customignore.MatchesPath(filePath) {
return false
}
if gitignore != nil && gitignore.MatchesPath(filePath) {
return false
}
if terraformignore != nil && terraformignore.MatchesPath(filePath) {
return false

for _, v := range ignoreList {
if v != nil && v.MatchesPath(filePath) {
return false
}
}

if projectRoot != nil {
Expand Down