-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from apoorvajagtap/issue#224
Adds flag to add paramValues
- Loading branch information
Showing
9 changed files
with
128 additions
and
0 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
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,50 @@ | ||
package flags | ||
|
||
import ( | ||
"fmt" | ||
|
||
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" | ||
) | ||
|
||
// ParamArrayValue implements pflag.Value interface, in order to store ParamValue key-value | ||
// pairs used on Shipwright's BuildSpec. | ||
type ParamArrayValue struct { | ||
params *[]buildv1alpha1.ParamValue // pointer to the slice of ParamValue | ||
} | ||
|
||
// String prints out the string representation of the slice of EnvVar objects. | ||
func (p *ParamArrayValue) String() string { | ||
slice := []string{} | ||
for _, e := range *p.params { | ||
slice = append(slice, fmt.Sprintf("%s=%v", e.Name, e.Value)) | ||
} | ||
csv, _ := writeAsCSV(slice) | ||
return fmt.Sprintf("[%s]", csv) | ||
} | ||
|
||
// Set receives a key-value entry separated by equal sign ("="). | ||
func (p *ParamArrayValue) Set(value string) error { | ||
k, v, err := splitKeyValue(value) | ||
if err != nil { | ||
return err | ||
} | ||
for _, e := range *p.params { | ||
if k == e.Name { | ||
return fmt.Errorf("environment variable '%s' is already set", k) | ||
} | ||
} | ||
*p.params = append(*p.params, buildv1alpha1.ParamValue{Name: k, SingleValue: &buildv1alpha1.SingleValue{Value: &v}}) | ||
return nil | ||
} | ||
|
||
// Type analogous to the pflag "stringArray" type, where each flag entry will be tranlated to a | ||
// single array (slice) entry, therefore the comma (",") is accepted as part of the value, as any | ||
// other special character. | ||
func (p *ParamArrayValue) Type() string { | ||
return "stringArray" | ||
} | ||
|
||
// NewCoreEnvVarArrayValue instantiate a ParamValSliceValue sharing the EnvVar pointer. | ||
func NewParamArrayValue(params *[]buildv1alpha1.ParamValue) *ParamArrayValue { | ||
return &ParamArrayValue{params: params} | ||
} |
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,60 @@ | ||
package flags | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" | ||
) | ||
|
||
func TestNewParamArrayValue(t *testing.T) { | ||
g := gomega.NewWithT(t) | ||
|
||
testCases := map[string]struct { | ||
paramPassed string | ||
paramKey string | ||
paramValue string | ||
expectedErr error | ||
}{ | ||
"simpleKeyValPair": { | ||
paramPassed: "dockerfile=Dockerfile", | ||
paramKey: "dockerfile", | ||
paramValue: "Dockerfile", | ||
expectedErr: nil, | ||
}, | ||
"specialCharKeyValPair": { | ||
paramPassed: "b=cd=e", | ||
paramKey: "b", | ||
paramValue: "cd=e", | ||
expectedErr: nil, | ||
}, | ||
"noEquals": { | ||
paramPassed: "bc", | ||
expectedErr: errors.New("informed value 'bc' is not in key=value format"), | ||
}, | ||
"withSpaceVal": { | ||
paramPassed: "b=c d", | ||
paramKey: "b", | ||
paramValue: "c d", | ||
expectedErr: nil, | ||
}, | ||
} | ||
for tName, tCase := range testCases { | ||
t.Run(tName, func(_ *testing.T) { | ||
buildSpec := buildv1alpha1.BuildSpec{} | ||
buildParamVal := NewParamArrayValue(&buildSpec.ParamValues) | ||
|
||
err := buildParamVal.Set(tCase.paramPassed) | ||
if tCase.expectedErr != nil { | ||
g.Expect(err).To(gomega.Equal(tCase.expectedErr)) | ||
return | ||
} | ||
g.Expect(err).To(gomega.BeNil()) | ||
|
||
paramVal := tCase.paramValue | ||
g.Expect(buildSpec.ParamValues[0].Name).To(gomega.Equal(tCase.paramKey)) | ||
g.Expect(buildSpec.ParamValues[0].Value).To(gomega.Equal(¶mVal)) | ||
}) | ||
} | ||
} |