Skip to content

Commit

Permalink
refactor: cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtcoder17 committed Nov 9, 2024
1 parent db127b0 commit 9472489
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 101 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,34 @@ tasks:
- echo "value of key3 is '$key3'"
- echo "value of key4 is '$key4'" # assuming key4 is defined in .secrets/env
```
## Updates with example runfile with all the features
```yaml
version: 0.0.1

tasks:
test:
env:
key1: value1
key2: value2
key3:
sh: echo -n "hello"
key4:
required: true
dotenv:
- .secrets/env # load dotenv file
cmd:
- echo "value of key1 is '$key1'"
- echo "value of key2 is '$key2'"
- echo "value of key3 is '$key3'"
- echo "value of key4 is '$key4'" # assuming key4 is defined in .secrets/env
build:
dir: cmd/app
cmd:
- go build -o app
run:
dir: cmd/app
cmd:
- go run .
```
4 changes: 4 additions & 0 deletions Runfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ tasks:
test:
cmd:
- go test -json ./pkg/runfile | gotestfmt

test:only-failing:
cmd:
- go test -json ./pkg/runfile | gotestfmt --hide successful-tests
96 changes: 0 additions & 96 deletions pkg/runfile/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,102 +151,6 @@ func runTask(ctx Context, rf *Runfile, args runTaskArgs) *Error {
return nil
}

// func (rf *Runfile) runTask(ctx Context, args runTaskArgs) *Error {
// runfilePath := fn.Must(filepath.Rel(rf.attrs.RootRunfilePath, rf.attrs.RunfilePath))
//
// trail := append(args.taskTrail, args.taskName)
//
// formatErr := func(err *Error) *Error {
// if runfilePath != "." {
// return err.WithTask(strings.Join(trail, "/")).WithRunfile(runfilePath)
// }
// return err.WithTask(strings.Join(trail, "/"))
// }
//
// logger := ctx.With("task", args.taskName, "runfile", runfilePath)
// logger.Debug("running task")
// task, ok := rf.Tasks[args.taskName]
// if !ok {
// return formatErr(TaskNotFound)
// }
//
// task.Name = args.taskName
// if task.Env == nil {
// task.Env = make(EnvVar)
// }
//
// for k, v := range args.envOverrides {
// task.Env[k] = v
// }
//
// pt, err := ParseTask(ctx, rf, task)
// if err != nil {
// return formatErr(err)
// }
//
// logger.Debug("debugging env", "pt.environ", pt.Env, "overrides", args.envOverrides)
// for _, command := range pt.Commands {
// logger.Debug("running command task", "command.run", command.Run, "parent.task", args.taskName)
// if command.Run != "" {
// if err := rf.runTask(ctx, runTaskArgs{
// taskTrail: trail,
// taskName: command.Run,
// envOverrides: pt.Env,
// }); err != nil {
// return err
// // return NewError("", "").WithTask(fmt.Sprintf("%s/%s", err.TaskName, command.Run)).WithRunfile(rf.attrs.RunfilePath).WithErr(err.WithMetadata())
// // e := formatErr(err).WithTask(fmt.Sprintf("%s/%s", err.TaskName, command.Run))
// // return e
// }
// continue
// }
//
// stdoutR, stdoutW := io.Pipe()
// stderrR, stderrW := io.Pipe()
//
// go func() {
// r := bufio.NewReader(stdoutR)
// for {
// b, err := r.ReadBytes('\n')
// if err != nil {
// logger.Info("stdout", "msg", string(b), "err", err)
// // return
// break
// }
// fmt.Fprintf(os.Stdout, "%s %s", ctx.theme.TaskPrefixStyle.Render(fmt.Sprintf("[%s]", args.taskName)), b)
// }
// }()
//
// go func() {
// r := bufio.NewReader(stderrR)
// for {
// b, err := r.ReadBytes('\n')
// if err != nil {
// fmt.Printf("hello err: %+v\n", err)
// logger.Info("stderr", "err", err)
// // return
// break
// }
// fmt.Fprintf(os.Stderr, "%s %s", ctx.theme.TaskPrefixStyle.Render(fmt.Sprintf("[%s]", args.taskName)), b)
// }
// }()
//
// cmd := createCommand(ctx, cmdArgs{
// shell: pt.Shell,
// env: ToEnviron(pt.Env),
// cmd: command.Command,
// workingDir: pt.WorkingDir,
// stdout: stdoutW,
// stderr: stderrW,
// })
// if err := cmd.Run(); err != nil {
// return formatErr(CommandFailed).WithErr(err)
// }
// }
//
// return nil
// }

type RunArgs struct {
Tasks []string
ExecuteInParallel bool
Expand Down
38 changes: 34 additions & 4 deletions pkg/runfile/run_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package runfile

import (
"context"
"log/slog"
"reflect"
"testing"
"time"
)

func Test_runTask(t *testing.T) {
type args struct {
ctx Context
rf *Runfile
args runTaskArgs
}
Expand All @@ -16,12 +18,40 @@ func Test_runTask(t *testing.T) {
args args
want *Error
}{
// TODO: Add test cases.
{
name: "1. Task Not Found",
args: args{
rf: &Runfile{
Tasks: map[string]Task{},
},
args: runTaskArgs{
taskName: "sample",
},
},
want: TaskNotFound,
},

{
name: "1. Task Not Found",
args: args{
rf: &Runfile{
Tasks: map[string]Task{},
},
args: runTaskArgs{
taskName: "sample",
},
},
want: TaskNotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := runTask(tt.args.ctx, tt.args.rf, tt.args.args); !reflect.DeepEqual(got, tt.want) {
t.Errorf("runTask() = %v, want %v", got, tt.want)
ctx, cf := context.WithTimeout(context.TODO(), 2*time.Second)
defer cf()

err := runTask(NewContext(ctx, slog.Default()), tt.args.rf, tt.args.args)
if !reflect.DeepEqual(err, tt.want) {
t.Errorf("runTask() = %v, want %v", err, tt.want)
}
})
}
Expand Down
1 change: 0 additions & 1 deletion pkg/runfile/task-parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type ParsedTask struct {
Commands []CommandJson `json:"commands"`
}

// func ParseTask(ctx Context, rf *Runfile, taskName string) (*ParsedTask, error) {
func ParseTask(ctx Context, rf *Runfile, task Task) (*ParsedTask, *Error) {
globalEnv := make(map[string]string)

Expand Down

0 comments on commit 9472489

Please sign in to comment.