Skip to content

Commit b4edd4f

Browse files
committed
feat: properly kills the running process on KeyboardInterrupt
1 parent dcc5129 commit b4edd4f

File tree

6 files changed

+92
-67
lines changed

6 files changed

+92
-67
lines changed

cmd/run/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ func main() {
220220
go func() {
221221
<-ctx.Done()
222222
cf()
223-
os.Exit(1)
224223
}()
225224

226225
if err := cmd.Run(ctx, os.Args); err != nil {

examples/Runfile.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ tasks:
2323
sh: echo -n "hello"
2424
k4:
2525
required: true
26-
k5:
27-
default:
28-
# value: "this is default value"
29-
# sh: echo this should be the default value
30-
gotmpl: len "asdfadf"
26+
# k5:
27+
# default:
28+
# # value: "this is default value"
29+
# # sh: echo this should be the default value
30+
# gotmpl: len "asdfadf"
3131
# dotenv:
3232
# - ../.secrets/env
3333
cmd:
3434
# - sleep 5
35-
# - echo "hi hello"
36-
# - echo "value of k1 is '$k1'"
37-
# - echo "value of k2 is '$k2'"
38-
# - echo "value of k3 is '$k3'"
35+
- echo "hi hello"
36+
- echo "value of k1 is '$k1'"
37+
- echo "value of k2 is '$k2'"
38+
- echo "value of k3 is '$k3'"
3939
- echo "hello from cook"
4040
- echo "value of key_id (from .dotenv) is '$key_id', ${#key_id}"
4141
- echo "k4 is $k4"
42-
- echo "k5 is $k5"
42+
# - echo "k5 is $k5"
4343

4444
clean:
4545
name: clean
@@ -62,11 +62,11 @@ tasks:
6262
laundry:
6363
name: laundry
6464
shell: ["node", "-e"]
65-
# env:
66-
# k4:
67-
# default:
68-
# sh: |+
69-
# console.log('1234' == '23344')
65+
env:
66+
k4:
67+
default:
68+
sh: |+
69+
console.log('1234' == '23344')
7070
cmd:
7171
- run: cook
7272
- console.log(process.env.k4)

parser/parse-task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func ParseTask(ctx context.Context, prf *types.ParsedRunfile, task types.Task) (
3737
Env: prf.Env,
3838
})
3939
if err != nil {
40-
return nil, err
40+
return nil, errors.WithErr(err)
4141
}
4242

4343
for k, v := range tenv {

runner/run-task.go

Lines changed: 74 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package runner
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67
"os"
8+
"os/exec"
79
"strings"
810

911
"github.com/alecthomas/chroma/quick"
1012
"github.com/charmbracelet/lipgloss"
1113
"github.com/muesli/termenv"
14+
"github.com/nxtcoder17/fwatcher/pkg/executor"
1215
"github.com/nxtcoder17/runfile/errors"
1316
fn "github.com/nxtcoder17/runfile/functions"
1417
"github.com/nxtcoder17/runfile/parser"
@@ -25,6 +28,26 @@ func isDarkTheme() bool {
2528
return termenv.NewOutput(os.Stdout).HasDarkBackground()
2629
}
2730

31+
func padString(v string, padWith string) string {
32+
sp := strings.Split(v, "\n")
33+
for i := range sp {
34+
if i == 0 {
35+
sp[i] = fmt.Sprintf("%s | %s", padWith, sp[i])
36+
continue
37+
}
38+
sp[i] = fmt.Sprintf("%s | %s", strings.Repeat(" ", len(padWith)), sp[i])
39+
}
40+
41+
return strings.Join(sp, "\n")
42+
}
43+
44+
// [snippet source](https://rderik.com/blog/identify-if-output-goes-to-the-terminal-or-is-being-redirected-in-golang/)
45+
func isTTY() bool {
46+
stdout, _ := os.Stdout.Stat()
47+
stderr, _ := os.Stderr.Stat()
48+
return ((stdout.Mode() & os.ModeCharDevice) == os.ModeCharDevice) && ((stderr.Mode() & os.ModeCharDevice) == os.ModeCharDevice)
49+
}
50+
2851
func runTask(ctx Context, prf *types.ParsedRunfile, args runTaskArgs) error {
2952
runfilePath := prf.Metadata.RunfilePath
3053
task := prf.Tasks[args.taskName]
@@ -47,7 +70,7 @@ func runTask(ctx Context, prf *types.ParsedRunfile, args runTaskArgs) error {
4770

4871
pt, err := parser.ParseTask(ctx, prf, task)
4972
if err != nil {
50-
return errors.ErrTaskParsingFailed.Wrap(err)
73+
return errors.WithErr(err)
5174
}
5275

5376
for _, command := range pt.Commands {
@@ -64,7 +87,7 @@ func runTask(ctx Context, prf *types.ParsedRunfile, args runTaskArgs) error {
6487
taskName: command.Run,
6588
envOverrides: pt.Env,
6689
}); err != nil {
67-
return errors.WithErr(err)
90+
return errors.WithErr(err).KV("env-vars", prf.Env)
6891
}
6992
continue
7093
}
@@ -114,60 +137,63 @@ func runTask(ctx Context, prf *types.ParsedRunfile, args runTaskArgs) error {
114137
// }()
115138
// }
116139

117-
borderColor := "#4388cc"
118-
if !isDarkTheme() {
119-
borderColor = "#3d5485"
120-
}
121-
s := lipgloss.NewStyle().BorderForeground(lipgloss.Color(borderColor)).PaddingLeft(1).PaddingRight(1).Border(lipgloss.RoundedBorder(), true, true, true, true)
140+
if isTTY() {
141+
borderColor := "#4388cc"
142+
if !isDarkTheme() {
143+
borderColor = "#3d5485"
144+
}
145+
s := lipgloss.NewStyle().BorderForeground(lipgloss.Color(borderColor)).PaddingLeft(1).PaddingRight(1).Border(lipgloss.RoundedBorder(), true, true, true, true)
122146

123-
hlCode := new(bytes.Buffer)
124-
// choose colorschemes from `https://swapoff.org/chroma/playground/`
125-
colorscheme := "catppuccin-macchiato"
126-
if !isDarkTheme() {
127-
colorscheme = "monokailight"
147+
hlCode := new(bytes.Buffer)
148+
// choose colorschemes from `https://swapoff.org/chroma/playground/`
149+
colorscheme := "catppuccin-macchiato"
150+
if !isDarkTheme() {
151+
colorscheme = "monokailight"
152+
}
153+
quick.Highlight(hlCode, strings.TrimSpace(command.Command), "bash", "terminal16m", colorscheme)
154+
155+
// fmt.Printf("%s\n", s.Render(args.taskName+" | "+hlCode.String()))
156+
fmt.Printf("%s\n", s.Render(padString(hlCode.String(), args.taskName)))
128157
}
129-
quick.Highlight(hlCode, strings.TrimSpace(command.Command), "bash", "terminal16m", colorscheme)
130-
131-
fmt.Printf("%s\n", s.Render(hlCode.String()))
132-
133-
cmd := CreateCommand(ctx, CmdArgs{
134-
Shell: pt.Shell,
135-
Env: fn.ToEnviron(pt.Env),
136-
Cmd: command.Command,
137-
WorkingDir: pt.WorkingDir,
138-
interactive: pt.Interactive,
139-
Stdout: os.Stdout,
140-
Stderr: os.Stderr,
141-
})
142158

143-
// ex := executor.NewExecutor(executor.ExecutorArgs{
144-
// Logger: logger,
145-
// Command: func(c context.Context) *exec.Cmd {
146-
// return CreateCommand(c, CmdArgs{
147-
// Shell: pt.Shell,
148-
// Env: fn.ToEnviron(pt.Env),
149-
// Cmd: command.Command,
150-
// WorkingDir: pt.WorkingDir,
151-
// interactive: pt.Interactive,
152-
// Stdout: os.Stdout,
153-
// Stderr: os.Stderr,
154-
// // stdout: stdoutW,
155-
// // stderr: stderrW,
156-
// })
157-
// },
159+
// cmd := CreateCommand(ctx, CmdArgs{
160+
// Shell: pt.Shell,
161+
// Env: fn.ToEnviron(pt.Env),
162+
// Cmd: command.Command,
163+
// WorkingDir: pt.WorkingDir,
164+
// interactive: pt.Interactive,
165+
// Stdout: os.Stdout,
166+
// Stderr: os.Stderr,
158167
// })
159168

160-
if err := cmd.Run(); err != nil {
161-
return errors.ErrTaskFailed.Wrap(err).KV("cmd", cmd.String())
162-
}
169+
// logger2 := logging.New(logging.Options{
170+
// Prefix: "executor",
171+
// ShowDebugLogs: true,
172+
// })
163173

164-
// stdoutW.Close()
165-
// stderrW.Close()
174+
ex := executor.NewExecutor(executor.ExecutorArgs{
175+
Logger: logger,
176+
Command: func(c context.Context) *exec.Cmd {
177+
return CreateCommand(c, CmdArgs{
178+
Shell: pt.Shell,
179+
Env: fn.ToEnviron(pt.Env),
180+
Cmd: command.Command,
181+
WorkingDir: pt.WorkingDir,
182+
interactive: pt.Interactive,
183+
Stdout: os.Stdout,
184+
Stderr: os.Stderr,
185+
})
186+
},
187+
})
166188

167-
// wg.Wait()
168-
// if err := ex.Exec(); err != nil {
169-
// return errors.ErrTaskFailed.Wrap(err).KV("task", args.taskName)
189+
// if err := cmd.Run(); err != nil {
190+
// return errors.ErrTaskFailed.Wrap(err).KV("cmd", cmd.String())
170191
// }
192+
193+
// wg.Wait()
194+
if err := ex.Exec(); err != nil {
195+
return errors.ErrTaskFailed.Wrap(err).KV("task", args.taskName)
196+
}
171197
}
172198

173199
return nil

runner/runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func Run(ctx Context, prf *types.ParsedRunfile, args RunArgs) error {
100100
tn := _tn
101101
g.Go(func() error {
102102
if err := runTask(ctx, prf, runTaskArgs{taskName: tn}); err != nil {
103-
return errors.ErrTaskFailed.Wrap(err).KV(attr(tn)...)
103+
return errors.WithErr(err).KV(attr(tn)...)
104104
}
105105
return nil
106106
})
@@ -116,7 +116,7 @@ func Run(ctx Context, prf *types.ParsedRunfile, args RunArgs) error {
116116

117117
for _, tn := range args.Tasks {
118118
if err := runTask(ctx, prf, runTaskArgs{taskName: tn}); err != nil {
119-
return errors.ErrTaskFailed.Wrap(err).KV(attr(tn)...)
119+
return errors.WithErr(err).KV(attr(tn)...)
120120
}
121121
}
122122

0 commit comments

Comments
 (0)