Skip to content

Commit 3020367

Browse files
authored
Merge pull request #36 from nxtcoder17/feat/more-shell-supportgg
Feat/more shell supports, and shell aliases #36
2 parents 00371ae + 251e409 commit 3020367

File tree

8 files changed

+90
-41
lines changed

8 files changed

+90
-41
lines changed

docs/includes.md

-10
This file was deleted.

docs/requirements-for-a-run-target.md

-27
This file was deleted.

errors/errors.go

+2
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,5 @@ var (
174174

175175
ErrTaskInvalidCommand = Err("task invalid command")
176176
)
177+
178+
var ErrInvalidShellAlias error = Err("invalid shell alias")

examples/Runfile.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ tasks:
4343

4444
clean:
4545
name: clean
46-
shell: ["python", "-c"]
46+
# shell: ["python", "-c"]
47+
shell: python
4748
# dotenv:
4849
# - ../.secrets/env
4950
cmd:

runner/run-task.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ func createCommandGroups(ctx types.Context, args CreateCommandGroupArgs) ([]exec
144144
cg.PreExecCommand = func(cmd *exec.Cmd) {
145145
str := strings.TrimSpace(cmd.String())
146146
sp := strings.SplitN(str, " ", len(args.Task.Shell)+1)
147-
printCommand(args.Stderr, args.Task.Name, "bash", sp[2])
147+
148+
lang := "bash"
149+
if len(args.Task.Shell) > 0 {
150+
lang = args.Task.Shell[0]
151+
}
152+
printCommand(args.Stderr, args.Task.Name, lang, sp[2])
148153
}
149154

150155
cg.Commands = append(

types/parsed-types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type ParsedTask struct {
1313
// Name should be resolved from key itself
1414
Name string `json:"-"`
1515

16-
Shell []string `json:"shell"`
16+
Shell Shell `json:"shell"`
1717
WorkingDir string `json:"workingDir"`
1818
Watch *TaskWatch `json:"watch,omitempty"`
1919
Env map[string]string `json:"environ"`

types/shell.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package types
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
var shellAliasMap = map[string][]string{
9+
"bash": {"bash", "-c"},
10+
"python": {"python", "-c"},
11+
"go": {"go", "run", "-e"},
12+
"node": {"node", "-e"},
13+
"ruby": {"ruby", "-e"},
14+
"perl": {"perl", "-e"},
15+
"php": {"php", "-r"},
16+
"rust": {"cargo", "script", "-e"},
17+
"clojure": {"closure", "-e"},
18+
"lua": {"lua", "-e"},
19+
"exlixir": {"exlixir", "-e"},
20+
"powershell": {"powershell", "-Command"},
21+
"haskell": {"runghc", "-e"},
22+
}
23+
24+
type Shell []string
25+
26+
// UnmarshalJSON implements custom unmarshaling for Shell
27+
func (s *Shell) UnmarshalJSON(data []byte) error {
28+
var v any
29+
if err := json.Unmarshal(data, &v); err != nil {
30+
return fmt.Errorf("invalid shell format: %w", err)
31+
}
32+
33+
switch val := v.(type) {
34+
case string:
35+
shell, ok := shellAliasMap[val]
36+
if !ok {
37+
return fmt.Errorf("invalid shell alias")
38+
}
39+
*s = Shell(shell)
40+
case []any:
41+
// INFO: json unmarshals array as []any
42+
var shells []string
43+
for _, item := range val {
44+
str, ok := item.(string)
45+
if !ok {
46+
return fmt.Errorf("invalid shell values, must be an []string")
47+
}
48+
shells = append(shells, str)
49+
}
50+
*s = Shell(shells)
51+
default:
52+
return fmt.Errorf("unexpected JSON type for shell")
53+
}
54+
55+
return nil
56+
}
57+
58+
// func (s *Shell) UnmarshalJSON(data []byte) error {
59+
// var single string
60+
// if err := json.Unmarshal(data, &single); err == nil {
61+
// // INFO: It means shell provided is a single string i.e. shell alias
62+
// shell, ok := shellAliasMap[single]
63+
// if !ok {
64+
// return fmt.Errorf("invalid shell alias")
65+
// }
66+
// *s = Shell(shell)
67+
// return nil
68+
// }
69+
//
70+
// var multiple []string
71+
// if err := json.Unmarshal(data, &multiple); err == nil {
72+
// // If it's a slice, assign it directly
73+
// *s = Shell(multiple)
74+
// return nil
75+
// }
76+
//
77+
// return fmt.Errorf("invalid shell format")
78+
// }

types/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type Task struct {
6161
- ["python", "-c"]
6262
- ["node", "-e"]
6363
*/
64-
Shell []string `json:"shell"`
64+
Shell Shell `json:"shell"`
6565

6666
// load env vars from [.env](https://www.google.com/search?q=sample+dotenv+files&udm=2) files
6767
DotEnv []string `json:"dotenv"`

0 commit comments

Comments
 (0)