Skip to content

Commit 65f8ccf

Browse files
authored
Merge pull request #40 from nxtcoder17/refactor
refactor: migrates to nixy from nix flakes - re-designs project structure and types by grouping them in `spec` (that is desired), and resolver (which resolves as per the spec) - migrates to an improved executor, and removes dependency on fwatcher's command group executor
2 parents fd15962 + 9d9701e commit 65f8ccf

43 files changed

Lines changed: 2242 additions & 3585 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.envrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

Runfile.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
tasks:
22
build:
3+
env:
4+
CGO_ENABLED: 0
35
cmd:
46
- |+
57
echo "building ..."
@@ -16,12 +18,7 @@ tasks:
1618
example:
1719
dir: ./examples
1820
cmd:
19-
- |+
20-
run cook clean
21-
22-
test:old:
23-
cmd:
24-
- go test -json ./pkg/runfile | gotestfmt
21+
- echo "hello world"
2522

2623
test:
2724
env:
@@ -30,7 +27,6 @@ tasks:
3027
only_failing:
3128
default: false
3229
watch:
33-
enable: true
3430
dir:
3531
- ./parser
3632
onlySuffixes:
@@ -43,8 +39,11 @@ tasks:
4339
testfmt_args=""
4440
[ "$only_failing" = "true" ] && testfmt_args="--hide successful-tests"
4541
46-
go test -json ./pkg/runfile/... $pattern_args | gotestfmt $testfmt_args
42+
go test -json ./pkg/runfile/resolver/... $pattern_args | gotestfmt $testfmt_args
43+
go test -json ./pkg/executor/... $pattern_args | gotestfmt $testfmt_args
4744
4845
test:only-failing:
4946
cmd:
50-
- go test -json ./pkg/runfile | gotestfmt --hide successful-tests
47+
- go test -json ./pkg/runfile/resolver/... | gotestfmt --hide successful-tests
48+
- go test -json ./pkg/executor/... | gotestfmt --hide successful-tests
49+

cmd/run/completions.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
package main
22

3-
import (
4-
"context"
5-
"fmt"
6-
"io"
7-
"log/slog"
3+
//
4+
// import (
5+
// "context"
6+
// "fmt"
7+
// "io"
8+
// "log/slog"
9+
//
10+
// "github.com/nxtcoder17/fastlog"
11+
// "github.com/nxtcoder17/runfile/pkg/runfile"
12+
// )
813

9-
"github.com/nxtcoder17/fastlog"
10-
"github.com/nxtcoder17/runfile/pkg/runfile"
11-
)
12-
13-
func generateShellCompletion(ctx context.Context, writer io.Writer, rfpath string) error {
14-
runfile, err := runfile.ParseFromFile(runfile.NewContext(ctx, fastlog.New()), rfpath)
15-
if err != nil {
16-
slog.Error("parsing, got", "err", err)
17-
panic(err)
18-
}
19-
20-
for k := range runfile.Tasks {
21-
fmt.Fprintf(writer, "%s\n", k)
22-
}
23-
24-
return nil
25-
}
14+
// func generateShellCompletion(ctx context.Context, writer io.Writer, rfpath string) error {
15+
// runfile, err := runfile.ParseFromFile(runfile.NewContext(ctx, fastlog.New()), rfpath)
16+
// if err != nil {
17+
// slog.Error("parsing, got", "err", err)
18+
// panic(err)
19+
// }
20+
//
21+
// for k := range runfile.Tasks {
22+
// fmt.Fprintf(writer, "%s\n", k)
23+
// }
24+
//
25+
// return nil
26+
// }

cmd/run/completions/run.fish

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
# args: (1)
2-
# run fish shell completion
3-
set PROGNAME run
4-
5-
# list_targets fetches all targets, with all flags provided on the cli
6-
function list_targets
7-
# eval $PROGNAME --list
8-
eval (commandline -b) --list
1+
# # args: (1)
2+
# # run fish shell completion
3+
# set PROGNAME run
4+
#
5+
# # list_targets fetches all targets, with all flags provided on the cli
6+
# function list_targets
7+
# # eval $PROGNAME --list
8+
# eval (commandline -b) --list
9+
# echo "shell:completion"
10+
# end
11+
#
12+
# complete -c $PROGNAME -d "runs named task" -xa '(list_targets)'
13+
# complete -c $PROGNAME -d "runs named task" -xa '(list_targets)'
14+
# complete -c $PROGNAME -l help -s h -d 'show help'
15+
# complete -c $PROGNAME -l list -s l -d 'list all tasks'
16+
# complete -c $PROGNAME -rF -l file -s f -d 'runs targets from this runfile'
17+
18+
# completion fish shell completion
19+
20+
function __fish_completion_no_subcommand --description 'Test if there has been any subcommand yet'
21+
for i in (commandline -opc)
22+
if contains -- $i
23+
return 1
24+
end
25+
end
26+
return 0
927
end
1028

