Skip to content

Commit 39eb4df

Browse files
authored
Merge pull request [#30](#30)
Rethink/watch mode
2 parents eeedbf5 + 174d62b commit 39eb4df

16 files changed

+454
-318
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
bin/
33
.secrets/
44
Taskfile.yml
5+
go.work*

Runfile.yml

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ tasks:
66
go build -o bin/run -ldflags="-s -w" -tags urfave_cli_no_docs ./cmd/run
77
echo "DONE"
88
9+
build:dev:
10+
cmd:
11+
- |+
12+
echo "building ..."
13+
go build -o bin/run-dev -ldflags="-s -w" -tags urfave_cli_no_docs ./cmd/run
14+
echo "DONE"
15+
916
example:
1017
dir: ./examples
1118
cmd:

cmd/run/main.go

+1-5
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
)
@@ -195,7 +192,7 @@ func main() {
195192
}
196193

197194
logger := logging.New(logging.Options{
198-
ShowCaller: true,
195+
ShowCaller: false,
199196
SlogKeyAsPrefix: "task",
200197
ShowDebugLogs: debug,
201198
SetAsDefaultLogger: true,
@@ -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

+2-2
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:

examples/run1/Runfile

+3
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ tasks:
1414
- node
1515

1616
printenv:
17+
dotenv:
18+
- ../../.secrets/env
1719
cmd:
1820
- printenv file1_env1
21+
- printenv key_id

examples/run2/Runfile

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
version: 0.0.1
22

3+
dotenv:
4+
- ../../.secrets/env
5+
36
tasks:
47
echo:
58
cmd:
69
- echo "hello from run2"
10+
- printenv key_id

go.mod

+1-3
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.4-0.20241218102704-76d04c526fb0
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
@@ -17,7 +16,6 @@ require (
1716
require (
1817
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
1918
github.com/charmbracelet/x/ansi v0.6.0 // indirect
20-
github.com/dlclark/regexp2 v1.11.4 // indirect
2119
github.com/fsnotify/fsnotify v1.8.0 // indirect
2220
github.com/google/go-cmp v0.6.0 // indirect
2321
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect

go.sum

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
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=
@@ -12,15 +6,11 @@ github.com/charmbracelet/x/ansi v0.6.0 h1:qOznutrb93gx9oMiGf7caF7bqqubh6YIM0SWKy
126
github.com/charmbracelet/x/ansi v0.6.0/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
137
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
148
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
15-
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
16-
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
179
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
1810
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
1911
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2012
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2113
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
22-
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
23-
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
2414
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
2515
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
2616
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
@@ -31,8 +21,10 @@ github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6T
3121
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
3222
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
3323
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
34-
github.com/nxtcoder17/fwatcher v1.0.4-0.20241218102704-76d04c526fb0 h1:k09TYswdqCsFz0fBnO3AjGT5MHfvjPRyBtHbBWuiJLM=
35-
github.com/nxtcoder17/fwatcher v1.0.4-0.20241218102704-76d04c526fb0/go.mod h1:MNmSwXYOrqp7U1pUxh0GWB5skpjFTWTQXhAA0+sPJcU=
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=
3628
github.com/phuslu/log v1.0.112 h1:vQ0ZFd5O+in/0IQAcjuEl6wRkHiQPw7T0sqwmOjpL0U=
3729
github.com/phuslu/log v1.0.112/go.mod h1:F8osGJADo5qLK/0F88djWwdyoZZ9xDJQL1HYRHFEkS0=
3830
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

parser/parse-command.go

+23-6
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,30 @@ 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,
3535
}
3636

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

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

parser/parse-command_test.go

+35
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+
}

parser/parse-task.go

+36-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212
"github.com/nxtcoder17/runfile/types"
1313
)
1414

15+
func isAbsPath(p string) bool {
16+
j, err := filepath.Abs(p)
17+
if err != nil {
18+
panic(err)
19+
}
20+
return j == p
21+
}
22+
1523
func ParseTask(ctx context.Context, prf *types.ParsedRunfile, task types.Task) (*types.ParsedTask, error) {
1624
workingDir := filepath.Dir(prf.Metadata.RunfilePath)
1725
if task.Metadata.RunfilePath != nil {
@@ -24,7 +32,19 @@ func ParseTask(ctx context.Context, prf *types.ParsedRunfile, task types.Task) (
2432
taskEnv = make(map[string]string)
2533
}
2634

27-
tdotenv, err := parseDotEnvFiles(task.DotEnv...)
35+
dotEnvs := make([]string, 0, len(task.DotEnv))
36+
for i := range task.DotEnv {
37+
de := task.DotEnv[i]
38+
if !filepath.IsAbs(de) {
39+
result := filepath.Join(filepath.Dir(*task.Metadata.RunfilePath), de)
40+
// fmt.Println("HERE", "runfilepath", prf.Metadata.RunfilePath, "dotenv", de, "result", result)
41+
de = result
42+
}
43+
44+
dotEnvs = append(dotEnvs, de)
45+
}
46+
47+
tdotenv, err := parseDotEnvFiles(dotEnvs...)
2848
if err != nil {
2949
return nil, err
3050
}
@@ -88,12 +108,26 @@ func ParseTask(ctx context.Context, prf *types.ParsedRunfile, task types.Task) (
88108
commands = append(commands, *c2)
89109
}
90110

111+
watch := task.Watch
112+
if watch != nil {
113+
for i := range watch.Dirs {
114+
if !isAbsPath(watch.Dirs[i]) {
115+
watch.Dirs[i] = filepath.Join(*task.Dir, watch.Dirs[i])
116+
}
117+
}
118+
// for i := range watch.ExcludeDirs {
119+
// if !isAbsPath(watch.ExcludeDirs[i]) {
120+
// watch.ExcludeDirs[i] = filepath.Join(*task.Dir, watch.ExcludeDirs[i])
121+
// }
122+
// }
123+
}
124+
91125
return &types.ParsedTask{
92126
Shell: task.Shell,
93127
WorkingDir: *task.Dir,
94128
Interactive: task.Interactive,
95129
Env: taskEnv,
96130
Commands: commands,
97-
Watch: task.Watch,
131+
Watch: watch,
98132
}, nil
99133
}

0 commit comments

Comments
 (0)