Skip to content

Commit

Permalink
Implement gnopls version cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Sep 22, 2023
1 parent c13556d commit 7a40b00
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func GnoplsCmd() *cobra.Command {
}

cmd.CompletionOptions.DisableDefaultCmd = true
cmd.AddCommand(CmdVersion())

return cmd
}
Expand Down
22 changes: 22 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
40 changes: 40 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7a40b00

Please sign in to comment.