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

Add ability in generate commands to take and use package name for output #47

Merged
merged 2 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions cli/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"go/format"
"io/ioutil"
"os"
"regexp"

"github.com/docker/oscalkit/generator"
"github.com/docker/oscalkit/templates"
"github.com/docker/oscalkit/types/oscal/catalog"
"github.com/sirupsen/logrus"

"github.com/urfave/cli"
)

var profilePath string
var outputFileName string
var packageName string

//Generate Cli command to generate go code for controls
var Generate = cli.Command{
Expand All @@ -33,6 +34,12 @@ var Generate = cli.Command{
Destination: &outputFileName,
Value: "output.go",
},
cli.StringFlag{
Name: "package, pkg",
Usage: "package name for generated go file (default is oscalkit)",
Destination: &packageName,
Value: "oscalkit",
},
},
Before: func(c *cli.Context) error {
if profilePath == "" {
Expand All @@ -41,6 +48,10 @@ var Generate = cli.Command{
return nil
},
Action: func(c *cli.Context) error {
err := validatePackageName(packageName)
if err != nil {
return cli.NewExitError(err, 1)
}

profilePath, err := generator.GetAbsolutePath(profilePath)
if err != nil {
Expand Down Expand Up @@ -76,8 +87,9 @@ var Generate = cli.Command{
return cli.NewExitError("cannot fetch template", 1)
}
err = t.Execute(newFile, struct {
Catalogs []*catalog.Catalog
}{catalogs})
Catalogs []*catalog.Catalog
PackageName string
}{catalogs, packageName})

//TODO: discuss better approach for formatting generate code file.
if err != nil {
Expand All @@ -101,3 +113,12 @@ var Generate = cli.Command{

},
}

func validatePackageName(packageName string) error {
var validPackage = regexp.MustCompile(`^[a-z][a-z0-9_]*$`)
if !validPackage.Match([]byte(packageName)) {
return fmt.Errorf("invalid package name: %s", packageName)
}

return nil
}
18 changes: 16 additions & 2 deletions cli/cmd/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/docker/oscalkit/generator"
"github.com/docker/oscalkit/impl"
"github.com/docker/oscalkit/templates"
"github.com/docker/oscalkit/types/oscal/implementation"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -41,6 +42,12 @@ var Implementation = cli.Command{
Destination: &outputFileName,
Value: "implementation.go",
},
cli.StringFlag{
Name: "package, pkg",
Usage: "package name for generated go file (default is oscalkit)",
Destination: &packageName,
Value: "oscalkit",
},
},
Before: func(c *cli.Context) error {
if profile == "" {
Expand All @@ -52,6 +59,10 @@ var Implementation = cli.Command{
return nil
},
Action: func(c *cli.Context) error {
err := validatePackageName(packageName)
if err != nil {
return cli.NewExitError(err, 1)
}

profileF, err := generator.GetFilePath(profile)
if err != nil {
Expand Down Expand Up @@ -96,12 +107,15 @@ var Implementation = cli.Command{
}

catalog := impl.NISTCatalog{ID: "NIST_SP-800-53"}
implementation := impl.GenerateImplementation(records, profile, &catalog)
implementationData := impl.GenerateImplementation(records, profile, &catalog)
t, err := templates.GetImplementationTemplate()
if err != nil {
return fmt.Errorf("cannot get implementation template err %v", err)
}
err = t.Execute(outputFile, implementation)
err = t.Execute(outputFile, struct {
Implementation implementation.Implementation
PackageName string
}{implementationData, packageName})
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions templates/catalog.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions templates/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func GetImplementationTemplate() (*template.Template, error) {
return template.New("").Parse(implementationTemplate)
}

const implementationTemplate = `package oscalkit
const implementationTemplate = `package {{.PackageName}}

import (
"github.com/docker/oscalkit/types/oscal/implementation"
Expand All @@ -16,7 +16,7 @@ import (
var ImplementationGenerated = implementation.Implementation{
Capabilities: implementation.Capabilities{},
ComponentDefinitions: []implementation.ComponentDefinition{
{{range .ComponentDefinitions}}
{{range .Implementation.ComponentDefinitions}}
implementation.ComponentDefinition{
ComponentConfigurations: []*implementation.ComponentConfiguration{
{{range .ComponentConfigurations}}
Expand Down
8 changes: 4 additions & 4 deletions templates/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func GetProfileTemplate() *template.Template {
}

const profileTemplate = `
package oscalkit
package {{.PackageName}}

import (
"net/url"
Expand All @@ -25,7 +25,7 @@ import (
var ApplicableProfileControls = profile.Profile{
Imports: []profile.Import{
profile.Import{
{{range .Imports}}
{{range .Profile.Imports}}
Exclude: &profile.Exclude{
IdSelectors: []profile.Call{
},
Expand All @@ -50,11 +50,11 @@ var ApplicableProfileControls = profile.Profile{
},
},
Merge: &profile.Merge{
AsIs: profile.AsIs("{{.Merge.AsIs}}"),
AsIs: profile.AsIs("{{.Profile.Merge.AsIs}}"),
},
Modify: &profile.Modify{
Alterations: []profile.Alter{
{{range .Modify.Alterations}}
{{range .Profile.Modify.Alterations}}
profile.Alter{
ControlId: "123",
Additions: []profile.Add{
Expand Down