Skip to content

Commit

Permalink
Merge pull request #9 from eunomie/docker-plugin
Browse files Browse the repository at this point in the history
refactor: make dague a docker cli plugin
  • Loading branch information
eunomie authored Dec 11, 2022
2 parents c44e61e + 99d88da commit 4d7bd78
Show file tree
Hide file tree
Showing 40 changed files with 899 additions and 2,019 deletions.
48 changes: 17 additions & 31 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ jobs:
with:
go-version: '1.19'
- name: bootstrap binary
run: go build -o dague ./cmd/dague
run: go build -o docker-dague ./cmd/docker-dague
- uses: actions/upload-artifact@v3
with:
name: dague-bin
path: ./dague
path: ./docker-dague

godoc:
needs: [build]
Expand All @@ -36,7 +36,7 @@ jobs:
with:
name: dague-bin
- name: Check docs
run: chmod +x ./dague; ./dague go doc -c
run: chmod +x ./docker-dague; ./docker-dague dague go:doc --check

govuln:
needs: [build]
Expand All @@ -48,7 +48,7 @@ jobs:
with:
name: dague-bin
- name: Check govuln
run: chmod +x ./dague; ./dague lint govuln
run: chmod +x ./docker-dague; ./docker-dague dague lint:govuln

gofumpt:
needs: [build]
Expand All @@ -60,66 +60,53 @@ jobs:
with:
name: dague-bin
- name: gofumpt
run: chmod +x ./dague; ./dague gofumpt print
run: chmod +x ./docker-dague; ./docker-dague dague fmt:print

