Skip to content

Commit

Permalink
Allow to disregard gitignore file
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmik committed Jul 31, 2023
1 parent d3eec79 commit e3f62ef
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
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

0 comments on commit e3f62ef

Please sign in to comment.