-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create validate command * make sure all output goes to stdout, except printing yaml * create a validate subcommand --------- Co-authored-by: Richard Hagen <[email protected]> Co-authored-by: Nils Gustav Stråbø <[email protected]>
- Loading branch information
1 parent
55ddb01
commit 3ec779b
Showing
5 changed files
with
128 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright © 2023 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// validateCmd represents the validate command | ||
var validateCmd = &cobra.Command{ | ||
Use: "validate", | ||
Short: "Validate Radix resources", | ||
Long: `Validate Radix resources.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return errors.New("please specify the resource you want to validate") | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(validateCmd) | ||
} |
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,89 @@ | ||
// Copyright © 2023 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1" | ||
"github.com/equinor/radix-operator/pkg/apis/radixvalidators" | ||
"github.com/equinor/radix-operator/pkg/apis/utils" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// logoutCmd represents the logout command | ||
var validateRadixConfigCmd = &cobra.Command{ | ||
Use: "radix-config", | ||
Short: "Validate radixconfig.yaml", | ||
Long: `Check radixconfig.yaml for structural and logical errors`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
cmd.SilenceUsage = true | ||
|
||
radixconfig, err := cmd.Flags().GetString("config-file") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
printfile, err := cmd.Flags().GetBool("print") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := os.Stat(radixconfig); errors.Is(err, os.ErrNotExist) { | ||
return fmt.Errorf("RadixConfig file not found: %s", radixconfig) | ||
} | ||
|
||
ra, err := utils.GetRadixApplicationFromFile(radixconfig) | ||
if err != nil { | ||
return fmt.Errorf("RadixConfig is invalid: %w", err) | ||
} | ||
|
||
if printfile { | ||
err = printRA(ra) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
err = radixvalidators.IsRadixApplicationValid(ra) | ||
if err != nil { | ||
return fmt.Errorf("RadixConfig is invalid:\n%w", err) | ||
} | ||
|
||
fmt.Fprintln(os.Stderr, "RadixConfig is valid") | ||
return nil | ||
}, | ||
} | ||
|
||
func printRA(ra *radixv1.RadixApplication) error { | ||
b, err := yaml.Marshal(ra) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(os.Stdout, "%s", b) | ||
return nil | ||
} | ||
|
||
func init() { | ||
validateCmd.AddCommand(validateRadixConfigCmd) | ||
validateRadixConfigCmd.Flags().StringP("config-file", "f", "radixconfig.yaml", "Name of the radixconfig file. Defaults to radixconfig.yaml in current directory") | ||
validateRadixConfigCmd.Flags().BoolP("print", "p", false, "Print parsed config file") | ||
setVerbosePersistentFlag(validateRadixConfigCmd) | ||
} |
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