-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved resource referencing and added useful volume resource …
…support
- Loading branch information
1 parent
e5a668f
commit ddaa37a
Showing
10 changed files
with
318 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package compose | ||
|
||
import ( | ||
"maps" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type EnvVarTracker struct { | ||
lookup func(key string) (string, bool) | ||
accessed map[string]string | ||
} | ||
|
||
func NewEnvVarTracker() *EnvVarTracker { | ||
return &EnvVarTracker{ | ||
lookup: os.LookupEnv, | ||
accessed: make(map[string]string), | ||
} | ||
} | ||
|
||
func (e *EnvVarTracker) Accessed() map[string]string { | ||
return maps.Clone(e.accessed) | ||
} | ||
|
||
// the env var tracker is a resource itself (an environment resource) | ||
var _ ResourceWithOutputs = (*EnvVarTracker)(nil) | ||
|
||
func (e *EnvVarTracker) LookupOutput(keys ...string) (interface{}, error) { | ||
if len(keys) == 0 { | ||
panic("requires at least 1 key") | ||
} | ||
envVarKey := strings.ToUpper(strings.Join(keys, "_")) | ||
envVarKey = strings.ReplaceAll(envVarKey, "-", "_") | ||
if v, ok := e.lookup(envVarKey); ok { | ||
e.accessed[envVarKey] = v | ||
} else { | ||
e.accessed[envVarKey] = "" | ||
} | ||
return "${" + envVarKey + "}", nil | ||
} | ||
|
||
type envVarResourceTracker struct { | ||
prefix string | ||
inner *EnvVarTracker | ||
} | ||
|
||
func (e *envVarResourceTracker) LookupOutput(keys ...string) (interface{}, error) { | ||
next := make([]string, 1+len(keys)) | ||
next[0] = e.prefix | ||
for i, k := range keys { | ||
next[1+i] = k | ||
} | ||
return e.inner.LookupOutput(next...) | ||
} | ||
|
||
func (e *EnvVarTracker) GenerateResource(resName string) ResourceWithOutputs { | ||
return &envVarResourceTracker{ | ||
inner: e, | ||
prefix: resName, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package compose | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type ResourceWithOutputs interface { | ||
LookupOutput(keys ...string) (interface{}, error) | ||
} | ||
|
||
type StaticResource struct { | ||
Outputs map[string]interface{} | ||
} | ||
|
||
func (sr *StaticResource) LookupOutput(keys ...string) (interface{}, error) { | ||
resolvedValue := interface{}(sr.Outputs) | ||
remainingKeys := keys | ||
for partIndex, part := range remainingKeys { | ||
mapV, ok := resolvedValue.(map[string]interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf("cannot lookup a key in %T", resolvedValue) | ||
} | ||
resolvedValue, ok = mapV[part] | ||
if !ok { | ||
return nil, fmt.Errorf("output '%s' does not exist", strings.Join(keys[:partIndex], ".")) | ||
} | ||
} | ||
return resolvedValue, nil | ||
} |
Oops, something went wrong.