Skip to content

Commit

Permalink
feat: with override for golang actions (#205)
Browse files Browse the repository at this point in the history
* feat: with override for golang actions

Signed-off-by: Kasper J. Hermansen <[email protected]>

* feat: with docs

Signed-off-by: Kasper J. Hermansen <[email protected]>

* feat: with tests

Signed-off-by: Kasper J. Hermansen <[email protected]>

---------

Signed-off-by: Kasper J. Hermansen <[email protected]>
  • Loading branch information
kjuulh authored Dec 14, 2023
1 parent 64b0337 commit aeca37a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/features/golang-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,15 @@ Why would you want such a feature?
- A more thorough user experience can be designed.
- No longer bound to what is installed on a client machine, you don't need curl,
wget, uname, grep, yq, jq etc. installed

## Configuration

### SHUTTLE_GOLANG_ACTIONS

This variable controls whether or not to enable or disable the golang actions. This may be wanted in environments where golang or docker isn't available, or wanted.
The variable is default `true`, `false` will disable golang, and any other value will be considered `true`
### SHUTTLE_GOLANG_ACTIONS_IMAGE
This variable controls an override for proving different image for building the golang actions. The image is automatically kept up-to-date, but it may be needed to set this in the place of use, such that a race condition doesn't occur.
17 changes: 16 additions & 1 deletion pkg/executors/golang/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func compileWithDagger(ctx context.Context, ui *ui.UI, shuttlelocaldir string) (
log.Printf("nakedShuttleDir: %s", nakedShuttleDir)

shuttleBinary := client.Container().
From("golang:1.20-alpine").
From(getGolangImage()).
WithWorkdir("/app").
WithDirectory(".", src).
WithWorkdir(path.Join(nakedShuttleDir, "tmp")).
Expand Down Expand Up @@ -220,3 +220,18 @@ func goInstalled() bool {

return true
}

func getGolangImage() string {
const (
// renovate: datasource=docker depName=golang
golangImageVersion = "1.20-alpine"
)

golangImage := fmt.Sprintf("golang:%s", golangImageVersion)
golangImageOverride := os.Getenv("SHUTTLE_GOLANG_ACTIONS_IMAGE")
if golangImageOverride != "" {
return golangImageOverride
}

return golangImage
}
16 changes: 16 additions & 0 deletions pkg/executors/golang/executer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package executer

import (
"context"
"os"

"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/ui"
Expand All @@ -13,6 +14,11 @@ func List(
path string,
c *config.ShuttleProjectContext,
) (*Actions, error) {
if !isActionsEnabled() {
ui.Verboseln("shuttle golang actions disabled")
return NewActions(), nil
}

binaries, err := prepare(ctx, ui, path, c)
if err != nil {
return nil, err
Expand All @@ -33,3 +39,13 @@ func List(

return actions, nil
}

func isActionsEnabled() bool {
enabled := os.Getenv("SHUTTLE_GOLANG_ACTIONS")

if enabled == "false" {
return false
}

return true
}
33 changes: 33 additions & 0 deletions pkg/executors/golang/executer/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package executer

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsActionsEnabled(t *testing.T) {
t.Run("default is enabled", func(t *testing.T) {
t.Setenv("SHUTTLE_GOLANG_ACTIONS", "")

assert.True(t, isActionsEnabled())
})

t.Run("set false is not enabled", func(t *testing.T) {
t.Setenv("SHUTTLE_GOLANG_ACTIONS", "false")

assert.False(t, isActionsEnabled())
})

t.Run("set true is enabled", func(t *testing.T) {
t.Setenv("SHUTTLE_GOLANG_ACTIONS", "true")

assert.True(t, isActionsEnabled())
})

t.Run("set any other value is enabled", func(t *testing.T) {
t.Setenv("SHUTTLE_GOLANG_ACTIONS", "blabla")

assert.True(t, isActionsEnabled())
})
}
5 changes: 5 additions & 0 deletions pkg/executors/golang/executer/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func Run(
path string,
args ...string,
) error {
if !isActionsEnabled() {
ui.Verboseln("shuttle golang actions disabled")
return nil
}

binaries, err := prepare(ctx, ui, path, c)
if err != nil {
ui.Errorln("failed to run command: %v", err)
Expand Down
25 changes: 24 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,28 @@
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"local>lunarway/renovate-config"
],
"regexManagers": [
{
"description": "Update docker images in go files",
"fileMatch": [
"^.*\\.go$"
],
"matchStrings": [
"\\/\\/ renovate: datasource=(?<datasource>[a-z-]+?) depName=(?<depName>[a-z-]+)\\s+([a-zA-Z]*)\\s*[:|=]\\s+\"(?<currentValue>.*)\"\\,?"
],
"versioningTemplate": "docker"
}
],
"packageRules": [
{
"description": "Update docker tags frequently",
"matchDatasources": [
"docker"
],
"extends": [
"schedule:nonOfficeHours"
]
}
]
}
}

0 comments on commit aeca37a

Please sign in to comment.