Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/cmd/version.go
2497 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"time"
10
11
"github.com/gitpod-io/local-app/pkg/constants"
12
"github.com/gitpod-io/local-app/pkg/prettyprint"
13
"github.com/gitpod-io/local-app/pkg/selfupdate"
14
"github.com/sagikazarmark/slog-shim"
15
"github.com/spf13/cobra"
16
)
17
18
var versionCmd = &cobra.Command{
19
Use: "version",
20
Short: "Prints the CLI version",
21
RunE: func(cmd *cobra.Command, args []string) error {
22
type Version struct {
23
Version string `print:"version"`
24
GitCommit string `print:"commit"`
25
BuildTime string `print:"built at"`
26
Latest string `print:"latest"`
27
}
28
v := Version{
29
Version: constants.Version.String(),
30
GitCommit: constants.GitCommit,
31
BuildTime: constants.MustParseBuildTime().Format(time.RFC3339),
32
Latest: "<not available>",
33
}
34
35
if !versionOpts.DontCheckLatest {
36
dlctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
37
defer cancel()
38
39
mf, err := selfupdate.DownloadManifestFromActiveContext(dlctx)
40
if err != nil {
41
slog.Debug("cannot download manifest", "err", err)
42
}
43
if mf != nil {
44
v.Latest = mf.Version.String()
45
}
46
}
47
48
return WriteTabular([]Version{v}, versionOpts.Format, prettyprint.WriterFormatNarrow)
49
},
50
}
51
52
var versionOpts struct {
53
Format formatOpts
54
DontCheckLatest bool
55
}
56
57
func init() {
58
rootCmd.AddCommand(versionCmd)
59
addFormatFlags(versionCmd, &versionOpts.Format)
60
61
versionCmd.Flags().BoolVar(&versionOpts.DontCheckLatest, "dont-check-latest", false, "Don't check for the latest available version")
62
}
63
64
// isVersionCommand returns true if the given command is a version command
65
func isVersionCommand(cmd *cobra.Command) bool {
66
return cmd == versionCmd || cmd.Parent() == versionCmd
67
}
68
69