forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.go
More file actions
46 lines (36 loc) · 1018 Bytes
/
version.go
File metadata and controls
46 lines (36 loc) · 1018 Bytes
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
package version
import (
"fmt"
"regexp"
"strings"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/spf13/cobra"
)
func NewCmdVersion(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprint(f.IOStreams.Out, Format(version, buildDate))
},
}
cmdutil.DisableAuthCheck(cmd)
return cmd
}
func Format(version, buildDate string) string {
version = strings.TrimPrefix(version, "v")
var dateStr string
if buildDate != "" {
dateStr = fmt.Sprintf(" (%s)", buildDate)
}
return fmt.Sprintf("gh version %s%s\n%s\n", version, dateStr, changelogURL(version))
}
func changelogURL(version string) string {
path := "https://github.com/cli/cli"
r := regexp.MustCompile(`^v?\d+\.\d+\.\d+(-[\w.]+)?$`)
if !r.MatchString(version) {
return fmt.Sprintf("%s/releases/latest", path)
}
url := fmt.Sprintf("%s/releases/tag/v%s", path, strings.TrimPrefix(version, "v"))
return url
}