Skip to content

Commit

Permalink
Add JSON pretty printing to select scai-gen commands, mkdir -p the ou…
Browse files Browse the repository at this point in the history
…tput directory on any file write

Signed-off-by: Marcela Melara <[email protected]>
  • Loading branch information
marcelamelara committed Nov 2, 2023
1 parent 1a695b1 commit 79b5a53
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 15 deletions.
7 changes: 6 additions & 1 deletion scai-gen/cmd/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func init() {
}

func genAttrAssertion(_ *cobra.Command, args []string) error {
// want to make sure the AttributeAssertion is a JSON file
if !fileio.HasJSONExt(outFile) {
return fmt.Errorf("expected a .json extension for the generated SCAI AttributeAssertion file %s", outFile)
}

attribute := args[0]

var target *ita.ResourceDescriptor
Expand Down Expand Up @@ -101,5 +106,5 @@ func genAttrAssertion(_ *cobra.Command, args []string) error {
return fmt.Errorf("invalid SCAI attribute assertion: %w", err)
}

return fileio.WritePbToFile(aa, outFile)
return fileio.WritePbToFile(aa, outFile, false)
}
22 changes: 12 additions & 10 deletions scai-gen/cmd/rd.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,6 @@ func init() {
"The URI of the resource",
)

rdFileCmd.Flags().BoolVarP(
&withContent,
"content",
"c",
false,
"Flag to include the content of the file",
)

rdFileCmd.Flags().StringVarP(
&downloadLocation,
"download-location",
Expand Down Expand Up @@ -165,6 +157,11 @@ func readAnnotations(filename string) (*structpb.Struct, error) {
}

func genRdFromFile(_ *cobra.Command, args []string) error {
// want to make sure the ResourceDescriptor is a JSON file
if !fileio.HasJSONExt(outFile) {
return fmt.Errorf("expected a .json extension for the generated ResourceDescriptor file %s", outFile)
}

filename := args[0]
fileBytes, err := os.ReadFile(filename)
if err != nil {
Expand Down Expand Up @@ -203,10 +200,15 @@ func genRdFromFile(_ *cobra.Command, args []string) error {
return fmt.Errorf("invalid resource descriptor: %w", err)
}

return fileio.WritePbToFile(rd, outFile)
return fileio.WritePbToFile(rd, outFile, false)
}

func genRdForRemote(_ *cobra.Command, args []string) error {
// want to make sure the ResourceDescriptor is a JSON file
if !fileio.HasJSONExt(outFile) {
return fmt.Errorf("expected a .json extension for the generated ResourceDescriptor file %s", outFile)
}

remoteURI := args[0]

digestSet := make(map[string]string)
Expand Down Expand Up @@ -240,5 +242,5 @@ func genRdForRemote(_ *cobra.Command, args []string) error {
return fmt.Errorf("invalid resource descriptor: %w", err)
}

return fileio.WritePbToFile(rd, outFile)
return fileio.WritePbToFile(rd, outFile, false)
}
15 changes: 14 additions & 1 deletion scai-gen/cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,22 @@ func init() {
"",
"The filename of the JSON-encoded producer resource descriptor",
)

reportCmd.Flags().BoolVarP(
&prettyPrint,
"pretty-print",
"y",
false,
"Flag to JSON pretty-print the generated Report",
)
}

func genAttrReport(_ *cobra.Command, args []string) error {
// want to make sure the Report is a JSON file
if !fileio.HasJSONExt(outFile) {
return fmt.Errorf("expected a .json extension for the generated in-toto Statement file %s", outFile)
}

attrAsserts := make([]*scai.AttributeAssertion, 0, len(args))
for _, attrAssertPath := range args {
aa := &scai.AttributeAssertion{}
Expand Down Expand Up @@ -117,5 +130,5 @@ func genAttrReport(_ *cobra.Command, args []string) error {
return fmt.Errorf("invalid in-toto Statement: %w", err)
}

return fileio.WritePbToFile(statement, outFile)
return fileio.WritePbToFile(statement, outFile, prettyPrint)
}
5 changes: 4 additions & 1 deletion scai-gen/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ var rootCmd = &cobra.Command{
Short: "A CLI tool for generating/checking SCAI metadata",
}

var outFile string
var (
outFile string
prettyPrint bool
)

func init() {
rootCmd.AddCommand(rdCmd)
Expand Down
5 changes: 5 additions & 0 deletions scai-gen/cmd/sigstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func getNewFulcioSigner(ctx context.Context) (*fulcio.Signer, error) {
func signWithSigstore(cmd *cobra.Command, args []string) error {
fmt.Println("EXPERIMENTAL FEATURE. DO NOT USE IN PRODUCTION.")

// want to make sure the DSSE is a JSON file
if !fileio.HasJSONExt(outFile) {
return fmt.Errorf("expected a .json extension for the generated DSSE file %s", outFile)
}

statementFile := args[0]
statement := &ita.Statement{}
err := fileio.ReadPbFromFile(statementFile, statement)
Expand Down
17 changes: 17 additions & 0 deletions scai-gen/fileio/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fileio

import (
"os"
"path/filepath"
"strings"
)

func HasJSONExt(filename string) bool {
return strings.HasSuffix(filename, ".json")
}

func CreateOutDir(filename string) error {
outDir := filepath.Dir(filename)

return os.MkdirAll(outDir, 0644)
}
5 changes: 5 additions & 0 deletions scai-gen/fileio/dsse.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@ func ReadStatementFromDSSEFile(path string) (*ita.Statement, error) {
}

func WriteDSSEToFile(envBytes []byte, outFile string) error {
// ensure the out directory exists
if err := CreateOutDir(outFile); err != nil {
return fmt.Errorf("error creating output directory for file %s: %w", outFile, err)
}

return os.WriteFile(outFile, envBytes, 0644) //nolint:gosec
}
24 changes: 22 additions & 2 deletions scai-gen/fileio/pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,32 @@ import (
"google.golang.org/protobuf/proto"
)

func WritePbToFile(pb proto.Message, outFile string) error {
pbBytes, err := protojson.Marshal(pb)
func WritePbToFile(pb proto.Message, outFile string, pretty bool) error {
var (
pbBytes []byte
err error
)

if pretty {
opt := &protojson.MarshalOptions{
Multiline: true,
Indent: " ",
EmitUnpopulated: false,
}
pbBytes, err = opt.Marshal(pb)
} else {
pbBytes, err = protojson.Marshal(pb)
}

if err != nil {
return err
}

// ensure the out directory exists
if err = CreateOutDir(outFile); err != nil {
return fmt.Errorf("error creating output directory for file %s: %w", outFile, err)
}

return os.WriteFile(outFile, pbBytes, 0644) //nolint:gosec
}

Expand Down

0 comments on commit 79b5a53

Please sign in to comment.