convert

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2020 License: CC0-1.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Convert = cli.Command{
	Name:  "convert",
	Usage: "convert between one or more OSCAL file formats and to HTML format",
	Subcommands: []cli.Command{
		ConvertOSCAL,
		ConvertHTML,
		ConvertOpenControl,
	},
}

Convert ...

View Source
var ConvertHTML = cli.Command{
	Name:        "html",
	Usage:       "convert OSCAL file to human readable HTML",
	Description: `The command accepts source file and generates HTML representation of given file`,
	ArgsUsage:   "[source-file]",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:        "output-path, o",
			Usage:       "Output path for converted file(s). Defaults to current working directory",
			Destination: &outputPath,
		},
	},
	Before: func(c *cli.Context) error {
		if c.NArg() != 1 {

			stat, _ := os.Stdin.Stat()
			if (stat.Mode() & os.ModeCharDevice) == 0 {
				return nil
			}

			return cli.NewExitError("gocomply_oscalkit convert html requires at one argument", 1)
		}

		return nil
	},
	Action: func(c *cli.Context) error {
		for _, sourcePath := range c.Args() {
			source, err := oscal_source.Open(sourcePath)
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("could not load input file: %s", err), 1)
			}
			defer source.Close()

			buffer, err := source.HTML()
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("could convert to HTML: %s", err), 1)
			}
			if outputPath == "" {
				fmt.Println(buffer.String())
				return nil
			}

			f, err := os.Create(outputPath)
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("could write to file: %s", err), 1)
			}
			_, err = f.WriteString(buffer.String())
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("could write to file: %s", err), 1)
			}
			err = f.Close()
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("failed to close file: %s", err), 1)
			}
		}
		return nil
	},
}

ConvertHTML ...

View Source
var ConvertOSCAL = cli.Command{
	Name:  "oscal",
	Usage: "convert between one or more OSCAL file formats",
	Description: `Convert between OSCAL-formatted XML and JSON files. The command accepts
   one or more source file paths and can also be used with source file contents
	 piped/redirected from STDIN.`,
	ArgsUsage: "[source-files...]",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:        "output-path, o",
			Usage:       "Output path for converted file(s). Defaults to current working directory",
			Destination: &outputPath,
		},
		cli.StringFlag{
			Name:        "output-file, f",
			Usage:       `File name for converted output from STDIN. Defaults to "stdin.<json|xml|yaml>"`,
			Destination: &outputFile,
		},
		cli.BoolFlag{
			Name:        "yaml",
			Usage:       "If source file format is XML or JSON, also generate equivalent YAML output",
			Destination: &yaml,
		},
	},
	Before: func(c *cli.Context) error {
		if c.NArg() < 1 {

			stat, _ := os.Stdin.Stat()
			if (stat.Mode() & os.ModeCharDevice) == 0 {
				return nil
			}

			return cli.NewExitError("gocomply_oscalkit convert requires at least one argument", 1)
		}

		if c.NArg() > 1 {
			for _, arg := range c.Args() {

				if arg == "-" {
					return cli.NewExitError("Cannot use both file path and '-' (STDIN) in args", 1)
				}
			}
		}

		if c.Args().First() != "-" && outputFile != "" {
			return cli.NewExitError("--output-file (-f) is only used when converting from STDIN (-)", 1)
		}

		return nil
	},
	Action: func(c *cli.Context) error {

		if c.NArg() <= 0 || c.Args().First() == "-" {
			outputFormat, err := validateStdin(os.Stdin)
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("Error parsing from STDIN: %s", err), 1)
			}

			destFile, err := os.Create(outputFile)
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("Error opening output file %s: %s", outputFile, err), 1)
			}
			defer destFile.Close()

			return convert(os.Stdin, destFile, outputFormat)
		}

		for _, sourcePath := range c.Args() {

			matches, _ := filepath.Glob(sourcePath)

			for _, match := range matches {
				srcFile, err := os.Open(match)
				if err != nil {
					return err
				}
				defer srcFile.Close()

				destPath, outputFormat := createOutputPath(sourcePath)

				destFile, err := os.Create(destPath)
				if err != nil {
					return err
				}
				defer destFile.Close()

				if err := convert(srcFile, destFile, outputFormat); err != nil {
					return cli.NewExitError(fmt.Sprintf("Error converting to OSCAL from file %s: %s", match, err), 1)
				}
			}
		}

		return nil
	},
}

ConvertOSCAL ...

View Source
var ConvertOpenControl = cli.Command{
	Name:        "opencontrol",
	Usage:       "convert OSCAL Catalog file to OpenControl Standard file",
	Description: `Convert OSCAL-formatted Catalog file to OpenControl-formatted standard file.`,
	ArgsUsage:   "[input-catalog-file]",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:        "output-file, o",
			Usage:       "Output path for converted file",
			Destination: &outputPath,
		},
	},
	Before: func(c *cli.Context) error {
		if c.NArg() != 1 {
			return cli.NewExitError("gocomply_oscalkit convert opencontrol requires exactly one argument", 1)
		}

		if outputPath == "" {
			return cli.NewExitError("Please provide -o output destination", 1)
		}

		return nil
	},
	Action: func(c *cli.Context) error {
		for _, catalogPath := range c.Args() {
			source, err := oscal_source.Open(catalogPath)
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("Could not parse input file: %s", err), 1)
			}
			o := source.OSCAL()
			if o.DocumentType() != constants.CatalogDocument {
				return cli.NewExitError(fmt.Sprintf("Unexpected OSCAL %s found, expected Catalog document", o.DocumentType().String()), 1)
			}
			standard, err := opencontrol.NewStandard(o.Catalog)
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("Cannot convert to opencontrol standard: %v", err), 1)
			}
			return standard.SaveToFile(outputPath)
		}
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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