Skip to content

Commit

Permalink
Merge pull request #12 from eunomie/env-vars
Browse files Browse the repository at this point in the history
Manage env vars and expand
  • Loading branch information
eunomie authored Dec 23, 2022
2 parents 4de27a2 + 0f2c09b commit 06d5789
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 99 deletions.
96 changes: 56 additions & 40 deletions .dague.reference.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,59 @@
# Configuration of the code formatter to use
fmt:
# The tool to use. Default if gofmt, other possible value is gofumpt
tool: gofumpt

lint:
govulncheck:
enable: true
golangci:
enable: true
image: golangci/golangci-lint:v1.50.1

# Go configuration
# Go related configuration
go:
# Image used to build and run almost everything
# Base image used to build and run tools
image: golang:1.19.4-alpine3.17
# Directory where to mount the local folder
# Directory to mount files
appDir: /go/src
# List of (sub)folders that are go modules (to download, tidy)
modules:
- path: .
files: ["go.mod", "go.sum"]
- path: ./cmd/docker-dague
files: ["go.mod", "go.sum"]
# List of targets to build, with parameters
targets:
- name: local
type: local
path: ./cmd/docker-dague
out: ./dist
ldflags: -s -w -X 'github.com/eunomie/dague/internal.Version=${DAGUE_SET_VERSION:-dev}'
- name: cross
type: cross
path: ./cmd/docker-dague
out: ./dist
ldflags: -s -w -X 'github.com/eunomie/dague/internal.Version=${DAGUE_SET_VERSION:-dev}'
platforms:
- linux/amd64
- linux/arm64
- darwin/amd64
- darwin/arm64
- windows/amd64
- windows/arm64
# Configuration of formatters
fmt:
# Choice of the formatter, can be gofmt or gofumpt
formatter: gofumpt
# Configuration of goimports tool, that is run before the formatter
goimports:
# Configuration of locals to group them after 3rd party imports
locals:
- github.com/docker
- github.com/eunomie/dague

# Configuration of linters
lint:
# Govulncheck, can be enabled or disabled
govulncheck:
enable: true
# Golangci-lint, can be enabled or disabled
golangci:
enable: true
# Golangci-lint image to use
image: golangci/golangci-lint:v1.50.1

# Build configuration
build:
# List of targets to build by their name
targets:
- &dague-build # This identifier will be re-used to avoid duplication
name: local
# Type can be local (using local os and arch as target) or cross (to enable multi-platform builds)
type: local
# Relative path to build
path: ./cmd/docker-dague
# Relative folder to put the generate files
out: ./dist
# List of environment variables
env:
# Could be a static value
CGO_ENABLED: 0
# Or a shell command to execute, if starts with $
GIT_COMMIT: $ git describe --tags | cut -c 2-
# Ldflags to use to build. Environment variable will be expanded.
ldflags: -s -w -X 'github.com/eunomie/dague/internal.Version=${GIT_COMMIT:-dev}'
- << : *dague-build # Copy all from above target and specify some values
name: cross
type: cross
# Defines the list of platforms to build
platforms:
- linux/amd64
- linux/arm64
- darwin/amd64
- darwin/arm64
- windows/amd64
- windows/arm64
8 changes: 2 additions & 6 deletions .dague.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
go:
image: golang:1.19.4-alpine3.17
appDir: /go/src
fmt:
formatter: gofumpt
goimports:
locals:
- github.com/docker
- github.com/eunomie/dague

lint:
govulncheck:
enable: true
golangci:
enable: true
image: golangci/golangci-lint:v1.50.1

