Skip to content

Commit

Permalink
feat: key-value pairs from arguments, will now be added to target's Env
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtcoder17 committed Oct 6, 2024
1 parent cd7f783 commit b6e473a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jobs:
gh release list -R ${{ github.repository }} | grep -i $IMAGE_TAG
exit_code=$?
if [ $exit_code -ne 0 ]; then
gh release create $IMAGE_TAG -R ${{ github.repository }} --generate-notes --prerelease
gh release create $IMAGE_TAG -R ${{ github.repository }} --generate-notes --prerelease --draft=false
fi
- name: upload to github release
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ Create a `Runfile` in the root of your project, and add tasks to it.
- [x] Run tasks with Key-Value environment variables
- [x] Run tasks with dynamic environment variables (by shell execution)
- [x] Run tasks with dotenv files as their environment variables
- [ ] Running tasks in different working directory [reference](https://taskfile.dev/reference/schema/#task)
- [x] Running tasks in different working directory [reference](https://taskfile.dev/reference/schema/#task)
- [x] Running tasks in parallel
- [ ] Running tasks with watch mode
- [ ] Running tasks in parallel
- [x] Requirements prior to running a target
- [x] Environment validations and default value

### Example

Expand Down
15 changes: 13 additions & 2 deletions cmd/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

"github.com/nxtcoder17/fwatcher/pkg/logging"
Expand Down Expand Up @@ -70,7 +71,7 @@ func main() {
watch := c.Bool("watch")
debug := c.Bool("debug")

logging.NewSlogLogger(logging.SlogOptions{
logger := logging.NewSlogLogger(logging.SlogOptions{
ShowCaller: debug,
ShowDebugLogs: debug,
SetAsDefaultLogger: true,
Expand All @@ -90,6 +91,9 @@ func main() {
panic(err)
}

kv := make(map[string]string)

// INFO: for supporting flags that have been suffixed post arguments
args := make([]string, 0, len(c.Args().Slice()))
for _, arg := range c.Args().Slice() {
if arg == "-p" || arg == "--parallel" {
Expand All @@ -107,18 +111,25 @@ func main() {
continue
}

sp := strings.SplitN(arg, "=", 2)
if len(sp) == 2 {
kv[sp[0]] = sp[1]
continue
}

args = append(args, arg)
}

if parallel && watch {
return fmt.Errorf("parallel and watch can't be set together")
}

return rf.Run(ctx, runfile.RunArgs{
return rf.Run(runfile.NewContext(ctx, logger), runfile.RunArgs{
Tasks: args,
ExecuteInParallel: parallel,
Watch: watch,
Debug: debug,
KVs: kv,
})
},
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/runfile/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,23 @@ type RunArgs struct {
ExecuteInParallel bool
Watch bool
Debug bool
KVs map[string]string
}

func (rf *Runfile) Run(ctx context.Context, args RunArgs) error {
for _, v := range args.Tasks {
if _, ok := rf.Tasks[v]; !ok {
func (rf *Runfile) Run(ctx Context, args RunArgs) error {
for _, taskName := range args.Tasks {
task, ok := rf.Tasks[taskName]
if !ok {
return errors.TaskNotFound{Context: errors.Context{
Task: v,
Task: taskName,
Runfile: rf.attrs.RunfilePath,
}}
}

// INFO: adding parsed KVs as environments to the specified tasks
for k, v := range args.KVs {
task.Env[k] = v
}
}

if args.ExecuteInParallel {
Expand Down
15 changes: 15 additions & 0 deletions pkg/runfile/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"strings"
"text/template"
Expand All @@ -13,6 +14,20 @@ import (
fn "github.com/nxtcoder17/runfile/pkg/functions"
)

type Context struct {
context.Context
*slog.Logger
}

func NewContext(ctx context.Context, logger *slog.Logger) Context {
lgr := logger
if lgr == nil {
lgr = slog.Default()
}

return Context{Context: ctx, Logger: lgr}
}

type EvaluationArgs struct {
Shell []string
Env map[string]string
Expand Down

0 comments on commit b6e473a

Please sign in to comment.