Skip to content

Commit

Permalink
chore: split windows and unix specific run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Oct 12, 2023
1 parent a7895cb commit d0c8d46
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ cd cmd/templ && go build -o ~/bin/templ
Use goreleaser to build the command line binary using goreleaser.

```sh
goreleaser build --snapshot --rm-dist
goreleaser build --snapshot --clean
```

### generate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//go:build unix

package run

import (
"context"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
Expand All @@ -18,16 +18,6 @@ func KillAll() (err error) {
m.Lock()
defer m.Unlock()
for _, cmd := range running {
if runtime.GOOS == "windows" {
kill := exec.Command("TASKKILL", "/T", "/F", "/PID", strconv.Itoa(cmd.Process.Pid))
kill.Stderr = os.Stderr
kill.Stdout = os.Stdout
err := kill.Run()
if err != nil {
return err
}
continue
}
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
if err != nil {
return err
Expand All @@ -41,16 +31,6 @@ func Stop() (err error) {
m.Lock()
defer m.Unlock()
for _, cmd := range running {
if runtime.GOOS == "windows" {
kill := exec.Command("TASKKILL", "/T", "/F", "/PID", strconv.Itoa(cmd.Process.Pid))
kill.Stderr = os.Stderr
kill.Stdout = os.Stdout
err := kill.Run()
if err != nil {
return err
}
continue
}
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
if err != nil {
return err
Expand Down
74 changes: 74 additions & 0 deletions cmd/templ/generatecmd/run/run_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//go:build windows

package run

import (
"context"
"os"
"os/exec"
"strconv"
"strings"
"sync"
)

var m = &sync.Mutex{}
var running = map[string]*exec.Cmd{}

func KillAll() (err error) {
m.Lock()
defer m.Unlock()
for _, cmd := range running {
kill := exec.Command("TASKKILL", "/T", "/F", "/PID", strconv.Itoa(cmd.Process.Pid))
kill.Stderr = os.Stderr
kill.Stdout = os.Stdout
err := kill.Run()
if err != nil {
return err
}
}
running = map[string]*exec.Cmd{}
return
}

func Stop() (err error) {
m.Lock()
defer m.Unlock()
for _, cmd := range running {
kill := exec.Command("TASKKILL", "/T", "/F", "/PID", strconv.Itoa(cmd.Process.Pid))
kill.Stderr = os.Stderr
kill.Stdout = os.Stdout
err := kill.Run()
if err != nil {
return err
}
}
running = map[string]*exec.Cmd{}
return
}

func Run(ctx context.Context, workingDir, input string) (cmd *exec.Cmd, err error) {
cmd, ok := running[input]
if ok {
if err = Stop(); err != nil {
return
}
delete(running, input)
}
m.Lock()
defer m.Unlock()
parts := strings.Fields(input)
executable := parts[0]
args := []string{}
if len(parts) > 1 {
args = append(args, parts[1:]...)
}

cmd = exec.Command(executable, args...)
cmd.Env = os.Environ()
cmd.Dir = workingDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
running[input] = cmd
err = cmd.Start()
return
}

0 comments on commit d0c8d46

Please sign in to comment.