This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 396
/
version.go
83 lines (69 loc) · 1.91 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"errors"
"fmt"
"io"
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/Azure/draft/pkg/draft"
"github.com/Azure/draft/pkg/version"
)
const versionDesc = `
Show the version for draft.
This prints the client and server versions of draft. The output will look something like
this:
Client: &version.Version{SemVer:"v0.1.0", GitCommit:"4f97233d2cc2c7017b07f94211e55bb2670f990d", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v0.1.0", GitCommit:"4f97233d2cc2c7017b07f94211e55bb2670f990d", GitTreeState:"clean"}
`
type versionCmd struct {
out io.Writer
client *draft.Client
short bool
clientOnly bool
serverOnly bool
}
func newVersionCmd(out io.Writer) *cobra.Command {
version := &versionCmd{
out: out,
}
cmd := &cobra.Command{
Use: "version",
Short: "print the client version information",
Long: versionDesc,
RunE: func(cmd *cobra.Command, args []string) error {
if !version.clientOnly {
// We do this manually instead of in PreRun because we only
// need a tunnel if server version is requested.
setupConnection(cmd, args)
}
version.client = ensureDraftClient(version.client)
return version.run()
},
}
f := cmd.Flags()
f.BoolVarP(&version.clientOnly, "client", "c", false, "client version only")
f.BoolVarP(&version.serverOnly, "server", "s", false, "server version only")
return cmd
}
func (v *versionCmd) run() error {
if !v.serverOnly {
cv := version.New()
fmt.Fprintf(v.out, "Client: %s\n", formatVersion(cv, v.short))
}
if v.clientOnly {
return nil
}
sv, err := v.client.Version()
if err != nil {
log.Debug(err)
return errors.New("cannot connect to draftd")
}
fmt.Fprintf(v.out, "Server: %s\n", formatVersion(sv, v.short))
return nil
}
func formatVersion(v *version.Version, short bool) string {
if short {
return fmt.Sprintf("%s+g%s", v.SemVer, v.GitCommit[:7])
}
return fmt.Sprintf("%#v", v)
}