@@ -2,15 +2,18 @@ package ngrok
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "os"
8+ "os/exec"
79 "path/filepath"
810 "runtime"
911 "strings"
1012
1113 "github.com/1Password/shell-plugins/sdk"
1214 "github.com/1Password/shell-plugins/sdk/importer"
1315 "github.com/1Password/shell-plugins/sdk/schema/fieldname"
16+ "golang.org/x/mod/semver"
1417 "gopkg.in/yaml.v3"
1518)
1619
@@ -19,12 +22,30 @@ const (
1922 apiKeyYamlName = "api_key"
2023 authTokenYamlName = "authtoken"
2124 versionYamlName = "version"
25+ envVarAuthVersion = "v3.2.1"
2226)
2327
2428type fileProvisioner struct {
2529}
2630
27- func newNgrokProvisioner () sdk.Provisioner {
31+ func ngrokProvisioner () sdk.Provisioner {
32+ currentVersion , err := getNgrokVersion ()
33+ if err != nil {
34+ // When ngrok version check fails for any reason,
35+ // use config file to provision as a fallback
36+ return fileProvisioner {}
37+ }
38+
39+ // If the current ngrok CLI version is 3.2.1 or higher,
40+ // use environment variables to provision credentials
41+ //
42+ // semver.Compare resulting in 0 means 3.2.1 is in use
43+ // semver.Compare resulting in +1 means >3.2.1 is in use
44+ if semver .Compare (currentVersion , envVarAuthVersion ) >= 0 {
45+ return ngrokEnvVarProvisioner {}
46+ }
47+
48+ // Otherwise use config file to provision credentials
2849 return fileProvisioner {}
2950}
3051
@@ -95,6 +116,27 @@ func getConfigValueAndNewArgs(args []string, newFilePath string) (string, []stri
95116 return "" , append (args , fmt .Sprintf ("--config=%s" , newFilePath ))
96117}
97118
119+ // Get installed ngrok version and required version
120+ func getNgrokVersion () (string , error ) {
121+ cmd := exec .Command ("ngrok" , "--version" )
122+ ngrokVersion , err := cmd .Output ()
123+ if err != nil {
124+ return "" , errors .New ("ngrok not found" )
125+ }
126+
127+ // Example: "ngrok version 3.1.1\n" to "3.1.1\n"
128+ currentVersion := strings .TrimPrefix (string (ngrokVersion ), "ngrok version " )
129+
130+ // Example: "3.1.1\n" to "3.1.1"
131+ currentVersion = strings .Trim (currentVersion , "\n " )
132+
133+ // Example: "3.1.1" to "v3.1.1" as that's the format
134+ // the package semver expects
135+ currentVersion = "v" + currentVersion
136+
137+ return currentVersion , nil
138+ }
139+
98140func (f fileProvisioner ) Deprovision (ctx context.Context , in sdk.DeprovisionInput , out * sdk.DeprovisionOutput ) {
99141 // nothing to do here: files get deleted automatically by 1Password CLI
100142}
0 commit comments