build:
Expand All @@ -24,7 +19,8 @@ go:
out: ./dist
env:
CGO_ENABLED: 0
ldflags: -s -w -X 'github.com/eunomie/dague/internal.Version=${DAGUE_SET_VERSION}'
GIT_COMMIT: $ git describe --tags | cut -c 2-
ldflags: -s -w -X 'github.com/eunomie/dague/internal.Version=${GIT_COMMIT:-dev}'
- << : *dague-build
name: cross
type: cross
Expand Down
7 changes: 7 additions & 0 deletions cmd/docker-dague/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import "github.com/eunomie/dague/cmd/docker-dague/commands"
- [func LintGovuln(conf *config.Dague) *cobra.Command](<#func-lintgovuln>)
- [func Version() *cobra.Command](<#func-version>)
- [func VersionCommands(_ *config.Dague) []*cobra.Command](<#func-versioncommands>)
- [func interpretShell(ctx context.Context, cmd string, env map[string]string) (string, error)](<#func-interpretshell>)
- [type goDocOptions](<#type-godocoptions>)


Expand Down Expand Up @@ -156,6 +157,12 @@ func Version() *cobra.Command
func VersionCommands(_ *config.Dague) []*cobra.Command
```

## func interpretShell

```go
func interpretShell(ctx context.Context, cmd string, env map[string]string) (string, error)
```

## type goDocOptions

```go
Expand Down
75 changes: 62 additions & 13 deletions cmd/docker-dague/commands/golang.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package commands

import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"strings"

"mvdan.cc/sh/v3/syntax"

"mvdan.cc/sh/v3/expand"

"mvdan.cc/sh/v3/interp"

"github.com/spf13/cobra"

"mvdan.cc/sh/v3/shell"

"github.com/eunomie/dague/config"
"github.com/eunomie/dague/daggers"
"github.com/eunomie/dague/types"
Expand Down Expand Up @@ -126,20 +136,38 @@ func GoBuild(conf *config.Dague) *cobra.Command {
return fmt.Errorf("could not find the target %q to build", targetName)
}

env := map[string]string{}

for k, v := range target.Env {
if strings.HasPrefix(v, "$ ") {
shellCmd := strings.TrimPrefix(v, "$ ")
value, err := interpretShell(ctx, shellCmd, env)
if err != nil {
return err
}
env[k] = value
} else {
env[k] = v
}
}

var buildFlags []string
if target.Ldflags != "" {
buildFlags = append(buildFlags, "-ldflags="+os.ExpandEnv(target.Ldflags))
flags, err := shell.Expand(target.Ldflags, func(s string) string {
return env[s]
})
if err != nil {
return err
}
buildFlags = append(buildFlags, "-ldflags="+flags)
}
return daggers.RunInDagger(ctx, conf, func(c *daggers.Client) error {
if target.Type == "local" {
return daggers.LocalBuild(ctx, c, types.LocalBuildOpts{
BuildOpts: types.BuildOpts{
Dir: target.Out,
In: target.Path,
EnvVars: map[string]string{
"CGO_ENABLED": "0",
"GO11MODULE": "auto",
},
Dir: target.Out,
In: target.Path,
EnvVars: env,
BuildFlags: buildFlags,
},
Out: filepath.Base(target.Path),
Expand All @@ -152,12 +180,9 @@ func GoBuild(conf *config.Dague) *cobra.Command {
}
return daggers.CrossBuild(ctx, c, types.CrossBuildOpts{
BuildOpts: types.BuildOpts{
Dir: target.Out,
In: target.Path,
EnvVars: map[string]string{
"CGO_ENABLED": "0",
"GO11MODULE": "auto",
},
Dir: target.Out,
In: target.Path,
EnvVars: env,
BuildFlags: buildFlags,
},
OutFileFormat: filepath.Base(target.Path) + "_%s_%s",
Expand All @@ -169,3 +194,27 @@ func GoBuild(conf *config.Dague) *cobra.Command {

return cmd
}

func interpretShell(ctx context.Context, cmd string, env map[string]string) (string, error) {
script, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
if err != nil {
return "", err
}

out := bytes.NewBufferString("")

pairs := os.Environ()
for k, v := range env {
pairs = append(pairs, fmt.Sprintf("%s=%s", k, v))
}
runner, err := interp.New(interp.Env(expand.ListEnviron(pairs...)), interp.StdIO(nil, out, out))
if err != nil {
return "", err
}

if err = runner.Run(ctx, script); err != nil {
return "", err
}

return strings.TrimSpace(out.String()), nil
}
Loading

0 comments on commit 06d5789

Please sign in to comment.