-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
132 lines (112 loc) · 3.89 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package version
import (
"fmt"
"os"
"strings"
"github.com/openshift/odo/pkg/occlient"
"github.com/openshift/odo/pkg/odo/genericclioptions"
odoversion "github.com/openshift/odo/pkg/version"
"github.com/golang/glog"
"github.com/openshift/odo/pkg/notify"
"github.com/openshift/odo/pkg/odo/util"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubernetes/pkg/kubectl/util/templates"
)
// RecommendedCommandName is the recommended version command name
const RecommendedCommandName = "version"
// OdoReleasesPage is the GitHub page where we do all our releases
const OdoReleasesPage = "https://github.com/openshift/odo/releases"
var versionLongDesc = ktemplates.LongDesc("Print the client version information")
var versionExample = ktemplates.Examples(`
# Print the client version of odo
%[1]s`,
)
// VersionOptions encapsulates all options for odo version command
type VersionOptions struct {
// clientFlag indicates if the user only wants client information
clientFlag bool
// serverInfo contains the remote server information if the user asked for it, nil otherwise
serverInfo *occlient.ServerInfo
}
// NewVersionOptions creates a new VersionOptions instance
func NewVersionOptions() *VersionOptions {
return &VersionOptions{}
}
// Complete completes VersionOptions after they have been created
func (o *VersionOptions) Complete(name string, cmd *cobra.Command, args []string) error {
if !o.clientFlag {
// Let's fetch the info about the server, ignoring errors
client, err := occlient.New()
if err == nil {
o.serverInfo, _ = client.GetServerVersion()
}
}
return nil
}
// Validate validates the VersionOptions based on completed values
func (o *VersionOptions) Validate() (err error) {
return
}
// Run contains the logic for the odo service create command
func (o *VersionOptions) Run() (err error) {
// If verbose mode is enabled, dump all KUBECLT_* env variables
// this is usefull for debuging oc plugin integration
for _, v := range os.Environ() {
if strings.HasPrefix(v, "KUBECTL_") {
glog.V(4).Info(v)
}
}
fmt.Println("odo " + odoversion.VERSION + " (" + odoversion.GITCOMMIT + ")")
if !o.clientFlag && o.serverInfo != nil {
// make sure we only include OpenShift info if we actually have it
openshiftStr := ""
if len(o.serverInfo.OpenShiftVersion) > 0 {
openshiftStr = fmt.Sprintf("OpenShift: %v\n", o.serverInfo.OpenShiftVersion)
}
fmt.Printf("\n"+
"Server: %v\n"+
"%v"+
"Kubernetes: %v\n",
o.serverInfo.Address,
openshiftStr,
o.serverInfo.KubernetesVersion)
}
return
}
// NewCmdVersion implements the version odo command
func NewCmdVersion(name, fullName string) *cobra.Command {
o := NewVersionOptions()
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: name,
Short: versionLongDesc,
Long: versionLongDesc,
Example: fmt.Sprintf(versionExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
// Add a defined annotation in order to appear in the help menu
versionCmd.Annotations = map[string]string{"command": "utility"}
versionCmd.SetUsageTemplate(util.CmdUsageTemplate)
versionCmd.Flags().BoolVar(&o.clientFlag, "client", false, "Client version only (no server required).")
return versionCmd
}
// GetLatestReleaseInfo Gets information about the latest release
func GetLatestReleaseInfo(info chan<- string) {
newTag, err := notify.CheckLatestReleaseTag(odoversion.VERSION)
if err != nil {
// The error is intentionally not being handled because we don't want
// to stop the execution of the program because of this failure
glog.V(4).Infof("Error checking if newer odo release is available: %v", err)
}
if len(newTag) > 0 {
info <- fmt.Sprintf(`
---
A newer version of odo (%s) is available,
visit %s to update.
If you wish to disable this notification, run:
odo preference set UpdateNotification false
---`, fmt.Sprint(newTag), OdoReleasesPage)
}
}