Skip to content

Commit

Permalink
feat: add new config options
Browse files Browse the repository at this point in the history
  • Loading branch information
kperreau committed Apr 18, 2024
1 parent 49ca4bb commit 87f6f35
Show file tree
Hide file tree
Showing 24 changed files with 256 additions and 95 deletions.
17 changes: 17 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.gitignore
.dockerignore
Dockerfile
.github/
.git/
*.log
.goac/
.goacproject.yaml
.semver.yaml
coverage.out
goac
*_test.go
README.md
LICENSE
Makefile
.DS_Store
.idea
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ go.work

.goac
goac
.idea
.idea
.DS_Store
11 changes: 6 additions & 5 deletions .goacproject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ target:
- build
- -ldflags=-s -w
- -o
- "{{project-path}}/{{project-name}}"
- "{{project-path}}"
build-image:
envs:
- key: PROJECT_PATH
value: "{{project-path}}"
exec:
cmd: go
params:
- build
- -ldflags=-s -w
- -o
cmd: ./_scripts/build-image.sh
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM golang:1.22-bookworm AS builder

# Set Go env
ENV GOOS=linux CGO_ENABLED=0

WORKDIR /workspace

# Build Go binary
COPY . .
RUN --mount=type=cache,mode=0755,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go mod download
RUN --mount=type=cache,mode=0755,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 go build -ldflags="-s -w" -o /workspace/goac

# Deployment container
FROM gcr.io/distroless/static-debian12

COPY --from=builder /workspace/goac /goac
ENTRYPOINT ["/goac"]
33 changes: 33 additions & 0 deletions _scripts/build-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

set -e

: "${PUSH_IMAGE="false"}"

: "${REPOSITORY="kperreau/goac"}"

: "${PROJECT_PATH="."}" # project path (must be where the Dockerfile is)

DOCKERFILE="${PROJECT_PATH}/Dockerfile"

GIT_VERSION=$(git rev-parse --short=7 HEAD)

dockerCmd=(docker buildx build --platform="linux/amd64,linux/arm64" --network host)

if [[ "${PUSH_IMAGE}" == "true" ]]; then
dockerCmd+=(--push);
fi

# print cmd
echo "${dockerCmd[@]}" \
-t "${REPOSITORY}:latest" \
-t "${REPOSITORY}:${GIT_VERSION}" \
-f "${DOCKERFILE}" \
.

