/
version.go
48 lines (38 loc) · 1.03 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"io"
"github.com/spf13/cobra"
"github.com/zanetworker/opentaas/pkg/log"
"github.com/zanetworker/opentaas/pkg/version"
)
const versionDesc = `This command prints out the version of TaaS.`
type versionCmdOpts struct {
out io.Writer
short bool
}
func newVersionCmd(out io.Writer) *cobra.Command {
versionCmdOpts := &versionCmdOpts{out: out}
versionCmd := &cobra.Command{
Use: "version",
Short: "get version",
Long: versionDesc,
RunE: func(cmd *cobra.Command, args []string) error {
return versionCmdOpts.run(cmd)
},
}
f := versionCmd.Flags()
f.BoolVarP(&versionCmdOpts.short, "short", "s", false, "print only the version number and prefix of latest commit")
return versionCmd
}
func (v *versionCmdOpts) run(cmd *cobra.Command) error {
vOpts := version.Options{}
version, err := vOpts.BuildVersion(v.short)
if err != nil {
return err
}
if _, err := fmt.Fprintf(v.out, "TaaS Version: %s", version); err != nil {
log.Error("failed to create version string", err)
}
return nil
}