From 7a40b002e4c3289fa76692bfa1ebfb08a936d76f Mon Sep 17 00:00:00 2001 From: Hariom Verma Date: Fri, 22 Sep 2023 06:18:28 +0530 Subject: [PATCH] Implement `gnopls version` cmd --- cmd/cmd.go | 1 + cmd/version.go | 22 ++++++++++++++++++++ go.mod | 2 ++ go.sum | 4 ++++ internal/version/version.go | 40 +++++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+) create mode 100644 cmd/version.go create mode 100644 internal/version/version.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 7808570..eea33a2 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -20,6 +20,7 @@ func GnoplsCmd() *cobra.Command { } cmd.CompletionOptions.DisableDefaultCmd = true + cmd.AddCommand(CmdVersion()) return cmd } diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..27cc2f8 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,22 @@ +package cmd + +import ( + "fmt" + + "github.com/harry-hov/gnopls/internal/version" + "github.com/spf13/cobra" +) + +func CmdVersion() *cobra.Command { + cmd := &cobra.Command{ + Use: "version", + Short: "print the gnopls version information", + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println(version.GetVersion(cmd.Context())) + return nil + }, + } + + return cmd +} diff --git a/go.mod b/go.mod index 2b0e192..1b9b9b8 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,12 @@ module github.com/harry-hov/gnopls go 1.21.1 require ( + github.com/google/go-github v17.0.0+incompatible github.com/spf13/cobra v1.5.0 ) require ( + github.com/google/go-querystring v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index dd95c40..39ac77f 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= +github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..737ff8d --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,40 @@ +package version + +import ( + "context" + + "github.com/google/go-github/github" +) + +const versionUnknown = "v0.0.0-unknown" + +var Version string = versionUnknown + +func getLatestReleaseTag(ctx context.Context) (string, error) { + latest, _, err := github. + NewClient(nil). + Repositories. + GetLatestRelease(ctx, "harry-hov", "gnopls") + if err != nil { + return "", err + } + + if latest.TagName == nil { + return "", nil + } + + return *latest.TagName, nil +} + +func GetVersion(ctx context.Context) string { + if Version != versionUnknown { + return Version + } + + tag, err := getLatestReleaseTag(ctx) + if err != nil { + return Version + } + + return tag +}