# run docker build
"${dockerCmd[@]}" \
-t "${REPOSITORY}:latest" \
-t "${REPOSITORY}:${GIT_VERSION}" \
-f "${DOCKERFILE}" \
.
1 change: 0 additions & 1 deletion cmd/affected.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var affectedCmd = &cobra.Command{
t := project.StringToTarget(target)
if project.StringToTarget(target) != project.TargetNone {
projectsList, err := project.NewProjectsList(&project.Options{
Path: ".",
Target: t,
DryRun: dryrun,
MaxConcurrency: concurrency,
Expand Down
1 change: 0 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var listCmd = &cobra.Command{
}

listProject, err := project.NewProjectsList(&project.Options{
Path: ".",
Target: project.TargetNone,
MaxConcurrency: concurrency,
BinaryCheck: binaryCheck,
Expand Down
4 changes: 2 additions & 2 deletions pkg/project/affected.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func processAffected(p *Project, opts *processAffectedOptions) {
isAffected := p.isAffected()

if isAffected && p.CMDOptions.DryRun {
printer.Printf("%s %s %s\n", color.BlueString(p.Name), color.YellowString("=>"), p.Path)
printer.Printf("%s %s %s\n", color.BlueString(p.Name), color.YellowString("=>"), p.CleanPath)
}

if p.CMDOptions.DryRun || !isAffected {
Expand Down Expand Up @@ -91,7 +91,7 @@ func (p *Project) isAffected() bool {
return true
}

if p.CMDOptions.BinaryCheck && !utils.FileExist(path.Join(p.Path, p.Name)) {
if p.CMDOptions.BinaryCheck && !utils.FileExist(path.Join(p.CleanPath, p.Name)) {
return true
}

Expand Down
48 changes: 24 additions & 24 deletions pkg/project/affected_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func TestIsAffected_BinaryCheckFalse_ReturnFalse(t *testing.T) {
DirHash: "hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand Down Expand Up @@ -81,8 +81,8 @@ func TestIsAffected_BinaryCheckTrue_ReturnTrue(t *testing.T) {
DirHash: "hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand Down Expand Up @@ -110,8 +110,8 @@ func TestIsAffected_DiffHash_ReturnTrue(t *testing.T) {
DirHash: "new-hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand Down Expand Up @@ -176,8 +176,8 @@ func TestCountAffected_OneProject(t *testing.T) {
DirHash: "hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand Down Expand Up @@ -213,8 +213,8 @@ func TestCountAffected_OneProjectOfTwo(t *testing.T) {
DirHash: "hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand All @@ -234,8 +234,8 @@ func TestCountAffected_OneProjectOfTwo(t *testing.T) {
DirHash: "hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand Down Expand Up @@ -270,8 +270,8 @@ func TestCountAffected_TwoProjects(t *testing.T) {
DirHash: "new-hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand All @@ -291,8 +291,8 @@ func TestCountAffected_TwoProjects(t *testing.T) {
DirHash: "new-hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand Down Expand Up @@ -346,8 +346,8 @@ func TestAffected_Prints1AffectedProjects(t *testing.T) {
DirHash: "hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand All @@ -369,8 +369,8 @@ func TestAffected_Prints1AffectedProjects(t *testing.T) {
DirHash: "hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand Down Expand Up @@ -413,8 +413,8 @@ func TestAffected_MaxConcurrencyZero(t *testing.T) {
DirHash: "hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand Down Expand Up @@ -450,8 +450,8 @@ func TestProcessAffected_BuildAffectedProject(t *testing.T) {
DirHash: "hash",
Date: "date",
},
Path: ".",
Name: "goac",
CleanPath: ".",
Name: "goac",
Cache: &Cache{
Target: map[Target]*Metadata{
TargetBuild: {
Expand Down
39 changes: 31 additions & 8 deletions pkg/project/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package project
import (
"bytes"
"fmt"
"os"
"os/exec"
"path"

"github.com/fatih/color"
"github.com/kperreau/goac/pkg/printer"
"os"
"os/exec"
"strings"
)

func (p *Project) build() error {
printer.Printf("Building %s...\n", color.HiBlueString(p.Name))
params := append(p.Target[p.CMDOptions.Target].Exec.Params, path.Join(p.GoPath, p.Name), p.GoPath)

// replace variables env and params to proper values
replaceAllVariables(p)

var stderr bytes.Buffer
cmd := exec.Command(p.Target[p.CMDOptions.Target].Exec.CMD, params...)
cmd := exec.Command(p.Target[p.CMDOptions.Target].Exec.CMD, p.Target[p.CMDOptions.Target].Exec.Params...)
setEnv(p, cmd)
cmd.Stderr = &stderr
output, err := cmd.Output()
Expand All @@ -32,6 +33,28 @@ func (p *Project) build() error {
}

func setEnv(p *Project, cmd *exec.Cmd) {
cmd.Env = append(os.Environ(), fmt.Sprintf("BUILD_NAME=%s", p.Name))
cmd.Env = append(cmd.Env, fmt.Sprintf("PROJECT_PATH=%s", p.GoPath))
cmd.Env = os.Environ()
for _, env := range p.Target[p.CMDOptions.Target].Envs {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", env.Key, env.Value))
}
}

func replaceAllVariables(p *Project) {
// init variables
variables := map[string]string{
"{{project-name}}": p.Name,
"{{project-path}}": p.Path,
}

for i := range p.Target[p.CMDOptions.Target].Envs {
for search, replace := range variables {
p.Target[p.CMDOptions.Target].Envs[i].Value = strings.ReplaceAll(p.Target[p.CMDOptions.Target].Envs[i].Value, search, replace)
}
}

for i := range p.Target[p.CMDOptions.Target].Exec.Params {
for search, replace := range variables {
p.Target[p.CMDOptions.Target].Exec.Params[i] = strings.ReplaceAll(p.Target[p.CMDOptions.Target].Exec.Params[i], search, replace)
}
}
}
Loading

0 comments on commit 87f6f35

Please sign in to comment.