-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add shuttle golang actions as an execution type (#159)
This feature adds golang as a native shell scripting alternative. This means that you are now able to use golang functions as an alternative to ```yaml scripts: build: actions: - shell: echo "something" ``` now you can create a file touch actions/build.go With a function ```go package main func Build(ctx context.Context) error { println("something") } ``` Args and shuttle variables aren't currently supported and will follow up in a later PR. This sets the stage for further development, do note that the API isn't set in stone yet.
- Loading branch information
Showing
69 changed files
with
1,249 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import "context" | ||
|
||
func Dev(ctx context.Context) error { | ||
println("dev") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module actions | ||
|
||
go 1.18 | ||
|
||
replace github.com/lunarway/shuttle => ../../../../../ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package cmder | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"reflect" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type RootCmd struct { | ||
Cmds []*Cmd | ||
} | ||
|
||
func NewRoot() *RootCmd { | ||
cmd := &RootCmd{} | ||
|
||
return cmd | ||
} | ||
|
||
func (rc *RootCmd) AddCmds(cmd ...*Cmd) *RootCmd { | ||
rc.Cmds = append(rc.Cmds, cmd...) | ||
|
||
return rc | ||
} | ||
|
||
func (rc *RootCmd) Execute() { | ||
rootcmd := &cobra.Command{Use: "actions"} | ||
|
||
rootcmd.AddCommand( | ||
&cobra.Command{Use: "ls", RunE: func(cmd *cobra.Command, args []string) error { | ||
return cmd.Parent().Help() | ||
}}, | ||
) | ||
|
||
rootcmd.AddCommand(&cobra.Command{Hidden: true, Use: "lsjson", RunE: func(cmd *cobra.Command, args []string) error { | ||
cmdNames := make([]string, len(rc.Cmds)) | ||
for i, cmd := range rc.Cmds { | ||
cmd := cmd | ||
cmdNames[i] = cmd.Name | ||
} | ||
|
||
rawJson, err := json.Marshal(cmdNames) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Prints the commands and args as json to stdout | ||
_, err = fmt.Printf("%s", string(rawJson)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}}) | ||
|
||
for _, cmd := range rc.Cmds { | ||
cmd := cmd | ||
parameters := make([]string, len(cmd.Args)) | ||
|
||
cobracmd := &cobra.Command{ | ||
Use: cmd.Name, | ||
RunE: func(cobracmd *cobra.Command, args []string) error { | ||
if err := cobracmd.ParseFlags(args); err != nil { | ||
return err | ||
} | ||
|
||
inputs := make([]reflect.Value, 0, len(cmd.Args)+1) | ||
inputs = append(inputs, reflect.ValueOf(context.Background())) | ||
for _, arg := range parameters { | ||
inputs = append(inputs, reflect.ValueOf(arg)) | ||
} | ||
|
||
reflect. | ||
ValueOf(cmd.Func). | ||
Call(inputs) | ||
return nil | ||
}, | ||
} | ||
for i, arg := range cmd.Args { | ||
cobracmd.PersistentFlags().StringVar(¶meters[i], arg.Name, "", "") | ||
_ = cobracmd.MarkPersistentFlagRequired(arg.Name) | ||
} | ||
|
||
rootcmd.AddCommand(cobracmd) | ||
} | ||
|
||
if err := rootcmd.Execute(); err != nil { | ||
log.Fatalf("%v", err) | ||
} | ||
} | ||
|
||
type Arg struct { | ||
Name string | ||
} | ||
|
||
type Cmd struct { | ||
Name string | ||
Func any | ||
Args []Arg | ||
} | ||
|
||
func NewCmd(name string, f any) *Cmd { | ||
return &Cmd{ | ||
Name: name, | ||
Func: f, | ||
Args: []Arg{}, | ||
} | ||
} | ||
|
||
func WithArgs(cmd *Cmd, argName string) *Cmd { | ||
cmd.Args = append(cmd.Args, Arg{Name: argName}) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package codegen | ||
|
||
import ( | ||
"context" | ||
"embed" | ||
"html/template" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/lunarway/shuttle/pkg/executors/golang/discover" | ||
"github.com/lunarway/shuttle/pkg/executors/golang/parser" | ||
) | ||
|
||
var ( | ||
//go:embed templates/mainFile.tmpl | ||
mainFileTmpl embed.FS | ||
) | ||
|
||
func GenerateMainFile( | ||
ctx context.Context, | ||
shuttlelocaldir string, | ||
actions *discover.ActionsDiscovered, | ||
functions []*parser.Function, | ||
) error { | ||
tmpmainfile := path.Join(shuttlelocaldir, "tmp/main.go") | ||
|
||
file, err := os.Create(tmpmainfile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tmpl := template. | ||
Must( | ||
template. | ||
New("mainFile.tmpl"). | ||
Funcs(map[string]any{ | ||
"lower": strings.ToLower, | ||
}). | ||
ParseFS(mainFileTmpl, "templates/mainFile.tmpl"), | ||
) | ||
|
||
err = tmpl.Execute(file, functions) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package codegen | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
"path" | ||
|
||
"github.com/lunarway/shuttle/pkg/ui" | ||
) | ||
|
||
func CompileBinary(ctx context.Context, ui *ui.UI, shuttlelocaldir string) (string, error) { | ||
cmd := exec.Command("go", "build") | ||
cmd.Dir = path.Join(shuttlelocaldir, "tmp") | ||
|
||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
ui.Verboseln("compile-binary output: %s", string(output)) | ||
return "", err | ||
} | ||
|
||
return path.Join(shuttlelocaldir, "tmp", "actions"), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package codegen | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
"path" | ||
|
||
"github.com/lunarway/shuttle/pkg/ui" | ||
) | ||
|
||
func Format(ctx context.Context, ui *ui.UI, shuttlelocaldir string) error { | ||
cmd := exec.Command("go", "fmt", "./...") | ||
cmd.Dir = path.Join(shuttlelocaldir, "tmp") | ||
|
||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
ui.Errorln("go fmt: %s, error: %v", string(output), err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.