Files
deploy/util/io/paths.go
Michael CrenshawandGitHub c98ad49582 feat: collect and log hydration commands (#19346)
* feat: collect and log hydration commands

Signed-off-by: Michael Crenshaw <[email protected]>

* fix tests

Signed-off-by: Michael Crenshaw <[email protected]>

* don't actually log for now

Signed-off-by: Michael Crenshaw <[email protected]>

* add helm test, handle random values file path

Signed-off-by: Michael Crenshaw <[email protected]>

* fix test

Signed-off-by: Michael Crenshaw <[email protected]>

* simplify; only return commands on success; only return helm template command, not helm dependency

Signed-off-by: Michael Crenshaw <[email protected]>

* kustomize test

Signed-off-by: Michael Crenshaw <[email protected]>

* only return commands on success, to match Helm

Signed-off-by: Michael Crenshaw <[email protected]>

* fix import order

Signed-off-by: Michael Crenshaw <[email protected]>

* simplify, since we're not collecting non-template Helm commands

Signed-off-by: Michael Crenshaw <[email protected]>

* lint

Signed-off-by: Michael Crenshaw <[email protected]>

* fixes from comments

Signed-off-by: Michael Crenshaw <[email protected]>

---------

Signed-off-by: Michael Crenshaw <[email protected]>
2024-08-09 16:06:58 +00:00

73 lines
1.6 KiB
Go

package io
import (
"path/filepath"
"sync"
"github.com/google/uuid"
)
type TempPaths interface {
Add(key string, value string)
GetPath(key string) (string, error)
GetPathIfExists(key string) string
GetPaths() map[string]string
}
// RandomizedTempPaths allows generating and memoizing random paths, each path being mapped to a specific key.
type RandomizedTempPaths struct {
root string
paths map[string]string
lock sync.RWMutex
}
func NewRandomizedTempPaths(root string) *RandomizedTempPaths {
return &RandomizedTempPaths{
root: root,
paths: map[string]string{},
}
}
func (p *RandomizedTempPaths) Add(key string, value string) {
p.lock.Lock()
defer p.lock.Unlock()
p.paths[key] = value
}
// GetPath generates a path for the given key or returns previously generated one.
func (p *RandomizedTempPaths) GetPath(key string) (string, error) {
p.lock.Lock()
defer p.lock.Unlock()
if val, ok := p.paths[key]; ok {
return val, nil
}
uniqueId, err := uuid.NewRandom()
if err != nil {
return "", err
}
repoPath := filepath.Join(p.root, uniqueId.String())
p.paths[key] = repoPath
return repoPath, nil
}
// GetPathIfExists gets a path for the given key if it exists. Otherwise, returns an empty string.
func (p *RandomizedTempPaths) GetPathIfExists(key string) string {
p.lock.RLock()
defer p.lock.RUnlock()
if val, ok := p.paths[key]; ok {
return val
}
return ""
}
// GetPaths gets a copy of the map of paths.
func (p *RandomizedTempPaths) GetPaths() map[string]string {
p.lock.RLock()
defer p.lock.RUnlock()
paths := map[string]string{}
for k, v := range p.paths {
paths[k] = v
}
return paths
}