-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d243416
commit 8ea516b
Showing
7 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# builder image | ||
FROM golang:alpine as builder | ||
ARG TARGETPLATFORM | ||
COPY . /src | ||
WORKDIR /src | ||
RUN apk add --no-cache make bash git | ||
RUN make app-build PLATFORMS=$TARGETPLATFORM | ||
|
||
# running image | ||
FROM alpine | ||
WORKDIR /home | ||
COPY --from=builder /src/bin/goobar-* /bin/goobar | ||
|
||
# executable | ||
ENTRYPOINT [ "/bin/goobar" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/bin/bash | ||
|
||
SHELL = /bin/bash | ||
PLATFORMS ?= linux/amd64 darwin/amd64 windows/amd64 | ||
IMAGE_PREFIX ?= igolaizola | ||
REPO_NAME ?= goobar | ||
COMMIT_SHORT ?= $(shell git rev-parse --verify --short HEAD) | ||
VERSION ?= $(COMMIT_SHORT) | ||
VERSION_NOPREFIX ?= $(shell echo $(VERSION) | sed -e 's/^[[v]]*//') | ||
|
||
# Build the binaries for the current platform | ||
.PHONY: build | ||
build: | ||
os=$$(go env GOOS); \ | ||
arch=$$(go env GOARCH); \ | ||
PLATFORMS="$$os/$$arch" make app-build | ||
|
||
# Build the binaries | ||
# Example: PLATFORMS=linux/amd64 make app-build | ||
.PHONY: app-build | ||
app-build: | ||
@for platform in $(PLATFORMS) ; do \ | ||
os=$$(echo $$platform | cut -f1 -d/); \ | ||
arch=$$(echo $$platform | cut -f2 -d/); \ | ||
arm=$$(echo $$platform | cut -f3 -d/); \ | ||
arm=$${arm#v}; \ | ||
ext=""; \ | ||
if [ "$$os" == "windows" ]; then \ | ||
ext=".exe"; \ | ||
fi; \ | ||
file=./bin/$(REPO_NAME)-$(VERSION_NOPREFIX)-$$(echo $$platform | tr / -)$$ext; \ | ||
GOOS=$$os GOARCH=$$arch GOARM=$$arm CGO_ENABLED=0 \ | ||
go build \ | ||
-a -x -tags netgo,timetzdata -installsuffix cgo -installsuffix netgo \ | ||
-ldflags " \ | ||
-X main.Version=$(VERSION_NOPREFIX) \ | ||
-X main.GitRev=$(COMMIT_SHORT) \ | ||
" \ | ||
-o $$file \ | ||
./cmd/$(REPO_NAME); \ | ||
if [ $$? -ne 0 ]; then \ | ||
exit 1; \ | ||
fi; \ | ||
chmod +x $$file; \ | ||
done | ||
|
||
# Build the docker image | ||
# Example: PLATFORMS=linux/amd64 make docker-build | ||
.PHONY: docker-build | ||
docker-build: | ||
@platforms=($(PLATFORMS)); \ | ||
platform=$${platforms[0]}; \ | ||
if [[ $${#platforms[@]} -ne 1 ]]; then \ | ||
echo "Multi-arch build not supported"; \ | ||
exit 1; \ | ||
fi; \ | ||
docker build --platform $$platform -t $(IMAGE_PREFIX)/$(REPO_NAME):$(VERSION) .; \ | ||
if [ $$? -ne 0 ]; then \ | ||
exit 1; \ | ||
fi | ||
|
||
# Build the docker images using buildx | ||
# Example: PLATFORMS="linux/amd64 darwin/amd64 windows/amd64" make docker-buildx | ||
.PHONY: docker-buildx | ||
docker-buildx: | ||
@platforms=($(PLATFORMS)); \ | ||
platform=$$(IFS=, ; echo "$${platforms[*]}"); \ | ||
docker buildx build --platform $$platform -t $(IMAGE_PREFIX)/$(REPO_NAME):$(VERSION) . | ||
|
||
# Clean binaries | ||
.PHONY: clean | ||
clean: | ||
rm -rf bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"log" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/igolaizola/goobar" | ||
"github.com/peterbourgon/ff/v3" | ||
"github.com/peterbourgon/ff/v3/ffcli" | ||
) | ||
|
||
func main() { | ||
// Create signal based context | ||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) | ||
defer cancel() | ||
|
||
// Launch command | ||
cmd := newCommand() | ||
if err := cmd.ParseAndRun(ctx, os.Args[1:]); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func newCommand() *ffcli.Command { | ||
fs := flag.NewFlagSet("goobar", flag.ExitOnError) | ||
|
||
return &ffcli.Command{ | ||
ShortUsage: "goobar [flags] <subcommand>", | ||
FlagSet: fs, | ||
Exec: func(context.Context, []string) error { | ||
return flag.ErrHelp | ||
}, | ||
Subcommands: []*ffcli.Command{ | ||
newServeCommand(), | ||
newRunCommand(), | ||
}, | ||
} | ||
} | ||
|
||
func newServeCommand() *ffcli.Command { | ||
fs := flag.NewFlagSet("serve", flag.ExitOnError) | ||
_ = fs.String("config", "", "config file (optional)") | ||
|
||
port := fs.Int("port", 0, "port number") | ||
|
||
return &ffcli.Command{ | ||
Name: "serve", | ||
ShortUsage: "goobar serve [flags] <key> <value data...>", | ||
Options: []ff.Option{ | ||
ff.WithConfigFileFlag("config"), | ||
ff.WithConfigFileParser(ff.PlainParser), | ||
ff.WithEnvVarPrefix("GOOBAR"), | ||
}, | ||
ShortHelp: "run goobar server", | ||
FlagSet: fs, | ||
Exec: func(ctx context.Context, args []string) error { | ||
if *port == 0 { | ||
return errors.New("missing port") | ||
} | ||
return goobar.Serve(ctx, *port) | ||
}, | ||
} | ||
} | ||
|
||
func newRunCommand() *ffcli.Command { | ||
fs := flag.NewFlagSet("run", flag.ExitOnError) | ||
_ = fs.String("config", "", "config file (optional)") | ||
|
||
return &ffcli.Command{ | ||
Name: "run", | ||
ShortUsage: "goobar serve [flags] <key> <value data...>", | ||
Options: []ff.Option{ | ||
ff.WithConfigFileFlag("config"), | ||
ff.WithConfigFileParser(ff.PlainParser), | ||
ff.WithEnvVarPrefix("GOOBAR"), | ||
}, | ||
ShortHelp: "run goobar action", | ||
FlagSet: fs, | ||
Exec: func(ctx context.Context, args []string) error { | ||
return goobar.Run(ctx) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/igolaizola/goobar | ||
|
||
go 1.19 | ||
|
||
require github.com/peterbourgon/ff/v3 v3.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/peterbourgon/ff/v3 v3.3.0 h1:PaKe7GW8orVFh8Unb5jNHS+JZBwWUMa2se0HM6/BI24= | ||
github.com/peterbourgon/ff/v3 v3.3.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package goobar | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
) | ||
|
||
// Server serves the goobar server. | ||
func Serve(ctx context.Context, port int) error { | ||
log.Printf("server listening on port %d\n", port) | ||
<-ctx.Done() | ||
return nil | ||
} | ||
|
||
// Run runs the goobar process. | ||
func Run(ctx context.Context) error { | ||
log.Println("running") | ||
defer log.Println("finished") | ||
select { | ||
case <-ctx.Done(): | ||
case <-time.After(5 * time.Second): | ||
} | ||
return nil | ||
} |