-
Notifications
You must be signed in to change notification settings - Fork 451
/
version.go
108 lines (85 loc) · 2.62 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package talos
import (
"context"
"fmt"
"strings"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
"google.golang.org/protobuf/encoding/protojson"
"github.com/talos-systems/talos/pkg/cli"
"github.com/talos-systems/talos/pkg/machinery/client"
"github.com/talos-systems/talos/pkg/version"
)
// versionCmdFlags represents the `talosctl version` command's flags.
var versionCmdFlags struct {
clientOnly bool
shortVersion bool
json bool
}
// versionCmd represents the `talosctl version` command.
var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version",
Long: ``,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if !versionCmdFlags.json {
fmt.Println("Client:")
if versionCmdFlags.shortVersion {
version.PrintShortVersion()
} else {
version.PrintLongVersion()
}
// Exit early if we're only looking for client version
if versionCmdFlags.clientOnly {
return nil
}
fmt.Println("Server:")
}
return WithClient(func(ctx context.Context, c *client.Client) error {
var remotePeer peer.Peer
resp, err := c.Version(ctx, grpc.Peer(&remotePeer))
if err != nil {
if resp == nil {
return fmt.Errorf("error getting version: %s", err)
}
cli.Warning("%s", err)
}
defaultNode := client.AddrFromPeer(&remotePeer)
for _, msg := range resp.Messages {
node := defaultNode
if msg.Metadata != nil {
node = msg.Metadata.Hostname
}
if !versionCmdFlags.json {
fmt.Printf("\t%s: %s\n", "NODE", node)
version.PrintLongVersionFromExisting(msg.Version)
var enabledFeatures []string
if msg.Features.GetRbac() {
enabledFeatures = append(enabledFeatures, "RBAC")
}
fmt.Printf("\tEnabled: %s\n", strings.Join(enabledFeatures, ", "))
continue
}
b, err := protojson.Marshal(msg)
if err != nil {
return err
}
fmt.Printf("%s\n", b)
}
return nil
})
},
}
func init() {
versionCmd.Flags().BoolVar(&versionCmdFlags.shortVersion, "short", false, "Print the short version")
versionCmd.Flags().BoolVar(&versionCmdFlags.clientOnly, "client", false, "Print client version only")
// TODO remove when https://github.com/talos-systems/talos/issues/907 is implemented
versionCmd.Flags().BoolVar(&versionCmdFlags.json, "json", false, "")
cli.Should(versionCmd.Flags().MarkHidden("json"))
addCommand(versionCmd)
}