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

feat: Set environment variables with directory name #2

Merged
merged 1 commit into from
Jun 11, 2023
Merged
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
28 changes: 24 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,28 @@ func enterAndCreate(path string) (bool, error) {
}

// Run a command
func run(args ...string) error {
cmd := exec.Command(args[0], args[1:]...)
func run(directory string, args ...string) error {
var err error
commandName := args[0]

absDir, err := filepath.Abs(directory)
if err != nil {
return err
}

if strings.HasPrefix(commandName, ".") {
commandName, err = filepath.Abs(commandName)
if err != nil {
return err
}
}

cmd := exec.Command(commandName, args[1:]...)
cmd.Dir = absDir

cmd.Env = append(cmd.Environ(), "PWD="+absDir)
cmd.Env = append(cmd.Environ(), "INDIR="+absDir)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
Expand Down Expand Up @@ -76,7 +96,7 @@ func runInAllMatching(pattern, startDir string) error {
log.Fatalln(err)
}
// run the given command
if err := run(os.Args[2:]...); err != nil {
if err := run(directory, os.Args[2:]...); err != nil {
log.Fatalln(err) // exit(1) and skip the deferred function
}
}
Expand Down Expand Up @@ -113,7 +133,7 @@ func main() {
log.Fatalln(err)
}
// run the given command
if err := run(os.Args[2:]...); err != nil {
if err := run(dirName, os.Args[2:]...); err != nil {
if ok { // remove the created directory, if it's empty
os.Remove(filepath.Join(startDir, dirName))
}
Expand Down