From 8dc32326713e8d53f30148e1cdcface9930da1b8 Mon Sep 17 00:00:00 2001 From: Andreas Pfohl Date: Thu, 29 Jun 2023 22:39:45 +0200 Subject: [PATCH] Added release workflow and application versioning --- .github/workflows/release.yml | 57 +++++++++++++++++++++++++ cmd/root.go | 34 +++++++++++++-- components/configuration/cli_version.go | 32 ++++++++++++++ main.go | 11 ++++- 4 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/release.yml create mode 100644 components/configuration/cli_version.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..3f8c01e --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,57 @@ +name: Release + +on: + push: + tags: + - '*' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '1.20.5' + + - name: Set version + run: echo "version=${GITHUB_REF#refs/*/}" >> "$GITHUB_ENV" + + - name: Build + run: go build -ldflags="-X main.version=${{ env.version }} -X main.commit=$GITHUB_SHA -X main.date=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" -o op + + - name: Upload build artifact + uses: actions/upload-artifact@v2 + with: + name: openproject-cli + path: op + + release: + needs: [ build ] + runs-on: ubuntu-latest + permissions: + contents: write + + steps: + - uses: actions/checkout@v2 + + - name: Download artifact + uses: actions/download-artifact@v2 + with: + name: openproject-cli + + - name: Set version + run: echo "version=${GITHUB_REF#refs/*/}" >> "$GITHUB_ENV" + + - name: Set executable flag + run: chmod +x op + + - name: Zip executable + run: zip -r openproject-cli_linux_x64_${{ env.version }}.zip op + + - name: Create release + uses: softprops/action-gh-release@v1 + with: + files: openproject-cli_linux_x64_${{ env.version }}.zip diff --git a/cmd/root.go b/cmd/root.go index ffa3dfc..f70eaa6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,13 +1,15 @@ package cmd import ( + "fmt" "net/url" "os" - - "github.com/opf/openproject-cli/cmd/create" + "runtime" + "time" "github.com/spf13/cobra" + "github.com/opf/openproject-cli/cmd/create" "github.com/opf/openproject-cli/cmd/inspect" "github.com/opf/openproject-cli/cmd/list" "github.com/opf/openproject-cli/cmd/update" @@ -17,15 +19,33 @@ import ( "github.com/opf/openproject-cli/components/routes" ) +var showVersionFlag bool + var rootCmd = &cobra.Command{ Use: os.Args[0], Short: "An easy-to-use CLI for the OpenProject APIv3", Long: `OpenProject CLI is a fast, reliable and easy-to-use tool to manage your work packages, notifications and projects of your OpenProject instance.`, + Run: func(cmd *cobra.Command, args []string) { + if showVersionFlag { + versionText := fmt.Sprintf( + "%s: %s\n\tcommit: %s\n\tbuilt: %s\n\tbuilt with: %s", + "OpenProject CLI", + configuration.CliVersion.Version, + configuration.CliVersion.Commit, + configuration.CliVersion.Date.Format(time.UnixDate), + runtime.Version(), + ) + + fmt.Println(printer.Yellow(versionText)) + } + }, } -func Execute() error { +func Execute(version *configuration.Version) error { + configuration.Init(version) + return rootCmd.Execute() } @@ -46,6 +66,14 @@ func init() { requests.Init(parse, token) routes.Init(parse) + rootCmd.Flags().BoolVarP( + &showVersionFlag, + "version", + "", + false, + "Show version information of the OpenProject CLI", + ) + rootCmd.AddCommand( loginCmd, list.RootCmd, diff --git a/components/configuration/cli_version.go b/components/configuration/cli_version.go new file mode 100644 index 0000000..af1cbc6 --- /dev/null +++ b/components/configuration/cli_version.go @@ -0,0 +1,32 @@ +package configuration + +import "time" + +type Version struct { + Version string + Commit string + Date time.Time +} + +var CliVersion *Version + +func Init(version *Version) { + CliVersion = version +} + +func BuildCliVersion(version string, commit string, date string) *Version { + buildCommit := string([]rune(commit)[:7]) + + var buildDate time.Time + if date == "unknown" { + buildDate = time.Now() + } else { + buildDate, _ = time.Parse(time.RFC3339, date) + } + + return &Version{ + Version: version, + Commit: buildCommit, + Date: buildDate, + } +} diff --git a/main.go b/main.go index cb970c4..72659d0 100644 --- a/main.go +++ b/main.go @@ -5,10 +5,19 @@ import ( "os" "github.com/opf/openproject-cli/cmd" + "github.com/opf/openproject-cli/components/configuration" +) + +var ( + version = "current" + commit = "none" + date = "unknown" ) func main() { - if err := cmd.Execute(); err != nil { + cliVersion := configuration.BuildCliVersion(version, commit, date) + + if err := cmd.Execute(cliVersion); err != nil { _, _ = fmt.Fprintln(os.Stderr, err) os.Exit(1) }