Skip to content

Commit

Permalink
Merges [PR](#9)
Browse files Browse the repository at this point in the history
Feat/requirements for a run target
  • Loading branch information
nxtcoder17 authored Sep 29, 2024
2 parents f5a0bf1 + 5bc0617 commit 1a1419f
Show file tree
Hide file tree
Showing 20 changed files with 1,739 additions and 129 deletions.
134 changes: 134 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
name: Release

on:
workflow_dispatch:

push:
tags:
- 'v*'

branches:
- master

paths:
- "cmd/run/**"
- ".github/**"
- "pkg/**"
- go.* # go.mod, and go.sum
- flake.*

permissions:
contents: write
packages: write

jobs:
build-binary:
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, macos-14, macos-13]
arch: [amd64, arm64]
include:
- os: ubuntu-latest
platform: linux

- os: macos-13
platform: darwin

- os: macos-14
platform: darwin
exclude:
- os: macos-14
arch: amd64
- os: macos-13
arch: arm64

name: Building run-${{ matrix.platform }}-${{ matrix.arch }}
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- uses: nxtcoder17/actions/setup-cache-go@v1
with:
cache_key: "run-${{ matrix.platform }}-${{ matrix.arch }}"
working_directory: .

- uses: nxtcoder17/actions/generate-image-tag@v1

- uses: nxtcoder17/actions/setup-nix-cachix@v1
with:
flake_lock: "./flake.lock"
nix_develop_arguments: ".#default"
cachix_cache_name: ${{ secrets.CACHIX_CACHE_NAME }}
cachix_auth_token: ${{ secrets.CACHIX_AUTH_TOKEN }}

