Skip to content

Commit

Permalink
feat: dry run mode
Browse files Browse the repository at this point in the history
Closes #37
  • Loading branch information
scop committed Nov 18, 2023
1 parent b6d39aa commit 71590f4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ $ wrun -help
Usage of wrun:
-archive-exe-path value
[OS/arch=]path to executable within archive matcher (separator always /, implies archive processing)
-dry-run
Dry run, skip execution (but do download/set up cache)
-http-timeout duration
HTTP client timeout (default 5m0s)
-url value
Expand Down
24 changes: 21 additions & 3 deletions wrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ type config struct {
archiveExePathMatches []archiveExePathMatch
usePreCommitCache bool
httpTimeout time.Duration
dryRun bool
done bool
}

Expand Down Expand Up @@ -232,6 +233,7 @@ func parseFlags(set *flag.FlagSet, args []string) (config, error) {
cfg.done = true
return nil
})
set.BoolVar(&cfg.dryRun, "dry-run", false, "Dry run, skip execution (but do download/set up cache)")
if err := set.Parse(args); err != nil {
return config{}, err
}
Expand Down Expand Up @@ -457,6 +459,15 @@ Environment variables:
args := make([]string, len(flagSet.Args())+1)
args[0] = exe
copy(args[1:], flagSet.Args())
if cfg.dryRun {
infoOut("exec (...not, stat due to dry-run): %v", args)
if fi, statErr := os.Stat(exe); statErr != nil {
return statErr
} else if !fi.Mode().IsRegular() {
return fmt.Errorf("not a regular file: %v", exe)
}
return nil
}
infoOut("exec: %v", args)
return syscall.Exec(exe, args, os.Environ())
}
Expand All @@ -467,6 +478,10 @@ Environment variables:
} else {
warnOut("exec cached: %v", err)
}
} else if cfg.dryRun {
return
} else {
panic("BUG: unreachable; successful non-dry-run cache exec")
}

// Set up tempfile for download
Expand Down Expand Up @@ -602,7 +617,10 @@ Environment variables:
// Execute

cleanUpTempFile() // Note: deferred cleanup does not happen if we exec successfully
err = exec(exePath)
errorOut("exec: %v", err)
rc = 1
if err = exec(exePath); err != nil {
errorOut("exec: %v", err)
rc = 1
} else if !cfg.dryRun {
panic("BUG: unreachable; successful non-dry-run exec")
}
}

0 comments on commit 71590f4

Please sign in to comment.