mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
Added version command and corresponding implementation
This commit is contained in:
parent
6d61511e36
commit
c8297534cc
2 changed files with 66 additions and 0 deletions
24
cmd/version.go
Normal file
24
cmd/version.go
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
magellan "github.com/OpenCHAMI/magellan/internal"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var versionCmd = &cobra.Command{
|
||||||
|
Use: "version",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if cmd.Flag("rev").Value.String() == "true" {
|
||||||
|
fmt.Println(magellan.VersionCommit())
|
||||||
|
} else {
|
||||||
|
fmt.Println(magellan.VersionTag())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
versionCmd.Flags().Bool("rev", false, "show the version commit")
|
||||||
|
rootCmd.AddCommand(versionCmd)
|
||||||
|
}
|
||||||
42
internal/version.go
Normal file
42
internal/version.go
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
package magellan
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// VersionCommit() returns a string with 'r{commit_count}.{commit_version}' format.
|
||||||
|
func VersionCommit() string {
|
||||||
|
var (
|
||||||
|
version string
|
||||||
|
revlist []byte
|
||||||
|
revparse []byte
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
revlist, err = exec.Command("git", "rev-list", "--count", "HEAD").Output()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
revparse, err = exec.Command("git", "rev-parse", "--short", "HEAD").Output()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
version = fmt.Sprintf("r%s.%s", strings.TrimRight(string(revlist), "\n"), string(revparse))
|
||||||
|
return strings.TrimRight(version, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// VersionTag() returns a string with format '{git_tag}' using the `git describe` command.
|
||||||
|
func VersionTag() string {
|
||||||
|
var (
|
||||||
|
describe []byte
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
describe, err = exec.Command("git", "describe", "--long", "HEAD").Output()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimRight(string(describe), "\n")
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue