Skip to content

Commit

Permalink
repo/variables.go: Report which service or variable was not found
Browse files Browse the repository at this point in the history
  • Loading branch information
livingsilver94 committed Nov 26, 2023
1 parent 1aa2c97 commit 64a3b3a
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions repo/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func NewVariables() Variables {
func (vars Variables) AddParent(srv, parent string) error {
val, ok := vars.resolved[srv]
if !ok {
return ErrNoService
return errNoService(srv)
}
if _, ok := vars.resolved[parent]; !ok {
return ErrNoService
return errNoService(parent)
}
val.Parents = append(val.Parents, parent)
vars.resolved[srv] = val
Expand All @@ -64,14 +64,14 @@ func (vars Variables) AddParent(srv, parent string) error {
// If the value is not clear text, it is resolved immediately and then cached.
// If key is already present for srv, Insert is no-op.
func (vars Variables) Insert(srv, key string, val service.VarValue) error {
switch _, err := vars.Get(srv, key); err {
case ErrNoService:
vars.resolved[srv] = value{Vars: make(map[string]string)}
case ErrNoVariable:
break
default:
_, err := vars.Get(srv, key)
if err == nil {
return nil
}
if errors.Is(err, ErrNoService) {
vars.resolved[srv] = value{Vars: make(map[string]string)}
}

var v string
if kind := val.Kind; kind == service.ClearText {
v = val.Value
Expand Down Expand Up @@ -106,24 +106,24 @@ func (vars Variables) InsertMany(srv string, values map[string]service.VarValue)
func (vars Variables) Parents(srv string) ([]string, error) {
val, ok := vars.resolved[srv]
if !ok {
return nil, ErrNoService
return nil, errNoService(srv)
}
return val.Parents, nil
}

// Get returns the value of a Service's variable, previously cached via Insert.
// If srv does not exist, ErrNoService is returned.
// If key does not exist, ErrNoVariable is returned.
func (vars Variables) Get(service, key string) (string, error) {
val, ok := vars.resolved[service]
func (vars Variables) Get(srv, key string) (string, error) {
val, ok := vars.resolved[srv]
if !ok {
return "", ErrNoService
return "", errNoService(srv)
}
variable, ok := val.Vars[key]
if !ok {
v, ok := vars.Common[key]
if !ok {
return "", ErrNoVariable
return "", errNoVariable(key)
}
variable = v
}
Expand Down Expand Up @@ -180,3 +180,11 @@ type value struct {
Parents []string
Vars map[string]string
}

func errNoService(name string) error {
return fmt.Errorf("%q %w", name, ErrNoService)
}

func errNoVariable(name string) error {
return fmt.Errorf("%q %w", name, ErrNoVariable)
}

0 comments on commit 64a3b3a

Please sign in to comment.