Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version info #78

Merged
merged 4 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ builds:
- id: importer
binary: importer
main: ./cmd/importer/main.go
hooks:
pre: ./tools/prep-release.sh
goos:
- darwin
- linux
# - windows # Taking out Windows for now
- windows
goarch:
- amd64

Expand Down
1 change: 1 addition & 0 deletions VERSION.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.1
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
module github.com/upsidr/importer

go 1.16
go 1.17

require (
github.com/google/go-cmp v0.5.5
github.com/spf13/cobra v1.2.1
)

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
1 change: 1 addition & 0 deletions internal/cli/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func Run(args []string) error {
updateCmd,
generateCliCmd,
purgeCliCmd,
versionCmd,
)
return cmd.Execute()
}
26 changes: 26 additions & 0 deletions internal/cli/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/upsidr/importer/internal/version"
)

var (
versionCmd = &cobra.Command{
Aliases: []string{"v"},
Use: "version",
Short: "Print version information",
Run: executeVersion,
}
)

func executeVersion(cmd *cobra.Command, args []string) {
// Suppress usage message after this point
cmd.SilenceUsage = true

v := version.GetVersion()
fmt.Println(v.VersionInfo())
}
Empty file added internal/version/RC.txt
Empty file.
Empty file added internal/version/REVISION.txt
Empty file.
1 change: 1 addition & 0 deletions internal/version/VERSION.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev
54 changes: 54 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package version

import (
"embed"
"fmt"
"strings"
)

var (
//go:embed *.txt
versionData embed.FS

unknownVersion = &Version{
Version: "(version unknown)",
}
)

type Version struct {
Version string
Revision string
ReleaseCandidate string
}

func GetVersion() *Version {
base, err := versionData.ReadFile("VERSION.txt")
if err != nil {
return unknownVersion
}
v := &Version{
Version: string(base),
}

if rev, err := versionData.ReadFile("REVISION.txt"); err == nil {
v.Revision = strings.TrimSpace(string(rev))
}
if rc, err := versionData.ReadFile("RC.txt"); err == nil {
v.ReleaseCandidate = strings.TrimSpace(string(rc))
}

return v
}

func (v *Version) VersionInfo() string {
result := v.Version
if v.ReleaseCandidate != "" {
result = fmt.Sprintf("%s-%s", v.Version, v.ReleaseCandidate)
}

if v.Revision != "" {
result = result + " ( " + v.Revision + " )"
}

return result
}
9 changes: 9 additions & 0 deletions tools/prep-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

__root_dir=$(dirname "$0")/..

# Fill in revision information
git rev-parse --short HEAD >"$__root_dir"/internal/version/REVISION.txt

# Copy the version info in the root dir
cp "$__root_dir"/VERSION.txt "$__root_dir"/internal/version/VERSION.txt