diff --git a/.cmdx.yaml b/.cmdx.yaml index 864d04b7..bc5ab81b 100644 --- a/.cmdx.yaml +++ b/.cmdx.yaml @@ -40,7 +40,9 @@ tasks: short: r description: release the new version usage: release the new version - script: "bash scripts/release.sh {{.version}}" + script: | + git tag "{{.version}}" + git push origin "{{.version}}" args: - name: version required: true diff --git a/cmd/tfmigrator/main.go b/cmd/tfmigrator/main.go index fb7494bd..74a20d38 100644 --- a/cmd/tfmigrator/main.go +++ b/cmd/tfmigrator/main.go @@ -9,6 +9,12 @@ import ( "github.com/tfmigrator/cli/pkg/cli" ) +var ( + version = "" + commit = "" //nolint:gochecknoglobals + date = "" //nolint:gochecknoglobals +) + func main() { if err := core(); err != nil { logrus.Fatal(err) @@ -20,6 +26,11 @@ func core() error { Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr, + LDFlags: &cli.LDFlags{ + Version: version, + Commit: commit, + Date: date, + }, } ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) defer stop() diff --git a/pkg/cli/runner.go b/pkg/cli/runner.go index b4286bb5..4206958c 100644 --- a/pkg/cli/runner.go +++ b/pkg/cli/runner.go @@ -4,21 +4,31 @@ import ( "context" "io" - "github.com/tfmigrator/cli/pkg/constant" "github.com/urfave/cli/v2" ) +type LDFlags struct { + Version string + Commit string + Date string +} + +func (flags *LDFlags) AppVersion() string { + return flags.Version + " (" + flags.Commit + ")" +} + type Runner struct { - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + Stdin io.Reader + Stdout io.Writer + Stderr io.Writer + LDFlags *LDFlags } func (runner *Runner) Run(ctx context.Context, args ...string) error { app := cli.App{ Name: "tfmigrator", Usage: "Migrate Terraform Configuration and State. https://github.com/tfmigrator/cli", - Version: constant.Version, + Version: runner.LDFlags.AppVersion(), Commands: []*cli.Command{ { Name: "run", diff --git a/pkg/constant/version.go b/pkg/constant/version.go deleted file mode 100644 index 20229650..00000000 --- a/pkg/constant/version.go +++ /dev/null @@ -1,7 +0,0 @@ -package constant - -// Don't edit this file. -// This file is generated by the release command. - -// Version is the tfmigrator's version. -const Version = "0.2.0" diff --git a/scripts/release.sh b/scripts/release.sh deleted file mode 100644 index 55085ea1..00000000 --- a/scripts/release.sh +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env bash -# Usage -# bash scripts/release.sh v0.3.2 - -set -eu -set -o pipefail - -REMOTE=https://github.com/tfmigrator/cli - -ee() { - echo "+ $*" - eval "$@" -} - -BRANCH="$(git branch | grep "^\* " | sed -e "s/^\* \(.*\)/\1/")" -if [ "$BRANCH" != "main" ]; then - read -r -p "The current branch isn't main but $BRANCH. Are you ok? (y/n)" YN - if [ "${YN}" != "y" ]; then - echo "cancel to release" - exit 0 - fi -fi - -TAG="$1" -echo "TAG: $TAG" -VERSION="${TAG#v}" - -if [ "$TAG" = "$VERSION" ]; then - echo "the tag must start with 'v'" >&2 - exit 1 -fi - -cd "$(dirname "$0")/.." - -VERSION_FILE=pkg/constant/version.go -echo "create $VERSION_FILE" -cat << EOS > "$VERSION_FILE" -package constant - -// Don't edit this file. -// This file is generated by the release command. - -// Version is the tfmigrator's version. -const Version = "$VERSION" -EOS - -ee git add "$VERSION_FILE" -echo "+ git commit -m \"build: update version to $TAG\"" -git commit -m "build: update version to $TAG" -ee git tag "$TAG" -ee git push "$REMOTE" "$BRANCH" -ee git push "$REMOTE" "$TAG"