Skip to content

Commit e3477d9

Browse files
committed
feat: improved command execution, and controls
- revamped executor model in [fwatcher](nxtcoder17/fwatcher#8)
1 parent 117853c commit e3477d9

File tree

11 files changed

+358
-281
lines changed

11 files changed

+358
-281
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
bin/
33
.secrets/
44
Taskfile.yml
5+
go.work*

cmd/run/main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import (
1616
"github.com/nxtcoder17/runfile/logging"
1717
"github.com/nxtcoder17/runfile/runner"
1818

19-
// "github.com/nxtcoder17/runfile/pkg/runfile"
20-
21-
// "github.com/nxtcoder17/runfile/pkg/runfile"
2219
"github.com/nxtcoder17/runfile/parser"
2320
"github.com/urfave/cli/v3"
2421
)
@@ -240,7 +237,6 @@ func main() {
240237
} else {
241238
slog.Error("got", "err", err)
242239
}
243-
os.Exit(1)
244240
}
245241
}
246242

examples/Runfile.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ tasks:
4444
clean:
4545
name: clean
4646
shell: ["python", "-c"]
47-
dotenv:
48-
- ../.secrets/env
47+
# dotenv:
48+
# - ../.secrets/env
4949
cmd:
5050
- run: laundry
5151
# vars:

go.mod

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ module github.com/nxtcoder17/runfile
33
go 1.22.7
44

