Skip to content

Commit

Permalink
Fixes after review
Browse files Browse the repository at this point in the history
Signed-off-by: Razieh Behjati <razieh@google.com>
  • Loading branch information
rbehjati committed Jan 10, 2023
1 parent e0fe32f commit 80dc390
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions internal/builders/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ go run *.go dry-run \
--build-config-path testdata/config.toml \
--build-definition-path bd.json \
--builder-image bash@sha256:9e2ba52487d \
--git-commit-hash sha1:9b5f98310dbbad675834474fa68c37d880687cb9 \
--git-commit-digest sha1:9b5f98310dbbad675834474fa68c37d880687cb9 \
--source-repo git+https://github.com/project-oak/transparent-release
```

Expand All @@ -38,7 +38,7 @@ The following is an example:
go run *.go build \
--build-config-path internal/builders/docker/testdata/config.toml \
--builder-image bash@sha256:9e2ba52487d945504d250de186cb4fe2e3ba023ed2921dd6ac8b97ed43e76af9 \
--git-commit-hash sha1:cf5804b5c6f1a4b2a0b03401a487dfdfbe3a5f00 \
--git-commit-digest sha1:cf5804b5c6f1a4b2a0b03401a487dfdfbe3a5f00 \
--source-repo git+https://github.com/slsa-framework/slsa-github-generator \
--subjects-path subjects.json \
--force-checkout
Expand Down
40 changes: 20 additions & 20 deletions internal/builders/docker/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ package main
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"

"github.com/slsa-framework/slsa-github-generator/internal/builders/docker/pkg"
"github.com/spf13/cobra"

"github.com/slsa-framework/slsa-github-generator/internal/builders/docker/pkg"
"github.com/slsa-framework/slsa-github-generator/internal/utils"
)

// DryRunCmd returns a new *cobra.Command that validates the input flags, and
Expand All @@ -37,14 +39,14 @@ func DryRunCmd(check func(error)) *cobra.Command {
Use: "dry-run [FLAGS]",
Short: "Generates and stores a JSON-formatted BuildDefinition based on the input arguments.",
Run: func(cmd *cobra.Command, args []string) {
outPath, err := filepath.Abs(buildDefinitionPath)
w, err := utils.CreateNewFileUnderCurrentDirectory(buildDefinitionPath, os.O_WRONLY)
check(err)

config, err := pkg.NewDockerBuildConfig(io)
check(err)

bd := pkg.CreateBuildDefinition(config)
check(writeToFile(*bd, outPath))
check(writeToFile(*bd, w))
},
}

Expand All @@ -56,18 +58,6 @@ func DryRunCmd(check func(error)) *cobra.Command {
return cmd
}

func writeToFile[T any](obj T, path string) error {
bytes, err := json.Marshal(obj)
if err != nil {
return fmt.Errorf("marshaling the object failed: %v", err)
}

if err := os.WriteFile(path, bytes, 0o600); err != nil {
return fmt.Errorf("writing to file failed: %v", err)
}
return nil
}

// BuildCmd returns a new *cobra.Command that builds the artifacts using the
// input flags, and prints out their digests, or terminates with an error.
func BuildCmd(check func(error)) *cobra.Command {
Expand All @@ -79,9 +69,7 @@ func BuildCmd(check func(error)) *cobra.Command {
Use: "build [FLAGS]",
Short: "Builds the artifacts using the build config, source repo, and the builder image.",
Run: func(cmd *cobra.Command, args []string) {
// The BuildArtifacts function changes directory. Here we convert
// the output path `subjectsPath` to an absolute path.
outPath, err := filepath.Abs(subjectsPath)
w, err := utils.CreateNewFileUnderCurrentDirectory(subjectsPath, os.O_WRONLY)
check(err)
config, err := pkg.NewDockerBuildConfig(io)
check(err)
Expand All @@ -96,7 +84,7 @@ func BuildCmd(check func(error)) *cobra.Command {

artifacts, err := db.BuildArtifacts()
check(err)
check(writeToFile(artifacts, outPath))
check(writeToFile(artifacts, w))
},
}

Expand All @@ -108,3 +96,15 @@ func BuildCmd(check func(error)) *cobra.Command {

return cmd
}

func writeToFile[T any](obj T, w io.Writer) error {
bytes, err := json.Marshal(obj)
if err != nil {
return fmt.Errorf("marshaling the object failed: %w", err)
}

if _, err := w.Write(bytes); err != nil {
return fmt.Errorf("writing to file failed: %w", err)
}
return nil
}

0 comments on commit 80dc390

Please sign in to comment.