- name: Build Binary
shell: bash
env:
CGO_ENABLED: 0
run: |+
binary=bin/run-${{ matrix.platform }}-${{ matrix.arch }}
go build -o $binary -ldflags="-s -w" -tags urfave_cli_no_docs cmd/run/main.go
if [ "${{ matrix.platform }}" = "linux" ]; then
upx $binary
fi
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: run-${{ matrix.platform }}-${{ matrix.arch }}
path: bin/*

release:
needs: build-binary
runs-on: ubuntu-latest
steps:
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: ${{ github.workspace }}/binaries
pattern: "run-*"

- name: flattening all the executable artifacts
shell: bash
run: |+
ls -R ${{ github.workspace }}/binaries
mkdir -p ${{ github.workspace }}/upload/binaries
shopt -s globstar
file ./** | grep 'executable,' | awk -F: '{print $1}' | xargs -I {} cp {} ${{ github.workspace }}/upload/binaries
shopt -u globstar
- uses: nxtcoder17/actions/generate-image-tag@v1

- name: running for master branch
if: startsWith(github.ref, 'refs/heads/master')
run: |+
echo "IMAGE_TAG=nightly" | tee -a $GITHUB_ENV | tee -a $GITHUB_OUTPUT
- name: ensure github release exists
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |+
set +e
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
fi
- name: upload to github release
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |+
gh release upload $IMAGE_TAG -R ${{github.repository}} ${{github.workspace}}/upload/binaries/*
- name: mark release as latest
if: startsWith(github.ref, 'refs/tags/')
env:
GH_TOKEN: ${{ github.token }}
shell: bash
run: |+
gh release edit $IMAGE_TAG -R ${{ github.repository }} --latest
7 changes: 6 additions & 1 deletion Runfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ tasks:
echo "DONE"
example:
dir: ./examples
cmd:
- |+
run -f ./examples/Runfile cook
run cook clean
test:
cmd:
- go test -json ./pkg/runfile | gotestfmt
71 changes: 62 additions & 9 deletions cmd/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"syscall"

"github.com/nxtcoder17/fwatcher/pkg/logging"
"github.com/nxtcoder17/runfile/pkg/runfile"
"github.com/urfave/cli/v3"
)
Expand All @@ -26,6 +27,23 @@ func main() {
Aliases: []string{"f"},
Value: "",
},

&cli.BoolFlag{
Name: "parallel",
Aliases: []string{"p"},
Value: false,
},

&cli.BoolFlag{
Name: "watch",
Aliases: []string{"w"},
Value: false,
},

&cli.BoolFlag{
Name: "debug",
Value: false,
},
},
EnableShellCompletion: true,
ShellComplete: func(ctx context.Context, c *cli.Command) {
Expand All @@ -38,7 +56,7 @@ func main() {
panic(err)
}

runfile, err := runfile.ParseRunFile(runfilePath)
runfile, err := runfile.Parse(runfilePath)
if err != nil {
panic(err)
}
Expand All @@ -48,25 +66,60 @@ func main() {
}
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() > 1 {
return fmt.Errorf("too many arguments")
}
if c.Args().Len() != 1 {
return fmt.Errorf("missing argument")
parallel := c.Bool("parallel")
watch := c.Bool("watch")
debug := c.Bool("debug")

logging.NewSlogLogger(logging.SlogOptions{
ShowCaller: debug,
ShowDebugLogs: debug,
SetAsDefaultLogger: true,
})

if c.Args().Len() < 1 {
return fmt.Errorf("missing argument, at least one argument is required")
}

runfilePath, err := locateRunfile(c)
if err != nil {
return err
}

runfile, err := runfile.ParseRunFile(runfilePath)
rf, err := runfile.Parse(runfilePath)
if err != nil {
panic(err)
}

s := c.Args().First()
return runfile.Run(ctx, s)
args := make([]string, 0, len(c.Args().Slice()))
for _, arg := range c.Args().Slice() {
if arg == "-p" || arg == "--parallel" {
parallel = true
continue
}

if arg == "-w" || arg == "--watch" {
watch = true
continue
}

if arg == "--debug" {
debug = true
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{
Tasks: args,
ExecuteInParallel: parallel,
Watch: watch,
Debug: debug,
})
},
}

Expand Down
27 changes: 27 additions & 0 deletions docs/requirements-for-a-run-target.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
Github Issue: https://github.com/nxtcoder17/Runfile/issues/8
---

### Expectations from this implementation ?

I would like to be able to do stuffs like:
- whether these environment variables have been defined
- whether this filepath exists or not
- whether `this command` or script runs sucessfully

And, when answers to these questsions are `true`, then only run the target, otherwise throw the errors

### Problems with Taskfile.dev implementation

They have 2 ways to tackle this, with

- `requires`: but, it works with vars only, ~no environment variables~

- `preconditions`: test conditions must be a valid linux `test` command, which assumes everyone knows how to read bash's `test` or `if` statements


### My Approach

1. Support for `test` commands, must be there, for advanced users

2. But, for simpler use cases, there should be alternate ways to do it, something that whole team just understands.
32 changes: 22 additions & 10 deletions examples/Runfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,43 @@ tasks:
k2: 'f"\( asfsadfssdfas asfd $Asdfasdfa'
k3:
sh: echo -n "hello"
k4:
required: true
dotenv:
- .secrets/env
- ../.secrets/env
cmd:
- echo "hi hello"
- echo "value of k1 is '$k1'"
- echo "value of k2 is '$k2'"
- echo "value of k3 is '$k3'"
- echo "value of key_id (from .dotenv) is '$key_id', ${#key_id}"
# - sleep 5
# - echo "hi hello"
# - echo "value of k1 is '$k1'"
# - echo "value of k2 is '$k2'"
# - echo "value of k3 is '$k3'"
# - echo "value of key_id (from .dotenv) is '$key_id', ${#key_id}"
- echo "hello from cook"

clean:
name: clean
shell: ["python", "-c"]
dotenv:
- .secrets/env
- ../.secrets/env
cmd:
- run: laundry
# vars:
# k1: v1
- |+
import secrets
import os
print(os.environ['key_id'])
# print(secrets.token_hex(32))
import time
# print("key_id from env: ", os.environ['key_id'])
time.sleep(2)
print("hello from clean")
print(secrets.token_hex(32))
laundry:
name: laundry
shell: ["node", "-e"]
cmd:
- console.log("laundry")
- run: cook
- console.log("hello from laundry")
eat:
name: eat
cmd:
Expand Down
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
go_1_22

upx

gotestfmt
];

shellHook = ''
Expand Down
20 changes: 19 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,26 @@ module github.com/nxtcoder17/runfile
go 1.22.7

require (
github.com/go-task/slim-sprig/v3 v3.0.0
github.com/joho/godotenv v1.5.1
github.com/nxtcoder17/fwatcher v1.0.1
github.com/urfave/cli/v3 v3.0.0-alpha9
golang.org/x/sync v0.8.0
sigs.k8s.io/yaml v1.4.0
)

require github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.13.0 // indirect
github.com/charmbracelet/log v0.4.0 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.19.0 // indirect
)
Loading

0 comments on commit 1a1419f

Please sign in to comment.