Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
Add sub commands for generate cmd (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
minhaj10p authored and anweiss committed Jan 23, 2019
1 parent 6d17962 commit 2f8e944
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os"

"github.com/docker/oscalkit/cli/cmd/convert"
"github.com/docker/oscalkit/cli/cmd/generate"
"github.com/docker/oscalkit/cli/version"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
Expand Down Expand Up @@ -49,8 +50,7 @@ func Execute() error {
convert.Convert,
Validate,
Sign,
Generate,
Implementation,
generate.Generate,
}

return app.Run(os.Args)
Expand Down
92 changes: 92 additions & 0 deletions cli/cmd/generate/catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package generate

import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"os"

"github.com/sirupsen/logrus"

"github.com/docker/oscalkit/generator"
"github.com/urfave/cli"
)

var isJSON bool

// Catalog generates json/xml catalogs
var Catalog = cli.Command{
Name: "catalogs",
Usage: "generates json/xml catalogs provided profile",
Flags: []cli.Flag{
cli.StringFlag{
Name: "profile, p",
Usage: "profile to intersect against",
Destination: &profilePath,
},
cli.StringFlag{
Name: "output, o",
Usage: "output filename",
Destination: &outputFileName,
Value: "output",
},
cli.BoolFlag{

Name: "json, j",
Usage: "flag for generating catalogs in json",
Destination: &isJSON,
},
},
Before: func(c *cli.Context) error {
if profilePath == "" {
return cli.NewExitError("oscalkit generate is missing the --profile flag", 1)
}
return nil
},
Action: func(c *cli.Context) error {

profilePath, err := generator.GetAbsolutePath(profilePath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("cannot get absolute path, err: %v", err), 1)
}

_, err = os.Stat(profilePath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("cannot fetch file, err %v", err), 1)
}
f, err := os.Open(profilePath)
if err != nil {
return cli.NewExitError(err, 1)
}
defer f.Close()

profile, err := generator.ReadProfile(f)
if err != nil {
return cli.NewExitError(err, 1)
}
catalogs, err := generator.CreateCatalogsFromProfile(profile)
if err != nil {
return cli.NewExitError(fmt.Sprintf("cannot create catalogs from profile, err: %v", err), 1)
}

var bytes []byte
if !isJSON {
bytes, err = xml.MarshalIndent(catalogs, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(outputFileName+".xml", bytes, 0644)
}
bytes, err = json.MarshalIndent(catalogs, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(outputFileName+".json", bytes, 0644)

},
After: func(c *cli.Context) error {
logrus.Info("catalog file generated")
return nil
},
}
8 changes: 4 additions & 4 deletions cli/cmd/generate.go → cli/cmd/generate/code.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package generate

import (
"fmt"
Expand All @@ -18,9 +18,9 @@ var profilePath string
var outputFileName string
var packageName string

//Generate Cli command to generate go code for controls
var Generate = cli.Command{
Name: "generate",
// Code Cli command to generate go code for controls in catalog
var Code = cli.Command{
Name: "code",
Usage: "generates go code against provided profile",
Flags: []cli.Flag{
cli.StringFlag{
Expand Down
16 changes: 16 additions & 0 deletions cli/cmd/generate/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package generate

import (
"github.com/urfave/cli"
)

// Generate Cli command to generate go code for controls
var Generate = cli.Command{
Name: "generate",
Usage: "generates catalogs code/xml/json against provided profile",
Subcommands: []cli.Command{
Catalog,
Code,
Implementation,
},
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package generate

import (
"bytes"
Expand Down

0 comments on commit 2f8e944

Please sign in to comment.