Skip to content

Commit

Permalink
Switch to golang.org/x/mod/semver for the version check
Browse files Browse the repository at this point in the history
  • Loading branch information
arunsathiya committed Mar 29, 2023
1 parent 04bef9a commit d824696
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.3.4 // indirect
github.com/hashicorp/go-hclog v0.14.1 // indirect
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
Expand All @@ -33,6 +32,7 @@ require (
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.9.0
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ github.com/hashicorp/go-hclog v0.14.1 h1:nQcJDQwIAGnmoUWp8ubocEX40cCml/17YkF6csQ
github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-plugin v1.4.6 h1:MDV3UrKQBM3du3G7MApDGvOsMYy3JQJ4exhSoKBAeVA=
github.com/hashicorp/go-plugin v1.4.6/go.mod h1:viDMjcLJuDui6pXb8U4HVfb8AamCWhHGUjr2IrTF67s=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
Expand Down Expand Up @@ -92,6 +90,8 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
33 changes: 16 additions & 17 deletions plugins/ngrok/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
"github.com/1Password/shell-plugins/sdk/importer"
"github.com/1Password/shell-plugins/sdk/provision"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
"github.com/hashicorp/go-version"
"golang.org/x/mod/semver"
"gopkg.in/yaml.v3"
)

const (
ngrokVersion = "2"
version = "2"
apiKeyYamlName = "api_key"
authTokenYamlName = "authtoken"
versionYamlName = "version"
Expand All @@ -30,32 +30,31 @@ type fileProvisioner struct {

func ngrokProvisioner() sdk.Provisioner {
cmd := exec.Command("ngrok", "--version")
versionByte, err := cmd.Output()
ngrokVersion, err := cmd.Output()
if err != nil {
log.Fatalf(err.Error())
}

// Example: "ngrok version 3.1.1\n" to "3.1.1\n"
versionString := strings.TrimPrefix(string(versionByte), "ngrok version ")
currentVersion := strings.TrimPrefix(string(ngrokVersion), "ngrok version ")

// Example: "3.1.1\n" to "3.1.1"
versionString = strings.Trim(versionString, "\n")
currentVersion = strings.Trim(currentVersion, "\n")

// NGROK_API_KEY is supported only from ngrok 3.2.1
// NGROK_AUTH_TOKEN has already been supported
requiredVersion, err := version.NewVersion("3.2.1")
if err != nil {
log.Fatalf(err.Error())
}
// Example: "3.1.1" to "v3.1.1" as that's the format
// the package semver expects
currentVersion = "v" + currentVersion

currentVersion, err := version.NewVersion(versionString)
if err != nil {
log.Fatalf(err.Error())
}
// NGROK_API_KEY is supported only from ngrok 3.2.1
// NGROK_AUTH_TOKEN was already supported
requiredVersion := "v3.2.1"

// If the current ngrok CLI version is 3.2.1 or higher,
// use environment variables to provision the Shell Plugin credentials
if currentVersion.GreaterThan(requiredVersion) || currentVersion.Equal(requiredVersion) {
//
// semver.Compare resulting in 0 means 3.2.1 is in use
// semver.Compare resulting in +1 means >3.2.1 is in use
if semver.Compare(currentVersion, requiredVersion) == 0 || semver.Compare(currentVersion, requiredVersion) == +1 {
return provision.EnvVars(defaultEnvVarMapping)
}

Expand Down Expand Up @@ -99,7 +98,7 @@ func (f fileProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, o

config[authTokenYamlName] = in.ItemFields[fieldname.Authtoken]
config[apiKeyYamlName] = in.ItemFields[fieldname.APIKey]
config[versionYamlName] = ngrokVersion
config[versionYamlName] = version

newContents, err := yaml.Marshal(&config)
if err != nil {
Expand Down

0 comments on commit d824696

Please sign in to comment.