55
require (
6-
github.com/alecthomas/chroma/v2 v2.14.1-0.20241203085416-67f0e3b31d46
76
github.com/charmbracelet/lipgloss v1.0.0
87
github.com/joho/godotenv v1.5.1
98
github.com/muesli/termenv v0.15.2
10-
github.com/nxtcoder17/fwatcher v1.0.5-0.20250112020608-9c6a66687046
9+
github.com/nxtcoder17/fwatcher v1.1.0
1110
github.com/phuslu/log v1.0.112
1211
github.com/urfave/cli/v3 v3.0.0-beta1
1312
golang.org/x/sync v0.10.0
@@ -16,20 +15,12 @@ require (
1615

1716
require (
1817
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
19-
github.com/charmbracelet/log v0.4.0 // indirect
2018
github.com/charmbracelet/x/ansi v0.6.0 // indirect
21-
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
22-
github.com/dlclark/regexp2 v1.11.4 // indirect
2319
github.com/fsnotify/fsnotify v1.8.0 // indirect
24-
github.com/go-logfmt/logfmt v0.6.0 // indirect
2520
github.com/google/go-cmp v0.6.0 // indirect
2621
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
2722
github.com/mattn/go-isatty v0.0.20 // indirect
2823
github.com/mattn/go-runewidth v0.0.16 // indirect
2924
github.com/rivo/uniseg v0.4.7 // indirect
30-
github.com/russross/blackfriday/v2 v2.1.0 // indirect
31-
github.com/urfave/cli/v2 v2.27.5 // indirect
32-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
33-
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 // indirect
3425
golang.org/x/sys v0.28.0 // indirect
3526
)

go.sum

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
1-
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
2-
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
3-
github.com/alecthomas/chroma/v2 v2.14.1-0.20241203085416-67f0e3b31d46 h1:s18LOL63NvaZHsxUMGBIxcEVJK4un5SzXuGvCGPz0lg=
4-
github.com/alecthomas/chroma/v2 v2.14.1-0.20241203085416-67f0e3b31d46/go.mod h1:gUhVLrPDXPtp/f+L1jo9xepo9gL4eLwRuGAunSZMkio=
5-
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
6-
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
71
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
82
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
93
github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg=
104
github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo=
11-
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
12-
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
135
github.com/charmbracelet/x/ansi v0.6.0 h1:qOznutrb93gx9oMiGf7caF7bqqubh6YIM0SWKyA08pA=
146
github.com/charmbracelet/x/ansi v0.6.0/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
15-
github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0=
16-
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
177
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
188
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
19-
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
20-
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
219
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
2210
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
23-
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
24-
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
2511
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2612
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2713
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
28-
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
29-
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
3014
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
3115
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
3216
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
@@ -37,35 +21,21 @@ github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6T
3721
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
3822
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
3923
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
40-
github.com/nxtcoder17/fwatcher v1.0.4-0.20241218102704-76d04c526fb0 h1:k09TYswdqCsFz0fBnO3AjGT5MHfvjPRyBtHbBWuiJLM=
41-
github.com/nxtcoder17/fwatcher v1.0.4-0.20241218102704-76d04c526fb0/go.mod h1:MNmSwXYOrqp7U1pUxh0GWB5skpjFTWTQXhAA0+sPJcU=
42-
github.com/nxtcoder17/fwatcher v1.0.4-0.20241231074910-70c2485e84d3 h1:QDnTXuLuOEON1lGOYwrakigAAMU37EL4KMID5Uue5TU=
43-
github.com/nxtcoder17/fwatcher v1.0.4-0.20241231074910-70c2485e84d3/go.mod h1:MNmSwXYOrqp7U1pUxh0GWB5skpjFTWTQXhAA0+sPJcU=
44-
github.com/nxtcoder17/fwatcher v1.0.4-0.20250111091953-092ea9c4aee0 h1:hCGAP5LpxYTAb7H9r3bPCxDzwbaaFc5uZdu3wtpeZ28=
45-
github.com/nxtcoder17/fwatcher v1.0.4-0.20250111091953-092ea9c4aee0/go.mod h1:50dOLE7lHu6bwGtBM7lZ5m6b8wKKMUZDPgBKM2pgdEA=
46-
github.com/nxtcoder17/fwatcher v1.0.5-0.20250111155627-8bc1dfea0064 h1:HhEf79xzmViGl/SAvrrD/e5bxSKPZ0+B5zz0mlYKe6o=
47-
github.com/nxtcoder17/fwatcher v1.0.5-0.20250111155627-8bc1dfea0064/go.mod h1:50dOLE7lHu6bwGtBM7lZ5m6b8wKKMUZDPgBKM2pgdEA=
48-
github.com/nxtcoder17/fwatcher v1.0.5-0.20250112020608-9c6a66687046 h1:QL7gonI9kNiYiDbgDraigH9PGkztgahL24+IUp5HWWA=
49-
github.com/nxtcoder17/fwatcher v1.0.5-0.20250112020608-9c6a66687046/go.mod h1:50dOLE7lHu6bwGtBM7lZ5m6b8wKKMUZDPgBKM2pgdEA=
24+
github.com/nxtcoder17/fwatcher v1.0.5-0.20250117101009-2217d37d1bbd h1:c7QZjT/zcdMqHUaB85oCpXs6cdjJD2N1qFcHjYwoNL8=
25+
github.com/nxtcoder17/fwatcher v1.0.5-0.20250117101009-2217d37d1bbd/go.mod h1:50dOLE7lHu6bwGtBM7lZ5m6b8wKKMUZDPgBKM2pgdEA=
26+
github.com/nxtcoder17/fwatcher v1.1.0 h1:boRQ1zyN7SDnRYqrEUMJTKpf/j3sQ3YMY0FQKkUIQ0o=
27+
github.com/nxtcoder17/fwatcher v1.1.0/go.mod h1:50dOLE7lHu6bwGtBM7lZ5m6b8wKKMUZDPgBKM2pgdEA=
5028
github.com/phuslu/log v1.0.112 h1:vQ0ZFd5O+in/0IQAcjuEl6wRkHiQPw7T0sqwmOjpL0U=
5129
github.com/phuslu/log v1.0.112/go.mod h1:F8osGJADo5qLK/0F88djWwdyoZZ9xDJQL1HYRHFEkS0=
5230
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5331
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5432
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
5533
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
5634
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
57-
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
58-
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5935
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6036
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
61-
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
62-
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
6337
github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=
6438
github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
65-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
66-
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
67-
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 h1:1UoZQm6f0P/ZO0w1Ri+f+ifG/gXhegadRdwBIXEFWDo=
68-
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
6939
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
7040
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
7141
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

parser/parse-command.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func parseCommand(prf *types.ParsedRunfile, command any) (*types.ParsedCommandJs
1616
switch c := command.(type) {
1717
case string:
1818
{
19-
return &types.ParsedCommandJson{Command: c}, nil
19+
return &types.ParsedCommandJson{Commands: []string{c}}, nil
2020
}
2121
case map[string]any:
2222
{
@@ -30,13 +30,31 @@ func parseCommand(prf *types.ParsedRunfile, command any) (*types.ParsedCommandJs
3030
return nil, ferr(err)
3131
}
3232

33-
if cj.Run == "" && cj.Command == "" {
34-
return nil, ferr(fmt.Errorf("key: 'run' or 'cmd', must be specified when setting command in json format"))
33+
pcj := types.ParsedCommandJson{
34+
Env: cj.Env,
35+
Parallel: cj.Parallel,
3536
}
3637

37-
var pcj types.ParsedCommandJson
38-
pcj.Run = cj.Run
39-
pcj.Command = cj.Command
38+
switch {
39+
case cj.Run != "" || cj.Runs != nil:
40+
{
41+
pcj.Runs = cj.Runs
42+
if cj.Run != "" {
43+
pcj.Runs = append(pcj.Runs, cj.Run)
44+
}
45+
}
46+
case cj.Command != "" || cj.Commands != nil:
47+
{
48+
pcj.Commands = cj.Commands
49+
if cj.Command != "" {
50+
pcj.Commands = append(pcj.Commands, cj.Command)
51+
}
52+
}
53+
default:
54+
{
55+
return nil, fmt.Errorf("either 'run' or 'cmd' key, must be specified when setting command in json format")
56+
}
57+
}
4058

4159
if _, ok := prf.Tasks[cj.Run]; !ok {
4260
return nil, errors.ErrTaskNotFound.Wrap(fmt.Errorf("run target, not found")).KV("command", command, "run-target", cj.Run)

parser/parse-command_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package parser
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/nxtcoder17/runfile/types"
8+
)
9+
10+
func Test_parseCommand(t *testing.T) {
11+
type args struct {
12+
prf *types.ParsedRunfile
13+
command any
14+
}
15+
tests := []struct {
16+
name string
17+
args args
18+
want *types.ParsedCommandJson
19+
wantErr bool
20+
}{
21+
// TODO: Add test cases.
22+
}
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
got, err := parseCommand(tt.args.prf, tt.args.command)
26+
if (err != nil) != tt.wantErr {
27+
t.Errorf("parseCommand() error = %v, wantErr %v", err, tt.wantErr)
28+
return
29+
}
30+
if !reflect.DeepEqual(got, tt.want) {
31+
t.Errorf("parseCommand() = %v, want %v", got, tt.want)
32+
}
33+
})
34+
}
35+
}

0 commit comments

Comments
 (0)