Extract version command
This commit is contained in:
parent
1a1a6303cd
commit
bda27a46ae
3 changed files with 53 additions and 32 deletions
|
|
@ -1,10 +1,7 @@
|
|||
package root
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/api"
|
||||
|
|
@ -22,6 +19,7 @@ import (
|
|||
releaseCmd "github.com/cli/cli/pkg/cmd/release"
|
||||
repoCmd "github.com/cli/cli/pkg/cmd/repo"
|
||||
creditsCmd "github.com/cli/cli/pkg/cmd/repo/credits"
|
||||
versionCmd "github.com/cli/cli/pkg/cmd/version"
|
||||
"github.com/cli/cli/pkg/cmdutil"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
|
@ -49,23 +47,6 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
|
|||
},
|
||||
}
|
||||
|
||||
version = strings.TrimPrefix(version, "v")
|
||||
if buildDate == "" {
|
||||
cmd.Version = version
|
||||
} else {
|
||||
cmd.Version = fmt.Sprintf("%s (%s)", version, buildDate)
|
||||
}
|
||||
versionOutput := fmt.Sprintf("gh version %s\n%s\n", cmd.Version, changelogURL(version))
|
||||
cmd.AddCommand(&cobra.Command{
|
||||
Use: "version",
|
||||
Hidden: true,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Print(versionOutput)
|
||||
},
|
||||
})
|
||||
cmd.SetVersionTemplate(versionOutput)
|
||||
cmd.Flags().Bool("version", false, "Show gh version")
|
||||
|
||||
cmd.SetOut(f.IOStreams.Out)
|
||||
cmd.SetErr(f.IOStreams.ErrOut)
|
||||
|
||||
|
|
@ -74,7 +55,13 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
|
|||
cmd.SetUsageFunc(rootUsageFunc)
|
||||
cmd.SetFlagErrorFunc(rootFlagErrrorFunc)
|
||||
|
||||
formattedVersion := versionCmd.Format(version, buildDate)
|
||||
cmd.SetVersionTemplate(formattedVersion)
|
||||
cmd.Version = formattedVersion
|
||||
cmd.Flags().Bool("version", false, "Show gh version")
|
||||
|
||||
// Child commands
|
||||
cmd.AddCommand(versionCmd.NewCmdVersion(f, version, buildDate))
|
||||
cmd.AddCommand(aliasCmd.NewCmdAlias(f))
|
||||
cmd.AddCommand(authCmd.NewCmdAuth(f))
|
||||
cmd.AddCommand(configCmd.NewCmdConfig(f))
|
||||
|
|
@ -140,14 +127,3 @@ func resolvedBaseRepo(f *cmdutil.Factory) func() (ghrepo.Interface, error) {
|
|||
return baseRepo, nil
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
45
pkg/cmd/version/version.go
Normal file
45
pkg/cmd/version/version.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package version
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/cli/cli/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")
|
||||
|
||||
if buildDate != "" {
|
||||
version = fmt.Sprintf("%s (%s)", version, buildDate)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("gh version %s\n%s\n", version, 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
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package root
|
||||
package version
|
||||
|
||||
import (
|
||||
"testing"
|
||||
Loading…
Add table
Add a link
Reference in a new issue