Skip to content

Commit

Permalink
fixup: add section to func_yaml.md (and alphabetize it)
Browse files Browse the repository at this point in the history
Plus incorporate PR feedback

Signed-off-by: Lance Ball <lball@redhat.com>
  • Loading branch information
lance committed Jul 26, 2021
1 parent 9e339f7 commit 20bcbac
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 198 deletions.
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var configCmd = &cobra.Command{
Long: `Configure a function
Interactive propmt that allows configuration of Volume mounts, Environment
variables, and labels for a function project present in the current directory
variables, and Labels for a function project present in the current directory
or from the directory specified with --path.
`,
SuggestFor: []string{"cfg", "cofnig"},
Expand Down
4 changes: 2 additions & 2 deletions cmd/config_envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func runAddEnvsPrompt(ctx context.Context, f fn.Function) (err error) {
return
}

newEnv := fn.Tuple{}
newEnv := fn.Pair{}

switch selectedOption {
// SECTION - add new Environment variable with the specified value
Expand Down Expand Up @@ -403,7 +403,7 @@ func runRemoveEnvsPrompt(f fn.Function) (err error) {
return
}

var newEnvs fn.Tuples
var newEnvs fn.Pairs
removed := false
for i, e := range f.Envs {
if e.String() == selectedEnv {
Expand Down
32 changes: 18 additions & 14 deletions cmd/config_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func runAddLabelsPrompt(ctx context.Context, f fn.Function) (err error) {
return
}

newTuple := fn.Tuple{}
newPair := fn.Pair{}

switch selectedOption {
// SECTION - add new label with the specified value
Expand All @@ -162,13 +162,15 @@ func runAddLabelsPrompt(ctx context.Context, f fn.Function) (err error) {
Name: "name",
Prompt: &survey.Input{Message: "Please specify the label name:"},
Validate: func(val interface{}) error {
return utils.ValidateLabelName(val.(string))
return utils.ValidateLabel(val.(string), "")
},
},
{
Name: "value",
Prompt: &survey.Input{Message: "Please specify the label value:"},
},
Validate: func(val interface{}) error {
return utils.ValidateLabel("", val.(string))
}},
}
answers := struct {
Name string
Expand All @@ -183,8 +185,8 @@ func runAddLabelsPrompt(ctx context.Context, f fn.Function) (err error) {
return
}

newTuple.Name = &answers.Name
newTuple.Value = &answers.Value
newPair.Name = &answers.Name
newPair.Value = &answers.Value

// SECTION - add new label with value from a local environment variable
case optionLabelLocal:
Expand All @@ -193,13 +195,15 @@ func runAddLabelsPrompt(ctx context.Context, f fn.Function) (err error) {
Name: "name",
Prompt: &survey.Input{Message: "Please specify the label name:"},
Validate: func(val interface{}) error {
return utils.ValidateLabelName(val.(string))
return utils.ValidateLabel(val.(string), "")
},
},
{
Name: "value",
Prompt: &survey.Input{Message: "Please specify the local environment variable:"},
Validate: survey.Required,
Name: "value",
Prompt: &survey.Input{Message: "Please specify the local environment variable:"},
Validate: func(val interface{}) error {
return utils.ValidateLabel("", val.(string))
},
},
}
answers := struct {
Expand All @@ -220,16 +224,16 @@ func runAddLabelsPrompt(ctx context.Context, f fn.Function) (err error) {
}

value := fmt.Sprintf("{{ env:%s }}", answers.Value)
newTuple.Name = &answers.Name
newTuple.Value = &value
newPair.Name = &answers.Name
newPair.Value = &value
}

// we have all necessary information -> let's insert the label to the selected position in the list
if insertToIndex == len(f.Labels) {
f.Labels = append(f.Labels, newTuple)
f.Labels = append(f.Labels, newPair)
} else {
f.Labels = append(f.Labels[:insertToIndex+1], f.Labels[insertToIndex:]...)
f.Labels[insertToIndex] = newTuple
f.Labels[insertToIndex] = newPair
}

err = f.WriteConfig()
Expand Down Expand Up @@ -264,7 +268,7 @@ func runRemoveLabelsPrompt(f fn.Function) (err error) {
return
}

var newLabels fn.Tuples
var newLabels fn.Pairs
removed := false
for i, e := range f.Labels {
if e.String() == selectedLabel {
Expand Down
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func envFromCmd(cmd *cobra.Command) (*util.OrderedMap, []string, error) {
return util.NewOrderedMap(), []string{}, nil
}

func mergeEnvs(envs fn.Tuples, envToUpdate *util.OrderedMap, envToRemove []string) (fn.Tuples, error) {
func mergeEnvs(envs fn.Pairs, envToUpdate *util.OrderedMap, envToRemove []string) (fn.Pairs, error) {
updated := sets.NewString()

for i := range envs {
Expand All @@ -294,7 +294,7 @@ func mergeEnvs(envs fn.Tuples, envToUpdate *util.OrderedMap, envToRemove []strin
if !updated.Has(name) {
n := name
v := value
envs = append(envs, fn.Tuple{Name: &n, Value: &v})
envs = append(envs, fn.Pair{Name: &n, Value: &v})
}
}

Expand All @@ -309,7 +309,7 @@ func mergeEnvs(envs fn.Tuples, envToUpdate *util.OrderedMap, envToRemove []strin

errMsg := fn.ValidateEnvs(envs)
if len(errMsg) > 0 {
return fn.Tuples{}, fmt.Errorf(strings.Join(errMsg, "\n"))
return fn.Pairs{}, fmt.Errorf(strings.Join(errMsg, "\n"))
}

return envs, nil
Expand Down
32 changes: 16 additions & 16 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,77 @@ func Test_mergeEnvMaps(t *testing.T) {
v2 := "y"

type args struct {
envs fn.Tuples
envs fn.Pairs
toUpdate *util.OrderedMap
toRemove []string
}
tests := []struct {
name string
args args
want fn.Tuples
want fn.Pairs
}{
{
"add new var to empty list",
args{
fn.Tuples{},
fn.Pairs{},
util.NewOrderedMapWithKVStrings([][]string{{a, v1}}),
[]string{},
},
fn.Tuples{fn.Tuple{Name: &a, Value: &v1}},
fn.Pairs{fn.Pair{Name: &a, Value: &v1}},
},
{
"add new var",
args{
fn.Tuples{fn.Tuple{Name: &b, Value: &v2}},
fn.Pairs{fn.Pair{Name: &b, Value: &v2}},
util.NewOrderedMapWithKVStrings([][]string{{a, v1}}),
[]string{},
},
fn.Tuples{fn.Tuple{Name: &b, Value: &v2}, fn.Tuple{Name: &a, Value: &v1}},
fn.Pairs{fn.Pair{Name: &b, Value: &v2}, fn.Pair{Name: &a, Value: &v1}},
},
{
"update var",
args{
fn.Tuples{fn.Tuple{Name: &a, Value: &v1}},
fn.Pairs{fn.Pair{Name: &a, Value: &v1}},
util.NewOrderedMapWithKVStrings([][]string{{a, v2}}),
[]string{},
},
fn.Tuples{fn.Tuple{Name: &a, Value: &v2}},
fn.Pairs{fn.Pair{Name: &a, Value: &v2}},
},
{
"update multiple vars",
args{
fn.Tuples{fn.Tuple{Name: &a, Value: &v1}, fn.Tuple{Name: &b, Value: &v2}},
fn.Pairs{fn.Pair{Name: &a, Value: &v1}, fn.Pair{Name: &b, Value: &v2}},
util.NewOrderedMapWithKVStrings([][]string{{a, v2}, {b, v1}}),
[]string{},
},
fn.Tuples{fn.Tuple{Name: &a, Value: &v2}, fn.Tuple{Name: &b, Value: &v1}},
fn.Pairs{fn.Pair{Name: &a, Value: &v2}, fn.Pair{Name: &b, Value: &v1}},
},
{
"remove var",
args{
fn.Tuples{fn.Tuple{Name: &a, Value: &v1}},
fn.Pairs{fn.Pair{Name: &a, Value: &v1}},
util.NewOrderedMap(),
[]string{a},
},
fn.Tuples{},
fn.Pairs{},
},
{
"remove multiple vars",
args{
fn.Tuples{fn.Tuple{Name: &a, Value: &v1}, fn.Tuple{Name: &b, Value: &v2}},
fn.Pairs{fn.Pair{Name: &a, Value: &v1}, fn.Pair{Name: &b, Value: &v2}},
util.NewOrderedMap(),
[]string{a, b},
},
fn.Tuples{},
fn.Pairs{},
},
{
"update and remove vars",
args{
fn.Tuples{fn.Tuple{Name: &a, Value: &v1}, fn.Tuple{Name: &b, Value: &v2}},
fn.Pairs{fn.Pair{Name: &a, Value: &v1}, fn.Pair{Name: &b, Value: &v2}},
util.NewOrderedMapWithKVStrings([][]string{{a, v2}}),
[]string{b},
},
fn.Tuples{fn.Tuple{Name: &a, Value: &v2}},
fn.Pairs{fn.Pair{Name: &a, Value: &v2}},
},
}
for _, tt := range tests {
Expand Down
18 changes: 9 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func (v Volume) String() string {
return ""
}

type Tuples []Tuple
type Tuple struct {
type Pairs []Pair
type Pair struct {
Name *string `yaml:"name,omitempty"`
Value *string `yaml:"value"`
}

func (e Tuple) String() string {
func (e Pair) String() string {
if e.Name == nil && e.Value != nil {
match := regWholeSecret.FindStringSubmatch(*e.Value)
if len(match) == 2 {
Expand Down Expand Up @@ -117,10 +117,10 @@ type config struct {
Builder string `yaml:"builder"`
BuilderMap map[string]string `yaml:"builderMap"`
Volumes Volumes `yaml:"volumes"`
Envs Tuples `yaml:"envs"`
Envs Pairs `yaml:"envs"`
Annotations map[string]string `yaml:"annotations"`
Options Options `yaml:"options"`
Labels Tuples `yaml:"labels"`
Labels Pairs `yaml:"labels"`
// Add new values to the toConfig/fromConfig functions.
}

Expand Down Expand Up @@ -299,7 +299,7 @@ func validateVolumes(volumes Volumes) (errors []string) {
// - name: EXAMPLE4
// value: {{ configMap:configMapName:key }} # ENV from a key in configMap
// - value: {{ configMap:configMapName }} # all key-pair values from configMap are set as ENV
func ValidateEnvs(envs Tuples) (errors []string) {
func ValidateEnvs(envs Pairs) (errors []string) {

for i, env := range envs {
if env.Name == nil && env.Value == nil {
Expand Down Expand Up @@ -343,16 +343,16 @@ func ValidateEnvs(envs Tuples) (errors []string) {
// value: value1
// - name: EXAMPLE2 # label from the local ENV var
// value: {{ env:MY_ENV }}
func ValidateLabels(labels Tuples) (errors []string) {
func ValidateLabels(labels Pairs) (errors []string) {
for i, label := range labels {
if label.Name == nil && label.Value == nil {
errors = append(errors, fmt.Sprintf("label entry #%d is not properly set", i))
} else if label.Value == nil {
errors = append(errors, fmt.Sprintf("label entry #%d is missing value field, only name '%s' is set", i, *label.Name))
} else {

if err := utils.ValidateLabelName(*label.Name); err != nil {
errors = append(errors, fmt.Sprintf("label entry #%d has invalid name set: %q; %s", i, *label.Name, err.Error()))
if err := utils.ValidateLabel(*label.Name, *label.Value); err != nil {
errors = append(errors, fmt.Sprintf("label entry #%d has invalid name or value set: %q %q; %s", i, *label.Name, *label.Value, err.Error()))
}

if strings.HasPrefix(*label.Value, "{{") {
Expand Down

0 comments on commit 20bcbac

Please sign in to comment.