feat: move cmd to own dir
This commit is contained in:
parent
0fe5c8ce03
commit
a26d337ff9
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
cache.json
|
cache.json
|
||||||
|
dist/*
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
project_name: "glucose_exporter"
|
project_name: "glucose_exporter"
|
||||||
builds:
|
builds:
|
||||||
- env:
|
- main: ./cmd/glucose_exporter/
|
||||||
|
env:
|
||||||
- CGO_ENABLED=0
|
- CGO_ENABLED=0
|
||||||
goos:
|
goos:
|
||||||
- linux
|
- linux
|
||||||
@ -10,9 +11,9 @@ builds:
|
|||||||
- "-s"
|
- "-s"
|
||||||
- "-w"
|
- "-w"
|
||||||
- "-extldflags '-static'"
|
- "-extldflags '-static'"
|
||||||
- "-X git.wobcom.de/smartmetering/kerouac/cmd.version={{.Version}}"
|
- "-X main.version={{.Version}}"
|
||||||
- "-X git.wobcom.de/smartmetering/kerouac/cmd.commit={{.ShortCommit}}"
|
- "-X main.commit={{.ShortCommit}}"
|
||||||
- "-X git.wobcom.de/smartmetering/kerouac/cmd.date={{.Date}}"
|
- "-X main.date={{.Date}}"
|
||||||
checksum:
|
checksum:
|
||||||
name_template: "checksums.txt"
|
name_template: "checksums.txt"
|
||||||
snapshot:
|
snapshot:
|
||||||
|
15
Earthfile
15
Earthfile
@ -1,5 +1,5 @@
|
|||||||
VERSION 0.7
|
VERSION 0.7
|
||||||
ARG --global GO_VERSION=1.22.0
|
ARG --global GO_VERSION=1.22.1
|
||||||
ARG --global GOLANGCILINT_VERSION=v1.54.2
|
ARG --global GOLANGCILINT_VERSION=v1.54.2
|
||||||
ARG --global GORELEASER_VERSION=v1.24.0
|
ARG --global GORELEASER_VERSION=v1.24.0
|
||||||
FROM golang:$GO_VERSION-alpine3.18
|
FROM golang:$GO_VERSION-alpine3.18
|
||||||
@ -10,12 +10,14 @@ SAVE_CODE:
|
|||||||
SAVE ARTIFACT go.mod AS LOCAL go.mod
|
SAVE ARTIFACT go.mod AS LOCAL go.mod
|
||||||
SAVE ARTIFACT go.sum AS LOCAL go.sum
|
SAVE ARTIFACT go.sum AS LOCAL go.sum
|
||||||
SAVE ARTIFACT api AS LOCAL api
|
SAVE ARTIFACT api AS LOCAL api
|
||||||
|
SAVE ARTIFACT cmd AS LOCAL cmd
|
||||||
SAVE ARTIFACT httpslog AS LOCAL httpslog
|
SAVE ARTIFACT httpslog AS LOCAL httpslog
|
||||||
SAVE ARTIFACT internal AS LOCAL internal
|
SAVE ARTIFACT internal AS LOCAL internal
|
||||||
|
|
||||||
code:
|
code:
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
COPY --dir api ./
|
COPY --dir api ./
|
||||||
|
COPY --dir cmd ./
|
||||||
COPY --dir httpslog ./
|
COPY --dir httpslog ./
|
||||||
COPY --dir internal ./
|
COPY --dir internal ./
|
||||||
COPY --dir vendor ./
|
COPY --dir vendor ./
|
||||||
@ -71,8 +73,17 @@ goreleaser:
|
|||||||
RUN goreleaser $CMD
|
RUN goreleaser $CMD
|
||||||
SAVE ARTIFACT dist dist
|
SAVE ARTIFACT dist dist
|
||||||
|
|
||||||
|
ssl-certs:
|
||||||
|
RUN set -ex \
|
||||||
|
&& apk add --no-cache ca-certificates
|
||||||
|
SAVE ARTIFACT /etc/ssl/certs/ca-certificates.crt ca-certificates.crt
|
||||||
|
|
||||||
docker:
|
docker:
|
||||||
FROM scratch
|
FROM scratch
|
||||||
ARG DOCKER_TAG
|
ARG DOCKER_TAG
|
||||||
COPY +goreleaser/dist/glucose_exporter_linux_amd64_v1/glucose_exporter /bin/glucose_exporter
|
COPY +ssl-certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||||
|
COPY +goreleaser/dist/glucose_exporter_linux_amd64_v1/glucose_exporter /glucose_exporter
|
||||||
|
VOLUME /var/cache/glucose_exporter
|
||||||
|
ENTRYPOINT ["/glucose_exporter"]
|
||||||
|
CMD ["serve"]
|
||||||
SAVE IMAGE --push ghcr.io/xsteadfastx/glucose_exporter:$DOCKER_TAG
|
SAVE IMAGE --push ghcr.io/xsteadfastx/glucose_exporter:$DOCKER_TAG
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
|
//nolint:forbidigo,gochecknoglobals
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -9,19 +12,58 @@ import (
|
|||||||
|
|
||||||
"github.com/caarlos0/env/v10"
|
"github.com/caarlos0/env/v10"
|
||||||
"github.com/lmittmann/tint"
|
"github.com/lmittmann/tint"
|
||||||
|
|
||||||
"go.xsfx.dev/glucose_exporter/httpslog"
|
"go.xsfx.dev/glucose_exporter/httpslog"
|
||||||
"go.xsfx.dev/glucose_exporter/internal/cache"
|
"go.xsfx.dev/glucose_exporter/internal/cache"
|
||||||
"go.xsfx.dev/glucose_exporter/internal/config"
|
"go.xsfx.dev/glucose_exporter/internal/config"
|
||||||
"go.xsfx.dev/glucose_exporter/internal/metrics"
|
"go.xsfx.dev/glucose_exporter/internal/metrics"
|
||||||
)
|
)
|
||||||
|
|
||||||
const addr = ":2112"
|
var (
|
||||||
|
version = "dev"
|
||||||
|
commit = "none"
|
||||||
|
date = "unknown"
|
||||||
|
)
|
||||||
|
|
||||||
|
const help = "expected 'serve' or 'version' subcommands"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
logOpts := &tint.Options{Level: slog.LevelInfo, TimeFormat: time.Kitchen}
|
logOpts := &tint.Options{Level: slog.LevelInfo, TimeFormat: time.Kitchen}
|
||||||
initLogging(logOpts)
|
initLogging(logOpts)
|
||||||
|
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
slog.Error(help)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch os.Args[1] {
|
||||||
|
case "serve":
|
||||||
|
flags := flag.NewFlagSet("serve", flag.ExitOnError)
|
||||||
|
|
||||||
|
addr := flags.String("addr", ":2112", "address to listen")
|
||||||
|
flags.BoolFunc("help", "print help", func(_ string) error {
|
||||||
|
flags.PrintDefaults()
|
||||||
|
os.Exit(0)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := flags.Parse(os.Args[2:]); err != nil {
|
||||||
|
slog.Error("parsing flags", "err", err)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
serveCmd(*addr)
|
||||||
|
|
||||||
|
case "version":
|
||||||
|
versionCmd()
|
||||||
|
|
||||||
|
default:
|
||||||
|
slog.Error(help)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func serveCmd(addr string) {
|
||||||
if err := env.Parse(&config.Cfg); err != nil {
|
if err := env.Parse(&config.Cfg); err != nil {
|
||||||
slog.Error("parsing env config", "err", err)
|
slog.Error("parsing env config", "err", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -40,9 +82,9 @@ func main() {
|
|||||||
if err := os.WriteFile(cache.FullPath(), []byte("{}"), 0o600); err != nil {
|
if err := os.WriteFile(cache.FullPath(), []byte("{}"), 0o600); err != nil {
|
||||||
cacheFileLogger.Error("init cache file", "err", err)
|
cacheFileLogger.Error("init cache file", "err", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else {
|
|
||||||
cacheFileLogger.Debug("init cache file")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cacheFileLogger.Debug("init cache file")
|
||||||
} else {
|
} else {
|
||||||
cacheFileLogger.Error("getting cache file stat", "err", err)
|
cacheFileLogger.Error("getting cache file stat", "err", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -54,12 +96,23 @@ func main() {
|
|||||||
|
|
||||||
slog.Info("listening", "addr", addr)
|
slog.Info("listening", "addr", addr)
|
||||||
|
|
||||||
if err := http.ListenAndServe(addr, httpslog.Handler()(mux)); err != nil {
|
server := &http.Server{
|
||||||
|
Addr: addr,
|
||||||
|
ReadTimeout: time.Second,
|
||||||
|
ReadHeaderTimeout: time.Second,
|
||||||
|
Handler: httpslog.Handler()(mux),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := server.ListenAndServe(); err != nil {
|
||||||
slog.Error("listen and serve", "err", err)
|
slog.Error("listen and serve", "err", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func versionCmd() {
|
||||||
|
fmt.Printf("glucose_exporter %s, commit %s, %s\n", version, commit, date)
|
||||||
|
}
|
||||||
|
|
||||||
func initLogging(opts *tint.Options) {
|
func initLogging(opts *tint.Options) {
|
||||||
slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, opts)))
|
slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, opts)))
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user