11-
complete -c $PROGNAME -d "runs named task" -xa '(list_targets)'
12-
complete -c $PROGNAME -l help -s h -d 'show help'
13-
complete -c $PROGNAME -l list -s l -d 'list all tasks'
14-
complete -c $PROGNAME -rF -l file -s f -d 'runs targets from this runfile'
29+
complete -c run -n '__fish_completion_no_subcommand' -f -l help -s h -d 'show help'
30+
complete -c run -n '__fish_completion_no_subcommand' -f -l list -s l -d 'list all tasks'
31+
complete -c run -n '__fish_completion_no_subcommand' -rF -l file -s f -d 'runs targets from this runfile'
1532

cmd/run/main.go

Lines changed: 88 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/nxtcoder17/fastlog"
16-
"github.com/nxtcoder17/runfile/pkg/errors"
16+
"github.com/nxtcoder17/go.errors"
1717
"github.com/nxtcoder17/runfile/pkg/runfile"
1818
"github.com/urfave/cli/v3"
1919
)
@@ -95,24 +95,26 @@ func main() {
9595
return
9696
}
9797

98-
runfilePath, err := locateRunfile(c)
99-
if err != nil {
100-
slog.Error("locating runfile", "err", err)
101-
panic(err)
102-
}
98+
// runfilePath, err := locateRunfile(c)
99+
// if err != nil {
100+
// slog.Error("locating runfile", "err", err)
101+
// panic(err)
102+
// }
103103

104-
generateShellCompletion(ctx, c.Root().Writer, runfilePath)
104+
// generateShellCompletion(ctx, c.Root().Writer, runfilePath)
105105
},
106106

