-
Notifications
You must be signed in to change notification settings - Fork 91
/
serve_opts.go
65 lines (54 loc) · 1.9 KB
/
serve_opts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package providerserver
import (
"context"
"fmt"
"strings"
)
// ServeOpts are options for serving the provider.
type ServeOpts struct {
// Address is the full address of the provider. Full address form has three
// parts separated by forward slashes (/): Hostname, namespace, and
// provider type ("name").
//
// For example: registry.terraform.io/hashicorp/random.
Address string
// Debug runs the provider in a mode acceptable for debugging and testing
// processes, such as delve, by managing the process lifecycle. Information
// needed for Terraform CLI to connect to the provider is output to stdout.
// os.Interrupt (Ctrl-c) can be used to stop the provider.
Debug bool
}
// Validate a given provider address. This is only used for the Address field
// to preserve backwards compatibility for the Name field.
//
// This logic is manually implemented over importing
// github.com/hashicorp/terraform-registry-address as its functionality such as
// ParseAndInferProviderSourceString and ParseRawProviderSourceString allow
// shorter address formats, which would then require post-validation anyways.
func (opts ServeOpts) validateAddress(_ context.Context) error {
addressParts := strings.Split(opts.Address, "/")
formatErr := fmt.Errorf("expected hostname/namespace/type format, got: %s", opts.Address)
if len(addressParts) != 3 {
return formatErr
}
if addressParts[0] == "" || addressParts[1] == "" || addressParts[2] == "" {
return formatErr
}
return nil
}
// Validation checks for provider defined ServeOpts.
//
// Current checks which return errors:
//
// - If Address is not set
// - Address is a valid full provider address
func (opts ServeOpts) validate(ctx context.Context) error {
if opts.Address == "" {
return fmt.Errorf("Address must be provided")
}
err := opts.validateAddress(ctx)
if err != nil {
return fmt.Errorf("unable to validate Address: %w", err)
}
return nil
}