Skip to content

Commit

Permalink
feat: CLI argument for custom registry URL was introduced (#93)
Browse files Browse the repository at this point in the history
* feat: CLI argument for custom provider URL was introduced

* feat: CLI argument was moved to app.Config
  • Loading branch information
misterku authored Oct 12, 2022
1 parent a4dc8c2 commit 3759eb5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
7 changes: 7 additions & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ func installCmd() *cobra.Command {

var customBuildCommand string

var customTerraformRegistryURL string

cmd := &cobra.Command{
Use: "install [providerName]",
Args: cobra.ExactArgs(1),
Expand All @@ -22,6 +24,10 @@ func installCmd() *cobra.Command {
a := app.New()
a.Init()

if customTerraformRegistryURL != "" {
a.SetTerraformRegistryURL(customTerraformRegistryURL)
}

if a.IsTerraformPluginDirExistent() {
a.Install(args[0], versionString, customBuildCommand)
} else {
Expand All @@ -33,6 +39,7 @@ func installCmd() *cobra.Command {
}
cmd.Flags().StringVarP(&versionString, "version", "v", "", "The version of the provider")
cmd.Flags().StringVar(&customBuildCommand, "custom-build-command", "", "A custom build command to execute instead of the built-in commands")
cmd.Flags().StringVarP(&customTerraformRegistryURL, "custom-terraform-registry-url", "u", "", "A custom URL of terraform registry")

return cmd
}
7 changes: 7 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ type Config struct {
BaseDir string
GoPath string
ProvidersCacheDir string
TerraformRegistryURL string
RequestTimeoutInSeconds int
}

const (
DefaultProvidersCacheDir = "/.m1-terraform-provider-helper"
DefaultTerraformPluginDir = "/.terraform.d/plugins"
DefaultTerraformPluginBackupDir = "/.terraform.d/plugins_backup"
DefaultTerraformRegistryURL = "https://registry.terraform.io/v1/providers/"
FileModePerm = 0777
DefaultRequestTimeoutInSeconds = 10
)
Expand All @@ -47,6 +49,7 @@ func New() *App {
TerraformPluginDir: BaseDir + DefaultTerraformPluginDir,
TerraformPluginBackupDir: BaseDir + DefaultTerraformPluginBackupDir,
ProvidersCacheDir: BaseDir + DefaultProvidersCacheDir,
TerraformRegistryURL: DefaultTerraformRegistryURL,
},
Out: os.Stdout,
}
Expand Down Expand Up @@ -177,3 +180,7 @@ func (a *App) ListProviders() {
fmt.Fprintf(a.Out, "%s -> %s\n", k, strings.Join(v, ", "))
}
}

func (a *App) SetTerraformRegistryURL(url string) {
a.Config.TerraformRegistryURL = url
}
1 change: 1 addition & 0 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func setupTestAppInstance(t *testing.T) (app.App, *bytes.Buffer) {
BaseDir: tmpDir,
GoPath: app.GetCurrentGoPath(),
ProvidersCacheDir: tmpDir + "/cliCache",
TerraformRegistryURL: app.DefaultTerraformRegistryURL,
}
app := app.App{
Config: &config,
Expand Down
10 changes: 7 additions & 3 deletions internal/app/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ func executeBashCommand(command string, baseDir string) string {
return string(output)
}

func getProviderData(providerName string, requestTimeoutInSeconds int) (Provider, error) {
url := "https://registry.terraform.io/v1/providers/" + providerName
func (a *App) GetProviderData(providerName string) (Provider, error) {
return getProviderData(providerName, a.Config.RequestTimeoutInSeconds, a.Config.TerraformRegistryURL)
}

func getProviderData(providerName string, requestTimeoutInSeconds int, terraformRegistryURL string) (Provider, error) {
url := terraformRegistryURL + providerName

client := &http.Client{Timeout: time.Second * time.Duration(float64(requestTimeoutInSeconds))}
ctx := context.Background()
Expand Down Expand Up @@ -306,7 +310,7 @@ func getTerraformVersion() *goversion.Version {
func (a *App) Install(providerName string, version string, customBuildCommand string) bool {
fmt.Fprintf(a.Out, "Getting provider data from terraform registry\n")

providerData, err := getProviderData(providerName, a.Config.RequestTimeoutInSeconds)
providerData, err := a.GetProviderData(providerName)

if err != nil {
logrus.Fatalf("Error while trying to get provider data from terraform registry: %v", err.Error())
Expand Down
6 changes: 3 additions & 3 deletions internal/app/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestGetProviderData(t *testing.T) {
httpmock.RegisterResponder("GET", "https://registry.terraform.io/v1/providers/"+provider,
httpmock.NewStringResponder(200, `{"description": "`+description+`",
"source": "`+repo+`"}`))
providerData, err := getProviderData(provider, 2)
providerData, err := getProviderData(provider, 2, DefaultTerraformRegistryURL)
if err != nil {
t.Errorf("Should not have an error %s ", err)
}
Expand All @@ -89,7 +89,7 @@ func TestGetProviderData(t *testing.T) {
httpmock.RegisterResponder("GET", "https://registry.terraform.io/v1/providers/"+provider,
httpmock.NewStringResponder(200, `{"description": "`+description+`",
"source": "`+repo+`"}`).Delay(3*time.Second))
_, err := getProviderData(provider, 2)
_, err := getProviderData(provider, 2, DefaultTerraformRegistryURL)
if !strings.HasPrefix(err.Error(), "timeout error") {
t.Errorf("Expected \"error timeout\" but got %#v", err.Error())
}
Expand All @@ -103,7 +103,7 @@ func TestGetProviderData(t *testing.T) {
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", "https://registry.terraform.io/v1/providers/"+provider,
httpmock.NewStringResponder(200, `{"test:"812}`))
_, err := getProviderData(provider, 2)
_, err := getProviderData(provider, 2, DefaultTerraformRegistryURL)
if err == nil {
t.Error("Should run into JSON parse error")
}
Expand Down

0 comments on commit 3759eb5

Please sign in to comment.