diff --git a/Earthfile b/Earthfile index 77b27a8..696f424 100644 --- a/Earthfile +++ b/Earthfile @@ -14,8 +14,11 @@ version: FROM alpine RUN apk add git COPY . ./ - RUN --no-cache echo $(git describe --always --tags --dirty) > VERSION + RUN --no-cache echo $(git describe --tags | sed 's/\(.*\)-.*/\1/') > VERSION + RUN --no-cache echo $(git describe --always --dirty) > COMMIT ARG VERSION=$(cat VERSION) + ARG COMMIT=$(cat COMMMIT) + SAVE ARTIFACT COMMIT COMMIT SAVE ARTIFACT VERSION VERSION golang-image: @@ -58,8 +61,10 @@ build-immucore: COPY --dir internal /work COPY --dir pkg /work COPY +version/VERSION ./ + COPY +version/COMMIT ./ ARG VERSION=$(cat VERSION) - ARG LDFLAGS="-s -w -X github.com/kairos-io/immucore/internal/version.version=$VERSION" + ARG COMMIT=$(cat COMMIT) + ARG LDFLAGS="-s -w -X github.com/kairos-io/immucore/internal/version.version=$VERSION -X github.com/kairos-io/immucore/internal/version.gitCommit=$COMMIT" RUN echo ${LDFLAGS} RUN CGO_ENABLED=0 go build -o immucore -ldflags "${LDFLAGS}" SAVE ARTIFACT /work/immucore immucore AS LOCAL build/immucore-$VERSION diff --git a/internal/cmd/commands.go b/internal/cmd/commands.go index b7a41ae..0f7c66b 100644 --- a/internal/cmd/commands.go +++ b/internal/cmd/commands.go @@ -5,6 +5,7 @@ import ( "os" "github.com/kairos-io/immucore/internal/utils" + "github.com/kairos-io/immucore/internal/version" "github.com/kairos-io/immucore/pkg/mount" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -39,6 +40,9 @@ Sends a generic event payload with the configuration found in the scanned direct zerolog.SetGlobalLevel(zerolog.DebugLevel) } + v := version.Get() + log.Logger.Info().Str("commit", v.GitCommit).Str("compiled with", v.GoVersion).Str("version", v.Version).Msg("Immucore") + g := herd.DAG(herd.EnableInit) // You can pass rd.cos.disable in the cmdline to disable the whole immutable stuff @@ -86,4 +90,15 @@ Sends a generic event payload with the configuration found in the scanned direct return err }, }, + { + Name: "version", + Usage: "version", + Action: func(c *cli.Context) error { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() + zerolog.SetGlobalLevel(zerolog.InfoLevel) + v := version.Get() + log.Logger.Info().Str("commit", v.GitCommit).Str("compiled with", v.GoVersion).Str("version", v.Version).Msg("Immucore") + return nil + }, + }, } diff --git a/internal/version/version.go b/internal/version/version.go index 7c64f3a..83f5b25 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -4,7 +4,7 @@ import "runtime" var ( version = "v0.0.1" - // gitCommit is the git sha1 + // gitCommit is the git sha1 + dirty if build from a dirty git gitCommit = "" ) diff --git a/main.go b/main.go index 3ca0d47..f648f3d 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,9 @@ package main import ( "fmt" - "github.com/kairos-io/immucore/internal/version" "os" "github.com/kairos-io/immucore/internal/cmd" - "github.com/urfave/cli/v2" ) @@ -24,8 +22,6 @@ func main() { Commands: cmd.Commands, } - - fmt.Println(version.Get()) err := app.Run(os.Args) if err != nil {