convert

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2018 License: CC0-1.0 Imports: 12 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 from OpenControl format",
	Subcommands: []cli.Command{
		ConvertOSCAL,
		ConvertOpenControl,
	},
}

Convert ...

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:        "include-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("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() == "-" {
			rawSource, err := parseStdin(os.Stdin)
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("Error parsing from STDIN: %s", err), 1)
			}

			return convert(rawSource, "")
		}

		for _, sourcePath := range c.Args() {
			matches, _ := filepath.Glob(sourcePath)
			if len(matches) > 0 {
				for _, match := range matches {
					if err := convert(nil, match); err != nil {
						return cli.NewExitError(fmt.Sprintf("Error converting to OSCAL from file %s: %s", match, err), 1)
					}
				}
			} else {
				if err := convert(nil, sourcePath); err != nil {
					return cli.NewExitError(fmt.Sprintf("Error converting to OSCAL from file %s: %s", sourcePath, err), 1)
				}
			}
		}

		return nil
	},
}

ConvertOSCAL ...

View Source
var ConvertOpenControl = cli.Command{
	Name:  "opencontrol",
	Usage: `convert from OpenControl format to OSCAL "implementation" format`,
	Description: `Convert OpenControl-formatted "component" and "opencontrol" YAML into
	 OSCAL-formatted "implementation" layer JSON`,
	ArgsUsage: "[opencontrol.yaml-filepath] [opencontrols-dir-path]",
	Flags: []cli.Flag{
		cli.BoolFlag{
			Name:        "yaml, y",
			Usage:       "Generate YAML in addition to JSON",
			Destination: &yaml,
		},
		cli.BoolFlag{
			Name:        "xml, x",
			Usage:       "Generate XML in addition to JSON",
			Destination: &includeXML,
		},
	},
	Before: func(c *cli.Context) error {
		if c.NArg() != 2 {
			return cli.NewExitError("Missing opencontrol.yaml file and path to opencontrols/ directory", 1)
		}

		return nil
	},
	Action: func(c *cli.Context) error {
		ocOSCAL, err := oscal.NewFromOC(oscal.OpenControlOptions{
			OpenControlYAMLFilepath: c.Args().First(),
			OpenControlsDir:         c.Args()[1],
		})
		if err != nil {
			return cli.NewExitError(err, 1)
		}

		if includeXML {
			rawXMLOCOSCAL, err := ocOSCAL.RawXML(true)
			if err != nil {
				return cli.NewExitError(fmt.Sprintf("Error producing raw XML: %s", err), 1)
			}
			if err := ioutil.WriteFile("opencontrol-oscal.xml", rawXMLOCOSCAL, 0644); err != nil {
				return cli.NewExitError(err, 1)
			}
		}

		if yaml {
			rawYAMLOCOSCAL, err := ocOSCAL.RawYAML()
			if err != nil {
				return cli.NewExitError(err, 1)
			}
			if err := ioutil.WriteFile("opencontrol-oscal.yaml", rawYAMLOCOSCAL, 0644); err != nil {
				return cli.NewExitError(err, 1)
			}
		}

		rawOCOSCAL, err := ocOSCAL.RawJSON(true)
		if err != nil {
			return cli.NewExitError(err, 1)
		}

		if err := ioutil.WriteFile("opencontrol-oscal.json", rawOCOSCAL, 0644); err != nil {
			return cli.NewExitError(err, 1)
		}

		return nil
	},
}

ConvertOpenControl ...

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