build-cross:
needs: [build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: docker/setup-qemu-action@v1
- uses: actions/download-artifact@v2
with:
name: dague-bin
- name: Find new version
id: new_version
uses: mathieudutour/github-tag-action@v6.1
with:
dry_run: true
release_branches: main
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Cross build binaries
run: chmod +x ./dague; ./dague go cross ./cmd/dague
run: chmod +x ./docker-dague; ./docker-dague dague go:cross --ldflags="-X 'github.com/eunomie/dague/internal.Version=${{ steps.new_version.outputs.new_version }}'" ./cmd/docker-dague
- name: Upload artifacts
if: github.event_name == 'push'
uses: actions/upload-artifact@v3
with:
name: dague
path: ./dist/

docker:
release:
runs-on: ubuntu-latest
needs: [godoc, govuln, gofumpt, build-cross]
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: docker/setup-qemu-action@v1
- uses: docker/setup-buildx-action@v1
- if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# Find the new version first, and only create the tag when the image has been built and pushed
- name: Find new version
id: new_version
uses: mathieudutour/github-tag-action@v6.1
with:
dry_run: true
release_branches: main
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: eunomie/dague
tags: |
type=semver,pattern={{version}},value=${{steps.new_version.outputs.new_tag}}
type=semver,pattern={{major}}.{{minor}},value=${{steps.new_version.outputs.new_tag}}
- name: Build and push image
uses: docker/build-push-action@v3
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Tag version
if: github.event_name == 'push'
uses: mathieudutour/github-tag-action@v6.1
with:
custom_tag: ${{ steps.new_version.outputs.new_version }}
Expand All @@ -131,7 +118,6 @@ jobs:
name: dague
path: dist
- name: Create a GitHub release
if: github.event_name == 'push'
uses: ncipollo/release-action@v1
with:
artifacts: "dist/*"
Expand Down
10 changes: 0 additions & 10 deletions Dockerfile

This file was deleted.

140 changes: 28 additions & 112 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,130 +4,39 @@ Build Go projects, better. Based on [`dagger`](https://dagger.io).

## Why?

Especially when you're working on multiple projects, there's always the question of the tooling, the different version
of all the tools, do you have all the needed requirements, etc.

## Usage in Other Projects

You can use the default `dague` generated tool from the GitHub release page. It will give you the basic, the default tools.
You just have to copy it and run it.

But it has been done in a way to be easily extensible to support your own project. For instance, maybe you have a specific
target to build. Or you need to set some variable. Or anything else.

One of the key idea behind `dague` is to be a helper to build your own custom tool, for your own need. You just have to
pick the targets you want, and add yours.

Two different integrations are provided by default:

- one based on `kong`, to build CLIs easily
- one based on `mage`, if you want something more `make` style

### Kong

[`kong`](./kong) package and sub packages provides you commands and sub commands to build your own CLI.

For instance, look at [`cmd/dague/main.go`](./cmd/dague/main.go) to see how to create a CLI with the default targets:

```go
package main

import (
"github.com/eunomie/dague/kong"
"github.com/eunomie/dague/kong/gofumpt"
"github.com/eunomie/dague/kong/golang"
"github.com/eunomie/dague/kong/lint"
)

type (
CLI struct {
Lint lint.Lint `cmd:""`
Gofumpt gofumpt.Gofumpt `cmd:""`
Go golang.Golang `cmd:""`
}
)

func main() {
kong.Run(&CLI{})
}
```

That's all you need to have this CLI:
`dague` is a `docker` cli plugin. It acts as a opinionated Go toolchain only relying on Docker as dependency.

```text
Usage: dague <command>
You don't need to have the right version of Go or any other dependencies, if you have Docker you have everything.

Flags:
-h, --help Show context-sensitive help.

lint
lint govuln
checks vulnerabilities in Go code

gofumpt
gofumpt print
print result of gofumpt
gofumpt write
write result of gofumpt to existing files
golang
go deps
download go modules
go mod
run go mod tidy and export go.mod and go.sum files
go test
run go tests
go doc
generate to documentation in README.md files
Especially when you're working on multiple projects, there's always the question of the tooling, the different versions
of all the tools, do you have all the needed requirements, etc.

go build <in>
compile go code and export it for the local architecture
## Usage

go cross <in>
compile go code and export it for multiple architectures
Just drop `docker-dague` binary in your `~/.docker/cli-plugin` directory and you're setup.

Run "dague <command> --help" for more information on a command.
```
❯ docker dague --help
### Mage
Usage: docker dague COMMAND
The same way as with `kong`, you can create your own tool using `mage`.
Docker Dague
Here is a `magefile` example that will provide all the default tools:
Commands:
fmt:print Print result of gofumpt
fmt:write Write result of gofumpt to existing files
go:build Compile go code and export it for the local architecture
go:cross Compile go code and export it for multiple architectures
go:deps Download go modules
go:doc Generate Go documentation into readme files
go:mod Run go mod tidy and export go.mod and go.sum files
go:test Run go tests
lint:govuln Lint Go code using govulncheck
version Print version
```go
//go:build mage

package main

import (
//mage:import
_ "github.com/eunomie/dague/mage/golang"
//mage:import
_ "github.com/eunomie/dague/mage/lint"
//mage:import
_ "github.com/eunomie/dague/mage/gofumpt"
)
```

And here is the corresponding mage targets:

```text
Targets:
go:checkDoc verifies the documentation is up-to-date
go:cross compiles go code from target and export it into dist/ for multiple architectures (linux|darwin|windows)/(amd64|arm64)
go:deps downloads go modules
go:doc generates go documentation in README.md files
go:local compiles go code from target and export it into dist/ folder for the local architecture
go:mod runs go mod tidy and export go.mod and go.sum files
go:test runs go tests
gofumpt:print runs gofumpt and print the recommended changes
gofumpt:write runs gofumpt and write the recommended changes
lint:govuln checks vulnerabilities in Go code
Run 'docker dague COMMAND --help' for more information on a command.
```

<!-- gomarkdoc:embed:start -->
Expand All @@ -148,6 +57,7 @@ import "github.com/eunomie/dague"
- [func Exec(ctx context.Context, cont *dagger.Container, opts dagger.ContainerExecOpts) error](<#func-exec>)
- [func ExecCont(ctx context.Context, src *dagger.Container, opts dagger.ContainerExecOpts) (*dagger.Container, error)](<#func-execcont>)
- [func ExecOut(ctx context.Context, src *dagger.Container, opts dagger.ContainerExecOpts) (string, string, error)](<#func-execout>)
- [func ExportFilePattern(ctx context.Context, cont *dagger.Container, pattern, path string) error](<#func-exportfilepattern>)
- [func ExportGoMod(ctx context.Context, cont *dagger.Container, contDir, exportDir string) error](<#func-exportgomod>)
- [func GoInstall(packages ...string) dagger.ContainerExecOpts](<#func-goinstall>)
- [func GoModDownload() dagger.ContainerExecOpts](<#func-gomoddownload>)
Expand Down Expand Up @@ -228,6 +138,12 @@ stdout, stderr, err := dague.ExecOut(ctx, c.Container().From("golang"), dagger.C
})
```

## func ExportFilePattern

```go
func ExportFilePattern(ctx context.Context, cont *dagger.Container, pattern, path string) error
```

## func ExportGoMod

```go
Expand Down
20 changes: 0 additions & 20 deletions cmd/dague/main.go

This file was deleted.

23 changes: 14 additions & 9 deletions cmd/dague/README.md → cmd/docker-dague/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@

<!-- Code generated by gomarkdoc. DO NOT EDIT -->

# dague
# docker\-dague

```go
import "github.com/eunomie/dague/cmd/dague"
import "github.com/eunomie/dague/cmd/docker-dague"
```

## Index

- [Constants](<#constants>)
- [func main()](<#func-main>)
- [type CLI](<#type-cli>)
- [func pluginMain()](<#func-pluginmain>)


## Constants

```go
const (
PluginName = "dague"
)
```

## func main

```go
func main()
```

## type CLI
## func pluginMain

```go
type CLI struct {
Lint lint.Lint `cmd:""`
Gofumpt gofumpt.Gofumpt `cmd:""`
Go golang.Golang `cmd:""`
}
func pluginMain()
```


Expand Down
Loading

0 comments on commit 4d7bd78

Please sign in to comment.