Skip to content

Commit

Permalink
feat: print version
Browse files Browse the repository at this point in the history
  • Loading branch information
alovn committed May 25, 2022
1 parent 76c09fe commit f44671a
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release

on:
push:
tags:
- 'v*'

jobs:
build:
runs-on: ubuntu-latest
env:
GO111MODULE: on
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
steps:
- name: Checkout Source
uses: actions/checkout@v3
- name: Unshallow
run: git fetch --prune --unshallow
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
- name: Release Binaries
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@

# Dependency directories (remove the comment below to include it)
# vendor/
.DS_Store
.coverage
vendor/
.idea
bin/
dist/
26 changes: 26 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
project_name: apidocgen

release:
github:
owner: alovn
name: apidocgen

builds:
- main: ./main.go
binary: apidocgen
goos:
- darwin
- linux
- windows
goarch:
- amd64
- arm64
ignore:
- goos: windows
goarch: arm64
- goos: linux
goarch: arm64
ldflags: -X {{.ModulePath}}/cmd.Version={{.Version}} -X {{.ModulePath}}/cmd.GitCommit={{.Commit}} -X {{.ModulePath}}/cmd.BuildDate={{.Date}}
env:
- CGO_ENABLED=0
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Usage:
Available Commands:
help Help about any command
mock
version print version

Flags:
--dir string Search apis dir, comma separated (default ".")
Expand Down
45 changes: 45 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var (
// Version version
Version string = "0.1.0"
// BuildDate build date
BuildDate string
// GitCommit
GitCommit string
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "print version",
Long: `print version`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("version: %s\n", getVersion())
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}

func getVersion() string {
ret := Version
if GitCommit != "" && len(GitCommit) >= 7 {
ret += fmt.Sprintf("\ngit-commit: %s", GitCommit[0:7])
}
if BuildDate != "" {
ret += fmt.Sprintf("\nbuild-at: %s", BuildDate)
}
return ret
}

0 comments on commit f44671a

Please sign in to comment.