107107
Commands: []*cli.Command{
108108
{
109-
Name: "shell:completion",
110-
Usage: "<bash|zsh|fish|ps>",
111-
Suggest: true,
109+
Name: "shell:completion",
110+
Usage: "[shell]",
111+
EnableShellCompletion: false,
112112
Action: func(ctx context.Context, c *cli.Command) error {
113-
fmt.Printf("args: (%d)\n", c.NArg())
114-
if c.NArg() != 1 {
115-
return fmt.Errorf("needs argument one of [bash,zsh,fish,ps]")
113+
if c.NArg() == 0 {
114+
for _, shell := range []string{"fish", "bash", "zsh", "powershell"} {
115+
fmt.Fprintf(c.Writer, "%s\n", shell)
116+
}
117+
return nil
116118
}
117119

118120
switch c.Args().First() {
@@ -129,6 +131,26 @@ func main() {
129131
return nil
130132
},
131133
},
134+
{
135+
Name: "init",
136+
EnableShellCompletion: false,
137+
Action: func(ctx context.Context, c *cli.Command) error {
138+
dir, err := os.Getwd()
139+
if err != nil {
140+
return err
141+
}
142+
143+
_, err = getRunfilePath(dir)
144+
if err == nil {
145+
slog.Info("Runfile already exists in current directory")
146+
return nil
147+
}
148+
149+
// TODO: implement init command to create a sample Runfile
150+
slog.Info("init command not yet implemented")
151+
return nil
152+
},
153+
},
132154
},
133155

134156
Suggest: true,
@@ -139,12 +161,12 @@ func main() {
139161

140162
showList := c.Bool("list")
141163
if showList {
142-
runfilePath, err := locateRunfile(c)
143-
if err != nil {
144-
slog.Error("locating runfile, got", "err", err)
145-
return err
146-
}
147-
return generateShellCompletion(ctx, c.Root().Writer, runfilePath)
164+
// runfilePath, err := locateRunfile(c)
165+
// if err != nil {
166+
// slog.Error("locating runfile, got", "err", err)
167+
// return err
168+
// }
169+
// return generateShellCompletion(ctx, c.Root().Writer, runfilePath)
148170
}
149171

150172
if c.NArg() == 0 {
@@ -157,16 +179,6 @@ func main() {
157179
// INFO: for supporting flags that have been suffixed post arguments
158180
args := make([]string, 0, len(c.Args().Slice()))
159181
for _, arg := range c.Args().Slice() {
160-
if arg == "-p" || arg == "--parallel" {
161-
parallel = true
162-
continue
163-
}
164-
165-
if arg == "-w" || arg == "--watch" {
166-
watch = true
167-
continue
168-
}
169-
170182
if arg == "--debug" {
171183
debug = true
172184
continue
@@ -185,37 +197,17 @@ func main() {
185197
return fmt.Errorf("parallel and watch can't be set together")
186198
}
187199

188-
logger := fastlog.New(fastlog.Options{
189-
Format: fastlog.ConsoleFormat,
190-
EnableColors: true,
191-
ShowCaller: debug,
192-
ShowTimestamp: false,
193-
ShowDebugLogs: debug,
194-
})
200+
logger := fastlog.New(fastlog.Console(), fastlog.ShowDebugLogs(debug), fastlog.WithoutTimestamp())
201+
slog.SetDefault(logger.Slog())
195202

196203
runfilePath, err := locateRunfile(c)
197204
if err != nil {
198205
slog.Error("locating runfile, got", "err", err)
199206
return err
200207
}
201208

202-
rctx := runfile.NewContext(ctx, logger)
203-
204-
rf, err := runfile.ParseFromFile(rctx, runfilePath)
205-
if err != nil {
206-
slog.Error("parsing runfile, got", "err", err)
207-
panic(err)
208-
}
209-
210-
if err := rf.Run(rctx, args, runfile.RunOption{
211-
ExecuteInParallel: parallel,
212-
Watch: watch,
213-
Debug: debug,
214-
KVs: kv,
215-
}); err != nil {
216-
if err2, ok := err.(*errors.Error); ok {
217-
logger.Error(err2.Error(), err2.SlogAttrs()...)
218-
}
209+
if err := runfile.RunTask(ctx, runfilePath, args[0], kv); err != nil {
210+
return err
219211
}
220212

221213
return nil
@@ -231,8 +223,40 @@ func main() {
231223
}()
232224

233225
if err := cmd.Run(ctx, os.Args); err != nil {
234-
slog.Error("while running cmd, got", "err", err)
226+
if err2, ok := err.(*errors.Error); ok {
227+
slog.Error("failed to run task", err2.AsKeyValues()...)
228+
return
229+
}
230+
slog.Error("failed to run task", "err", err)
231+
}
232+
}
233+
234+
var ErrRunfileNotFound = fmt.Errorf("failed to locate your nearest Runfile")
235+
236+
func getRunfilePath(dir string) (string, error) {
237+
runfileNames := []string{
238+
"Runfile",
239+
"Runfile.yml",
240+
"Runfile.yaml",
241+
}
242+
243+
for _, f := range runfileNames {
244+
stat, err := os.Stat(filepath.Join(dir, f))
245+
if err != nil {
246+
if !os.IsNotExist(err) {
247+
return "", err
248+
}
249+
continue
250+
}
251+
252+
if stat.IsDir() {
253+
return "", fmt.Errorf("%s is a directory", filepath.Join(dir, f))
254+
}
255+
256+
return filepath.Join(dir, f), nil
235257
}
258+
259+
return "", ErrRunfileNotFound
236260
}
237261

238262
func locateRunfile(c *cli.Command) (string, error) {
@@ -247,28 +271,22 @@ func locateRunfile(c *cli.Command) (string, error) {
247271

248272
oldDir := ""
249273

250-
runfileNames := []string{
251-
"Runfile",
252-
"Runfile.yml",
253-
"Runfile.yaml",
254-
}
255-
256274
for oldDir != dir {
257-
for _, fn := range runfileNames {
258-
if _, err := os.Stat(filepath.Join(dir, fn)); err != nil {
259-
if !os.IsNotExist(err) {
260-
return "", err
261-
}
275+
fp, err := getRunfilePath(dir)
276+
if err != nil {
277+
if errors.Is(err, ErrRunfileNotFound) {
278+
// Not found in this dir, try parent
279+
oldDir = dir
280+
dir = filepath.Dir(dir)
262281
continue
263282
}
264-
265-
return filepath.Join(dir, fn), nil
283+
// Some other error
284+
return "", err
266285
}
267286

268-
oldDir = dir
269-
dir = filepath.Dir(dir)
287+
return fp, nil
270288
}
271289

272-
return "", fmt.Errorf("failed to locate your nearest Runfile")
290+
return "", ErrRunfileNotFound
273291
}
274292
}

0 commit comments

Comments
 (0)