convert

package
v0.1.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const FilepathKey = "filename"
View Source
const FilepathShorthand = "f"

Variables

View Source
var ConvertCmd = &cobra.Command{
	Use:   "convert",
	Short: "Converts Otterize intents to Kubernetes ClientIntents resources",
	Args:  cobra.NoArgs,
	RunE: func(cmd *cobra.Command, args []string) error {
		printer := intentsoutput.IntentsPrinter{}
		allowedExts := goset.NewSet(".yaml", ".yml")
		fileInfo, err := os.Stat(viper.GetString(FilepathKey))
		if err != nil {
			return fmt.Errorf("failed to get info for path %s: %w", viper.GetString(FilepathKey), err)
		}
		filePaths := make([]string, 0)
		if fileInfo.IsDir() {
			entries, err := os.ReadDir(viper.GetString(FilepathKey))
			if err != nil {
				return fmt.Errorf("failed to read dir %s: %w", viper.GetString(FilepathKey), err)
			}
			for _, entry := range entries {
				if !allowedExts.Contains(filepath.Ext(entry.Name())) || entry.Type() != regularFile {
					continue
				}
				filePaths = append(filePaths, filepath.Join(viper.GetString(FilepathKey), entry.Name()))
			}
		} else {
			filePaths = append(filePaths, viper.GetString(FilepathKey))
		}

		for _, path := range filePaths {
			err := func() error {
				file, err := os.Open(path)
				if err != nil {
					return err
				}
				defer file.Close()
				yamlReader := k8syaml.NewYAMLReader(bufio.NewReader(file))
				for {
					doc, err := yamlReader.Read()
					if err != nil {
						if errors.Is(err, io.EOF) {
							break
						}

						return fmt.Errorf("unable to parse YAML file %s: %w", path, err)
					}

					var intentsSpec v1alpha2.IntentsSpec
					err = yaml.UnmarshalStrict(doc, &intentsSpec)
					if err != nil {
						return fmt.Errorf("unable to parse YAML file %s: %w", path, err)
					}

					resource := NewIntentsResourceFromIntentsSpec(intentsSpec)
					err = printer.PrintObj(resource, os.Stdout)
					if err != nil {
						return err
					}
				}
				return nil
			}()
			if err != nil {
				return err
			}
		}

		return nil
	},
}

Functions

func NewIntentsResourceFromIntentsSpec

func NewIntentsResourceFromIntentsSpec(spec v1alpha2.IntentsSpec) *v1alpha2.ClientIntents

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL