Unverified Commit ad1a5261 authored by shalev007's avatar shalev007 Committed by GitHub
Browse files

feat: cli add save-rendered flag (#822)


* feat: add save rendered flag to test command

* gix: move save rendered to executor for testing

* test: add flag test

* fix: linter issues

* fix: make save rendered bool

* ref: remove comment
Co-authored-by: default avatarshalev avhar <shalev@datree.io>
parent 867302e8
Showing with 19 additions and 2 deletions
+19 -2
......@@ -71,7 +71,10 @@ func New(testCtx *test.TestCommandContext, kustomizeCtx *KustomizeContext) *cobr
if err != nil {
return err
}
defer os.Remove(tempFilename)
if !testCommandFlags.SaveRendered {
defer os.Remove(tempFilename)
}
err = test.TestWrapper(testCtx, []string{tempFilename}, testCommandFlags)
if err != nil {
......
......@@ -98,6 +98,11 @@ type mockKustomizeExecuter struct {
mock.Mock
}
func (m *mockKustomizeExecuter) SaveRenderedFile(saveRenderedFlag string, content []byte) (string, error) {
args := m.Called(saveRenderedFlag, content)
return args.String(0), args.Error(1)
}
func (m *mockKustomizeExecuter) ExecuteKustomizeBin(args []string) ([]byte, error) {
_args := m.Called(args)
return _args.Get(0).([]byte), _args.Error(1)
......
......@@ -60,6 +60,7 @@ type TestCommandFlags struct {
PolicyConfig string
NoRecord bool
SkipValidation string
SaveRendered bool
}
// TestCommandFlags constructor
......@@ -73,6 +74,7 @@ func NewTestCommandFlags() *TestCommandFlags {
PolicyName: "",
SchemaLocations: make([]string, 0),
SkipValidation: "",
SaveRendered: false,
}
}
......@@ -148,6 +150,7 @@ type TestCommandData struct {
PromptRegistrationURL string
ClientId string
SkipSchemaValidation bool
SaveRendered bool
}
type TestCommandContext struct {
......@@ -261,6 +264,7 @@ func (flags *TestCommandFlags) AddFlags(cmd *cobra.Command) {
// kubeconform flag
cmd.Flags().StringArrayVarP(&flags.SchemaLocations, "schema-location", "", []string{}, "Override schemas location search path (can be specified multiple times)")
cmd.Flags().BoolVarP(&flags.IgnoreMissingSchemas, "ignore-missing-schemas", "", false, "Ignore missing schemas when executing schema validation step")
cmd.Flags().BoolVarP(&flags.SaveRendered, "save-rendered", "", false, "Save The rendered file. needs to be a path to a directory")
}
func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigContent *localConfig.LocalConfig, evaluationPrerunDataResp *cliClient.EvaluationPrerunDataResponse) (*TestCommandData, error) {
......@@ -315,6 +319,7 @@ func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigCont
RegistrationURL: evaluationPrerunDataResp.RegistrationURL,
PromptRegistrationURL: evaluationPrerunDataResp.PromptRegistrationURL,
SkipSchemaValidation: testCommandFlags.SkipValidation == "schema",
SaveRendered: testCommandFlags.SaveRendered,
}
return testCommandOptions, nil
......@@ -361,7 +366,10 @@ func test(ctx *TestCommandContext, paths []string, testCommandData *TestCommandD
if err != nil {
return err
}
defer os.Remove(tempFile.Name())
if !testCommandData.SaveRendered {
defer os.Remove(tempFile.Name())
}
if _, err := io.Copy(tempFile, os.Stdin); err != nil {
return err
......
......@@ -678,6 +678,7 @@ func setup() {
k8sValidatorMock = &K8sValidatorMock{}
path := "valid/path"
filesConfigurationsChan := newFilesConfigurationsChan(path)
filesConfigurations = newFilesConfigurations(path)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment