Debian Codegraph @

module_package.go

raw
1 package tfaddr
2
3 import (
4 "strings"
5
6 svchost "github.com/hashicorp/terraform-svchost"
7 )
8
9 // A ModulePackage is an extra indirection over a ModulePackage where
10 // we use a module registry to translate a more symbolic address (and
11 // associated version constraint given out of band) into a physical source
12 // location.
13 //
14 // ModulePackage is distinct from ModulePackage because they have
15 // disjoint use-cases: registry package addresses are only used to query a
16 // registry in order to find a real module package address. These being
17 // distinct is intended to help future maintainers more easily follow the
18 // series of steps in the module installer, with the help of the type checker.
19 type ModulePackage struct {
20 Host svchost.Hostname
21 Namespace string
22 Name string
23 TargetSystem string
24 }
25
26 func (s ModulePackage) String() string {
27 // Note: we're using the "display" form of the hostname here because
28 // for our service hostnames "for display" means something different:
29 // it means to render non-ASCII characters directly as Unicode
30 // characters, rather than using the "punycode" representation we
31 // use for internal processing, and so the "display" representation
32 // is actually what users would write in their configurations.
33 return s.Host.ForDisplay() + "/" + s.ForRegistryProtocol()
34 }
35
36 func (s ModulePackage) ForDisplay() string {
37 if s.Host == DefaultModuleRegistryHost {
38 return s.ForRegistryProtocol()
39 }
40 return s.Host.ForDisplay() + "/" + s.ForRegistryProtocol()
41 }
42
43 // ForRegistryProtocol returns a string representation of just the namespace,
44 // name, and target system portions of the address, always omitting the
45 // registry hostname and the subdirectory portion, if any.
46 //
47 // This is primarily intended for generating addresses to send to the
48 // registry in question via the registry protocol, since the protocol
49 // skips sending the registry its own hostname as part of identifiers.
50 func (s ModulePackage) ForRegistryProtocol() string {
51 var buf strings.Builder
52 buf.WriteString(s.Namespace)
53 buf.WriteByte('/')
54 buf.WriteString(s.Name)
55 buf.WriteByte('/')
56 buf.WriteString(s.TargetSystem)
57 return buf.String()
58 }