diff --git a/cmd/root.go b/cmd/root.go index f01fd0f..a673680 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,8 +33,6 @@ var rootCmd = &cobra.Command{ // Execute the top level command func Execute() { if err := rootCmd.Execute(); err != nil { - // fmt.Println(radixCLIError) - // fmt.Fprintln(os.Stderr, err) os.Exit(1) } } diff --git a/cmd/validate.go b/cmd/validate.go new file mode 100644 index 0000000..8b72ff3 --- /dev/null +++ b/cmd/validate.go @@ -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) +} diff --git a/cmd/validateRadixConfig.go b/cmd/validateRadixConfig.go new file mode 100644 index 0000000..c4d1493 --- /dev/null +++ b/cmd/validateRadixConfig.go @@ -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) +} diff --git a/go.mod b/go.mod index 194176f..c3f220b 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ toolchain go1.21.0 require ( github.com/AzureAD/microsoft-authentication-library-for-go v1.1.0 github.com/equinor/radix-common v1.5.0 - github.com/equinor/radix-operator v1.43.0 + github.com/equinor/radix-operator v1.44.1 github.com/fatih/color v1.15.0 github.com/go-openapi/errors v0.20.4 github.com/go-openapi/runtime v0.26.0 @@ -18,6 +18,7 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.7.0 k8s.io/utils v0.0.0-20230505201702-9f6742963106 + sigs.k8s.io/yaml v1.3.0 ) require ( @@ -90,5 +91,4 @@ require ( sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/secrets-store-csi-driver v1.3.3 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect - sigs.k8s.io/yaml v1.3.0 // indirect ) diff --git a/go.sum b/go.sum index b007a29..34d3210 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/equinor/radix-common v1.5.0 h1:z5hQHlKG2x16/NnV4b9ynf9n5ZageYUewE4MANdA96Y= github.com/equinor/radix-common v1.5.0/go.mod h1:UZ69U56VFtTxABi5JjGdaqn9Df5ilfTTqzUQ0riofVM= -github.com/equinor/radix-operator v1.43.0 h1:25vXiqy0BZrTu1VjA/kFgP7IGQ/UoFAtx0QCq0Irm8E= -github.com/equinor/radix-operator v1.43.0/go.mod h1:fa1yXgnhluKtqlBP+k/VldbkpFRpkK5eBGfGhuilqNc= +github.com/equinor/radix-operator v1.44.1 h1:ogaVIERfEbeD5QexsXXZSN6k4v4hgbtFDFHoLgBkNjA= +github.com/equinor/radix-operator v1.44.1/go.mod h1:9YBEz5p/s2YCL8FGzjvRUXgPjtrBojnjw/tAXQNzwx0= github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U= github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=