From 7909dd318df474fab2b6ea62789befe976aed5c7 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 7 Jul 2020 15:22:59 -0700 Subject: [PATCH] command/init: Specialized error message for host that isn't registry If the user specifies a host that isn't a provider registry in a provider source address then we'll print out some specialized error messages for different variants of that situation. In particular, this includes a special case for when the error is on the hostname "github.com", in anticipation of folks incorrectly attempting to use GitHub repository URLs (or Go-style module paths that happen to be on GitHub) to specify providers, so we can give a more specific hint about that. This is just a different presentation of an existing error case that we are already covering in the installer tests, so there are no new tests here. We could in principle have a test covering the exact text of these error messages, but we don't have much precedent for command package tests covering that level of cosmetic detail. --- command/init.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/command/init.go b/command/init.go index 65d9acdd1..50691b7fa 100644 --- a/command/init.go +++ b/command/init.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-config-inspect/tfconfig" + svchost "github.com/hashicorp/terraform-svchost" "github.com/posener/complete" "github.com/zclconf/go-cty/cty" @@ -518,6 +519,44 @@ func (c *InitCommand) getProviders(config *configs.Config, state *states.State, ), )) } + case getproviders.ErrHostNoProviders: + switch { + case errorTy.Hostname == svchost.Hostname("github.com") && !errorTy.HasOtherVersion: + // If a user copies the URL of a GitHub repository into + // the source argument and removes the schema to make it + // provider-address-shaped then that's one way we can end up + // here. We'll use a specialized error message in anticipation + // of that mistake. We only do this if github.com isn't a + // provider registry, to allow for the (admittedly currently + // rather unlikely) possibility that github.com starts being + // a real Terraform provider registry in the future. + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Invalid provider registry host", + fmt.Sprintf("The given source address %q specifies a GitHub repository rather than a Terraform provider. Refer to the documentation of the provider to find the correct source address to use.", + provider.String(), + ), + )) + + case errorTy.HasOtherVersion: + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Invalid provider registry host", + fmt.Sprintf("The host %q given in in provider source address %q does not offer a Terraform provider registry that is compatible with this Terraform version, but it may be compatible with a different Terraform version.", + errorTy.Hostname, provider.String(), + ), + )) + + default: + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Invalid provider registry host", + fmt.Sprintf("The host %q given in in provider source address %q does not offer a Terraform provider registry.", + errorTy.Hostname, provider.String(), + ), + )) + } + default: diags = diags.Append(tfdiags.Sourceless( tfdiags.Error,