Skip to content

Commit

Permalink
build: add tools/ghx to mage
Browse files Browse the repository at this point in the history
  • Loading branch information
aweris committed Jul 12, 2023
1 parent 7d943fd commit d43e174
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/dagger/binaries/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

// Ghx returns a dagger file for the ghx binary. It'll return an error if the binary is not available.
// Deprecated: will be removed in the with followup commits
func Ghx(ctx context.Context, client *dagger.Client, version string) (*dagger.File, error) {
var file *dagger.File

Expand Down
31 changes: 31 additions & 0 deletions internal/dagger/tools/ghx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tools

import (
"context"
"fmt"

"dagger.io/dagger"

"github.com/aweris/gale/internal/version"
)

// Ghx returns a dagger file for the ghx binary. It'll return an error if the binary is not available.
func Ghx(ctx context.Context, client *dagger.Client) (*dagger.File, error) {
v := version.GetVersion()

tag := v.GitVersion

// If the tag is a dev tag, we'll use the main branch.
if tag == "v0.0.0-dev" {
tag = "main"
}

file := client.Container().From("ghcr.io/aweris/gale/tools/ghx:" + tag).File("/ghx")

// check, if the file doesn't exist or is empty
if size, err := file.Size(ctx); size == 0 || err != nil {
return nil, fmt.Errorf("ghx@%s binary not available", tag)
}

return file, nil
}
2 changes: 1 addition & 1 deletion internal/mage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/adrg/xdg v0.4.0 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/stretchr/testify v1.8.3 // indirect
github.com/vektah/gqlparser/v2 v2.5.3 // indirect
github.com/vektah/gqlparser/v2 v2.5.6 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/tools v0.10.0 // indirect
Expand Down
3 changes: 1 addition & 2 deletions internal/mage/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/vektah/gqlparser/v2 v2.5.3 h1:goUwv4+blhtwR3GwefadPVI4ubYc/WZSypljWMQa6IE=
github.com/vektah/gqlparser/v2 v2.5.3/go.mod h1:z8xXUff237NntSuH8mLFijZ+1tjV1swDbpDqjJmk6ME=
github.com/vektah/gqlparser/v2 v2.5.6 h1:Ou14T0N1s191eRMZ1gARVqohcbe1e8FrcONScsq8cRU=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
Expand Down
3 changes: 3 additions & 0 deletions internal/mage/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ package main
import (
//mage:import services
_ "github.com/aweris/gale/internal/mage/services"

//mage:import tools
_ "github.com/aweris/gale/internal/mage/tools"
)
53 changes: 53 additions & 0 deletions internal/mage/tools/ghx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tools

import (
"context"
"fmt"
"os"

"dagger.io/dagger"

"golang.org/x/mod/semver"

"github.com/magefile/mage/mg"

"github.com/aweris/gale/internal/dagger/images"
)

type Ghx mg.Namespace

// Publish publishes the artifact service to ghcr.io/aweris/gale/services/artifact with the given version.
func (_ Ghx) Publish(ctx context.Context, version string) error {
if version != "main" {
if ok := semver.IsValid(version); !ok {
return fmt.Errorf("invalid semver tag: %s", version)
}
}

image := fmt.Sprintf("ghcr.io/aweris/gale/tools/ghx:%s", version)

client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
return err
}

file := images.GoBase(client).
WithMountedDirectory("/src", client.Host().Directory(".")).
WithWorkdir("/src/tools/ghx").
WithEnvVariable("CGO_ENABLED", "0").
WithExec([]string{"go", "build", "-o", "/src/out/ghx", "."}).
File("/src/out/ghx")

addr, err := client.Container().
From("gcr.io/distroless/static").
WithFile("/ghx", file).
WithEntrypoint([]string{"/ghx"}).
Publish(ctx, image)
if err != nil {
return err
}

fmt.Printf("Artifact service published to %s\n", addr)

return nil
}

0 comments on commit d43e174

Please sign in to comment.