generated from MacroPower/go_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "kcl chart set" wrapper for KCL automation
- Loading branch information
1 parent
0aae8d9
commit 3cdc725
Showing
4 changed files
with
173 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package helmutil | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"kcl-lang.io/kcl-go" | ||
|
||
"github.com/MacroPower/kclipper/pkg/helmmodels" | ||
) | ||
|
||
func (c *ChartPkg) Set(chart string, keyValueOverrides string) error { | ||
if chart == "" { | ||
return errors.New("chart name cannot be empty") | ||
} | ||
|
||
hc := helmmodels.Chart{ | ||
ChartBase: helmmodels.ChartBase{ | ||
Chart: chart, | ||
}, | ||
} | ||
|
||
key, value, found := strings.Cut(keyValueOverrides, "=") | ||
if !found { | ||
return fmt.Errorf("no key=value pair found in '%s'", keyValueOverrides) | ||
} | ||
|
||
configValue := reflect.ValueOf(&helmmodels.ChartConfig{}).Elem().FieldByNameFunc(func(fieldName string) bool { | ||
return strings.EqualFold(fieldName, key) | ||
}) | ||
|
||
if !configValue.CanSet() { | ||
return fmt.Errorf("key '%s' is not a valid chart configuration attribute", key) | ||
} | ||
|
||
chartConfig := map[string]string{key: value} | ||
if err := c.updateChartsFile(c.BasePath, hc.GetSnakeCaseName(), chartConfig); err != nil { | ||
return err | ||
} | ||
|
||
_, err := kcl.FormatPath(c.BasePath) | ||
if err != nil { | ||
return fmt.Errorf("failed to format kcl files: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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,70 @@ | ||
package helmutil_test | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/MacroPower/kclipper/pkg/helmutil" | ||
) | ||
|
||
func TestChartPkg_Set(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Setup test data | ||
basePath := "testdata/got/set" | ||
chartPath := path.Join(basePath, "charts") | ||
_ = os.RemoveAll(chartPath) | ||
err := os.MkdirAll(chartPath, 0o755) | ||
require.NoError(t, err) | ||
|
||
ca := helmutil.NewChartPkg(chartPath, nil) | ||
|
||
tests := map[string]struct { | ||
chart string | ||
keyValueOverrides string | ||
expectedError error | ||
}{ | ||
"empty chart name": { | ||
chart: "", | ||
keyValueOverrides: "key=value", | ||
expectedError: errors.New("chart name cannot be empty"), | ||
}, | ||
"invalid key-value pair": { | ||
chart: "test-chart", | ||
keyValueOverrides: "invalidpair", | ||
expectedError: errors.New("no key=value pair found in 'invalidpair'"), | ||
}, | ||
"invalid chart configuration attribute": { | ||
chart: "test-chart", | ||
keyValueOverrides: "InvalidKey=value", | ||
expectedError: errors.New("key 'InvalidKey' is not a valid chart configuration attribute"), | ||
}, | ||
"successful set": { | ||
chart: "test-chart", | ||
keyValueOverrides: "schemaPath=https://example.com", | ||
expectedError: nil, | ||
}, | ||
"successful base set": { | ||
chart: "test-chart", | ||
keyValueOverrides: "repoURL=https://example.com", | ||
expectedError: nil, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := ca.Set(tc.chart, tc.keyValueOverrides) | ||
if tc.expectedError != nil { | ||
require.EqualError(t, err, tc.expectedError.Error()) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |