Skip to content

Commit

Permalink
Fix flags in run-locally
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Feb 2, 2024
1 parent e89e2a6 commit 010ff65
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 40 deletions.
5 changes: 1 addition & 4 deletions internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func getProject() (*project.Project, error) {
opts = append(
opts,
project.WithIgnoreFilePatterns(fProjectIgnorePatterns...),
project.WithRespectGitignore(fRespectGitignore),
)

// By default, commands try to find repo upward unless project is non-empty.
Expand All @@ -69,10 +70,6 @@ func getProject() (*project.Project, error) {
)
}

if fRespectGitignore {
opts = append(opts, project.WithRespectGitignore())
}

if fLoadEnv && fEnvOrder != nil {
opts = append(opts, project.WithEnvFilesReadOrder(fEnvOrder))
}
Expand Down
56 changes: 31 additions & 25 deletions internal/cmd/run_locally.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,16 @@ The --category option additionally filters the list of tasks to execute.`,
if err != nil {
return err
}
logger.Info("found tasks", zap.Int("count", len(tasks)))

logger.Debug("found tasks", zap.Int("count", len(tasks)))
tasks = filterTasksByCategory(tasks, category)
logger.Info("filtered tasks by category", zap.String("category", category), zap.Int("count", len(tasks)))

if category != "" {
tasks = filterTasksByCategory(tasks, category)
logger.Debug("filtered tasks by category", zap.String("category", category), zap.Int("count", len(tasks)))
}

if len(args) > 0 {
globs := make([]glob.Glob, 0, len(args))
for _, arg := range args {
g, err := glob.Compile(arg)
if err != nil {
return err
}
globs = append(globs, g)
}

tasks, err = filterTasksByGlobs(tasks, globs)
if err != nil {
return err
}
logger.Debug("filtered tasks by globs", zap.Strings("names", args), zap.Int("count", len(tasks)))
tasks, err = filterTasksByGlobs(tasks, args)
if err != nil {
return err
}
logger.Info("filtered tasks by globs", zap.Strings("globs", args), zap.Int("count", len(tasks)))

for _, t := range tasks {
err := runCommandNatively(cmd, t.CodeBlock, session, logger)
Expand Down Expand Up @@ -98,20 +84,23 @@ func runCommandNatively(cmd *cobra.Command, block *document.CodeBlock, sess *com
Logger: logger,
}

nativeCmd, err := command.NewNative(cfg, opts)
nativeCommand, err := command.NewNative(cfg, opts)
if err != nil {
return err
}

err = nativeCmd.Start(cmd.Context())
err = nativeCommand.Start(cmd.Context())
if err != nil {
return err
}

return nativeCmd.Wait()
return nativeCommand.Wait()
}

func filterTasksByCategory(tasks []project.Task, category string) (result []project.Task) {
if category == "" {
return tasks
}
for _, t := range tasks {
if t.CodeBlock.Category() == category {
result = append(result, t)
Expand All @@ -120,7 +109,24 @@ func filterTasksByCategory(tasks []project.Task, category string) (result []proj
return
}

func filterTasksByGlobs(tasks []project.Task, globs []glob.Glob) (result []project.Task, _ error) {
func parseGlobs(items []string) ([]glob.Glob, error) {
globs := make([]glob.Glob, 0, len(items))
for _, item := range items {
g, err := glob.Compile(item)
if err != nil {
return nil, err
}
globs = append(globs, g)
}
return globs, nil
}

func filterTasksByGlobs(tasks []project.Task, patterns []string) (result []project.Task, _ error) {
globs, err := parseGlobs(patterns)
if err != nil {
return nil, err
}

for _, g := range globs {
match := false

Expand Down
4 changes: 2 additions & 2 deletions internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func ExtractDataFromLoadEvent[T any](event LoadEvent) T {

type ProjectOption func(*Project)

func WithRespectGitignore() ProjectOption {
func WithRespectGitignore(value bool) ProjectOption {
return func(p *Project) {
p.respectGitignore = true
p.respectGitignore = value
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestProjectLoad(t *testing.T) {
p, err := NewDirProject(
gitProjectDir,
WithFindRepoUpward(),
WithRespectGitignore(),
WithRespectGitignore(true),
WithIgnoreFilePatterns(".git.bkp"),
WithIgnoreFilePatterns(".gitignore.bkp"),
WithIgnoreFilePatterns("ignored.md"),
Expand Down
11 changes: 3 additions & 8 deletions internal/project/projectservice/project_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,9 @@ func (s *projectServiceServer) Load(req *projectv1.LoadRequest, srv projectv1.Pr
func projectFromReq(req *projectv1.LoadRequest) (*project.Project, error) {
switch v := req.GetKind().(type) {
case *projectv1.LoadRequest_Directory:
var opts []project.ProjectOption

if !v.Directory.SkipGitignore {
opts = append(opts, project.WithRespectGitignore())
}

if patterns := v.Directory.IgnoreFilePatterns; len(patterns) > 0 {
opts = append(opts, project.WithIgnoreFilePatterns(patterns...))
opts := []project.ProjectOption{
project.WithRespectGitignore(!v.Directory.SkipGitignore),
project.WithIgnoreFilePatterns(v.Directory.IgnoreFilePatterns...),
}

if !v.Directory.SkipRepoLookupUpward {
Expand Down

0 comments on commit 010ff65

Please sign in to comment.