Skip to content

Commit 1a57a73

Browse files
author
Kasper Juul Hermansen
authored
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.
1 parent 4d93f7b commit 1a57a73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1249
-8
lines changed

cmd/cmd.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package cmd
22

33
import (
4+
stdcontext "context"
45
"fmt"
56
"io"
67
"log"
78
"os"
89
"path"
910

11+
"github.com/lunarway/shuttle/pkg/executors/golang/executer"
1012
"github.com/lunarway/shuttle/pkg/ui"
1113

1214
"github.com/lunarway/shuttle/pkg/config"
@@ -173,9 +175,29 @@ func getProjectContext(rootCmd *cobra.Command, uii *ui.UI, projectPath string, c
173175
}
174176

175177
var c config.ShuttleProjectContext
176-
_, err = c.Setup(fullProjectPath, uii, clean, skipGitPlanPulling, plan, projectFlagSet)
178+
projectContext, err := c.Setup(fullProjectPath, uii, clean, skipGitPlanPulling, plan, projectFlagSet)
177179
if err != nil {
178180
return config.ShuttleProjectContext{}, err
179181
}
182+
183+
ctx := stdcontext.Background()
184+
taskActions, err := executer.List(ctx, uii, fmt.Sprintf("%s/shuttle.yaml", projectContext.ProjectPath), &c)
185+
if err != nil {
186+
return config.ShuttleProjectContext{}, err
187+
}
188+
189+
for name := range taskActions {
190+
c.Scripts[name] = config.ShuttlePlanScript{
191+
Description: name,
192+
Actions: []config.ShuttleAction{
193+
{
194+
Task: name,
195+
},
196+
},
197+
198+
Args: []config.ShuttleScriptArgs{},
199+
}
200+
}
201+
180202
return c, nil
181203
}

cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func newRun(uii *ui.UI, contextProvider contextProvider) *cobra.Command {
1616
validateArgs bool
1717
)
1818

19-
executorRegistry := executors.NewRegistry(executors.ShellExecutor)
19+
executorRegistry := executors.NewRegistry(executors.ShellExecutor, executors.TaskExecutor)
2020

2121
runCmd := &cobra.Command{
2222
Use: "run [command]",

examples/moon-base/shuttle.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ scripts:
1010
serve:
1111
description: Serve starts an nginx image as a long running process.
1212
actions:
13-
- shell: docker run --rm -i --name shuttle-nginx nginx
13+
- shell: docker run --rm -i --name shuttle-nginx nginx
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import "context"
4+
5+
func Dev(ctx context.Context) error {
6+
println("dev")
7+
8+
return nil
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module actions
2+
3+
go 1.18
4+
5+
replace github.com/lunarway/shuttle => ../../../../../

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ require (
1515
gopkg.in/yaml.v2 v2.4.0
1616
)
1717

18+
require (
19+
github.com/otiai10/copy v1.9.0
20+
golang.org/x/mod v0.6.0
21+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
22+
)
23+
1824
require (
1925
github.com/Masterminds/goutils v1.1.1 // indirect
2026
github.com/Masterminds/semver/v3 v3.2.0 // indirect
@@ -29,5 +35,6 @@ require (
2935
github.com/spf13/cast v1.4.1 // indirect
3036
github.com/spf13/pflag v1.0.5 // indirect
3137
golang.org/x/crypto v0.3.0 // indirect
38+
golang.org/x/sys v0.2.0 // indirect
3239
gopkg.in/yaml.v3 v3.0.1 // indirect
3340
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ github.com/mitchellh/copystructure v1.1.1/go.mod h1:EBArHfARyrSWO/+Wyr9zwEkc6XMF
3030
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
3131
github.com/mitchellh/reflectwalk v1.0.1 h1:FVzMWA5RllMAKIdUSC8mdWo3XtwoecrH79BY70sEEpE=
3232
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
33+
github.com/otiai10/copy v1.9.0 h1:7KFNiCgZ91Ru4qW4CWPf/7jqtxLagGRmIxWldPP9VY4=
34+
github.com/otiai10/copy v1.9.0/go.mod h1:hsfX19wcn0UWIHUQ3/4fHuehhk2UyArQ9dVFAn3FczI=
35+
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
36+
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
37+
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
38+
github.com/otiai10/mint v1.4.0 h1:umwcf7gbpEwf7WFzqmWwSv0CzbeMsae2u9ZvpP8j2q4=
39+
github.com/otiai10/mint v1.4.0/go.mod h1:gifjb2MYOoULtKLqUAEILUG/9KONW6f7YsJ6vQLTlFI=
3340
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
3441
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3542
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -61,17 +68,22 @@ golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4
6168
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
6269
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
6370
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
71+
golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
72+
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
6473
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
6574
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
6675
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
6776
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
6877
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
78+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
6979
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
7080
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7181
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7282
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7383
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
84+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7485
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
86+
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
7587
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7688
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
7789
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

pkg/config/shuttleconfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func (c *ShuttleProjectContext) Setup(projectPath string, uii *ui.UI, clean bool
6666
if err != nil {
6767
return nil, err
6868
}
69+
6970
c.Scripts = make(map[string]ShuttlePlanScript)
7071
for scriptName, script := range c.Plan.Scripts {
7172
c.Scripts[scriptName] = script

pkg/config/shuttleplan.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (a ShuttleScriptArgs) String() string {
4545
type ShuttleAction struct {
4646
Shell string `yaml:"shell"`
4747
Dockerfile string `yaml:"dockerfile"`
48+
Task string `yaml:"task"`
4849
}
4950

5051
// ShuttlePlanConfiguration is a ShuttlePlan sub-element

pkg/executors/executor.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88

99
"github.com/lunarway/shuttle/pkg/config"
1010
"github.com/lunarway/shuttle/pkg/errors"
11+
"github.com/lunarway/shuttle/pkg/ui"
1112
)
1213

1314
type Registry struct {
1415
executors []Matcher
1516
}
1617

1718
type Matcher func(config.ShuttleAction) (Executor, bool)
18-
type Executor func(context.Context, ActionExecutionContext) error
19+
type Executor func(context.Context, *ui.UI, ActionExecutionContext) error
1920

2021
func NewRegistry(executors ...Matcher) *Registry {
2122
return &Registry{
@@ -63,7 +64,7 @@ func (r *Registry) Execute(ctx context.Context, p config.ShuttleProjectContext,
6364
Action: action,
6465
ActionIndex: actionIndex,
6566
}
66-
err := r.executeAction(ctx, actionContext)
67+
err := r.executeAction(ctx, p.UI, actionContext)
6768
if err != nil {
6869
return err
6970
}
@@ -176,11 +177,11 @@ func expectedArgumentsHelp(command string, args []config.ShuttleScriptArgs) stri
176177
return s.String()
177178
}
178179

179-
func (r *Registry) executeAction(ctx context.Context, context ActionExecutionContext) error {
180+
func (r *Registry) executeAction(ctx context.Context, ui *ui.UI, context ActionExecutionContext) error {
180181
for _, executor := range r.executors {
181182
handler, ok := executor(context.Action)
182183
if ok {
183-
return handler(ctx, context)
184+
return handler(ctx, ui, context)
184185
}
185186
}
186187

0 commit comments

Comments
 (0)