Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
Skipping container build if containerfile is not present (#5)
Browse files Browse the repository at this point in the history
* Skipping container build if containerfile is not present

* Support for task parts

* Upgrade version of golangci-lint

* Satisfy golangci-lint (makezero)
  • Loading branch information
cardil authored Jan 18, 2021
1 parent b550216 commit 9a5a4c1
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.31
version: v1.35.2
editorconfig:
name: EditorConfig
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion cleaning.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func Clean() {
t := tasks.Start("🚿", "Cleaning")
err := os.RemoveAll(internal.BuildDir())
errs := make([]error, 1)
errs := make([]error, 0, 1)
errs = append(errs, err)
for _, task := range config.CleaningTasks {
err = task()
Expand Down
9 changes: 5 additions & 4 deletions container/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"os/exec"
"path"
"strings"
"regexp"

"github.com/wavesoftware/go-magetasks/config"
)
Expand All @@ -25,7 +25,8 @@ func containerFile(bin config.Binary) string {
func imageName(bin config.Binary) string {
defBasename := fmt.Sprintf("localhost/%s", getenv("USER", "Anonymous"))
basename := getenv("CONTAINER_BASENAME", defBasename)
if !strings.HasSuffix(basename, "/") {
r := regexp.MustCompile(".+[A-Za-z0-9]$")
if r.MatchString(basename) {
basename += "/"
}
return basename + bin.Name
Expand All @@ -36,9 +37,9 @@ func resolveContainerEngine() (string, error) {
"podman", "docker",
}
for _, candidate := range candidates {
path, err := exec.LookPath(candidate)
binPath, err := exec.LookPath(candidate)
if err == nil {
return path, nil
return binPath, nil
}
}
return "", errNoContainerEngineInstalled
Expand Down
26 changes: 19 additions & 7 deletions container/images.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
package container

import (
"fmt"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/wavesoftware/go-ensure"
"github.com/wavesoftware/go-magetasks"
"github.com/wavesoftware/go-magetasks/config"
"github.com/wavesoftware/go-magetasks/internal"
"github.com/wavesoftware/go-magetasks/pkg/tasks"
)

// Images builds a OCI images of binaries.
func Images() {
mg.Deps(magetasks.Binary)

ce, err := resolveContainerEngine()
ensure.NoError(err)
if len(config.Binaries) > 0 {
t := tasks.StartMultiline("📦", "Packaging OCI images")
errs := make([]error, 0)
for _, binary := range config.Binaries {
p := t.Part(binary.Name)
cf := containerFile(binary)
if internal.DontExists(cf) {
p.Skip(fmt.Sprintf("containerfile %s don't exist", cf))
continue
}
st := p.Starting()
args := []string{
"build",
"-f", containerFile(binary),
"-t", imageName(binary),
".",
"build", "-f", cf, "-t", imageName(binary), ".",
}
err := sh.RunV(ce, args...)
err := sh.RunV(containerEngine(), args...)
st.Done(err)
errs = append(errs, err)
}
t.End(errs...)
}
}

func containerEngine() string {
ce, err := resolveContainerEngine()
ensure.NoError(err)
return ce
}
12 changes: 6 additions & 6 deletions container/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ package container
import (
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/wavesoftware/go-ensure"
"github.com/wavesoftware/go-magetasks/config"
"github.com/wavesoftware/go-magetasks/internal"
"github.com/wavesoftware/go-magetasks/pkg/tasks"
)

// Publish publishes built images to a remote registry.
func Publish() {
mg.Deps(Images)

ce, err := resolveContainerEngine()
ensure.NoError(err)
if len(config.Binaries) > 0 {
t := tasks.StartMultiline("📤", "Publishing OCI images")
errs := make([]error, 0)
for _, binary := range config.Binaries {
args := []string{
"push", imageName(binary),
cf := containerFile(binary)
if internal.DontExists(cf) {
continue
}
err := sh.RunV(ce, args...)
args := []string{"push", imageName(binary)}
err := sh.RunV(containerEngine(), args...)
errs = append(errs, err)
}
t.End(errs...)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
github.com/fatih/color v1.9.0
github.com/hashicorp/go-multierror v1.1.0
github.com/joho/godotenv v1.3.0
github.com/magefile/mage v1.10.0
github.com/stretchr/testify v1.6.1
github.com/wavesoftware/go-ensure v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/U
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
Expand Down
6 changes: 6 additions & 0 deletions internal/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ func EnsureBuildDir() {
d := path.Join(BuildDir(), "bin")
ensure.NoError(os.MkdirAll(d, os.ModePerm))
}

// DontExists will check if target file dont exist.
func DontExists(file string) bool {
_, err := os.Stat(file)
return err != nil && os.IsNotExist(err)
}
57 changes: 55 additions & 2 deletions pkg/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"github.com/wavesoftware/go-ensure"
)

var (
red = color.New(color.FgHiRed).Add(color.Bold).SprintFunc()
green = color.New(color.FgHiGreen).Add(color.Bold).SprintFunc()
yellow = color.New(color.FgHiYellow).Add(color.Bold).SprintFunc()
)

// Task represents a mage task enriched with icon and description that might
// be multiline.
type Task struct {
Expand All @@ -16,6 +22,47 @@ type Task struct {
multiline bool
}

// Part represents a part of a bigger Task.
type Part struct {
name string
t *Task
}

// PartProcessing is an interface that is used to process long running part of
// tasks.
type PartProcessing interface {
Done(err error)
}

// Skip print a warning message.
func (p *Part) Skip(reason string) {
if p.t.multiline {
msg := fmt.Sprintf(" %s › %s is skipped due to %s\n", p.t.icon, p.name, reason)
fmt.Print(mageTag() + yellow(msg))
}
}

type partProcessing struct {
p *Part
}

// Done is reporting a completeness of part processing.
func (p *partProcessing) Done(err error) {
if p.p.t.multiline && err != nil {
msg := fmt.Sprintf("%s have failed: %v\n", p.p.name, err)
fmt.Print(mageTag() + red(msg))
}
}

// Starting starts a part processing.
func (p *Part) Starting() PartProcessing {
if p.t.multiline {
msg := fmt.Sprintf(" %s › %s\n", p.t.icon, p.name)
fmt.Print(mageTag() + msg)
}
return &partProcessing{p: p}
}

// Start will start a single line task.
func Start(icon, action string) *Task {
t := &Task{
Expand Down Expand Up @@ -61,16 +108,22 @@ func (t *Task) End(errs ...error) {
ensure.NoError(err)
}

// Part create a part that can be reported further.
func (t *Task) Part(part string) *Part {
return &Part{
name: part,
t: t,
}
}

func erroneousMsg(t *Task) string {
red := color.New(color.FgHiRed).Add(color.Bold).SprintFunc()
if t.multiline {
return mageTag() + red(fmt.Sprintf(" %s have failed!\n", t.action))
}
return red(fmt.Sprintln("failed!"))
}

func successfulMsg(t *Task) string {
green := color.New(color.FgHiGreen).Add(color.Bold).SprintFunc()
if t.multiline {
return mageTag() + green(fmt.Sprintf(" %s was successful.\n", t.action))
}
Expand Down
1 change: 1 addition & 0 deletions tests/example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONTAINER_BASENAME=quay.io/cardil/magetasks-example-
14 changes: 11 additions & 3 deletions tests/example/Magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
package main

import (
"log"

"github.com/joho/godotenv"
// mage:import
"github.com/wavesoftware/go-magetasks"
"github.com/wavesoftware/go-magetasks/config"
Expand All @@ -16,8 +19,13 @@ import (
var Default = magetasks.Binary

func init() {
config.Binaries = append(config.Binaries, config.Binary{
Name: "dummy",
})
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
bins := []string{"dummy", "other"}
for _, bin := range bins {
config.Binaries = append(config.Binaries, config.Binary{Name: bin})
}
config.VersionVariablePath = "github.com/wavesoftware/go-magetasks/tests/example/internal.Version"
}
11 changes: 11 additions & 0 deletions tests/example/cmd/other/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"

"github.com/wavesoftware/go-magetasks/tests/example/internal"
)

func main() {
fmt.Printf("Dummy code! Version: %s\n", internal.Version)
}

0 comments on commit 9a5a4c1

Please sign in to comment.