Skip to content

Commit

Permalink
Added release workflow and application versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Pfohl committed Jun 29, 2023
1 parent de86489 commit 8dc3232
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 4 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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
34 changes: 31 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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()
}

Expand All @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions components/configuration/cli_version.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 8dc3232

Please sign in to comment.