Skip to content

Commit

Permalink
add inspect buildinfo cmd; add content file hash to buildinfo file
Browse files Browse the repository at this point in the history
  • Loading branch information
zuzuleinen committed Dec 9, 2022
1 parent acd12ff commit d9d9ffc
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 20 deletions.
7 changes: 4 additions & 3 deletions bobtask/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func MakeBuildInfoFiles() BuildInfoFiles {
type BuildInfoFile struct {
// Size of a file
Size int64 `yaml:"size"`
// Hash of file contents
Hash string `yaml:"hash"`
}

type BuildInfoDocker struct {
Expand All @@ -69,13 +71,12 @@ type Meta struct {
}

func (i *I) ToProto(inputHash string) *protos.BuildInfo {

filesystem := &protos.BuildInfoFiles{
Targets: make(map[string]*protos.BuildInfoFile, len(i.Target.Filesystem.Files)),
}
filesystem.Hash = i.Target.Filesystem.Hash
for k, v := range i.Target.Filesystem.Files {
filesystem.Targets[k] = &protos.BuildInfoFile{Size: v.Size}
filesystem.Targets[k] = &protos.BuildInfoFile{Size: v.Size, Hash: v.Hash}
}

docker := make(map[string]*protos.BuildInfoDocker)
Expand Down Expand Up @@ -111,7 +112,7 @@ func FromProto(p *protos.BuildInfo) *I {
if p.Target.Filesystem != nil {
bi.Target.Filesystem.Hash = p.Target.Filesystem.Hash
for k, v := range p.Target.Filesystem.Targets {
bi.Target.Filesystem.Files[k] = BuildInfoFile{Size: v.Size}
bi.Target.Filesystem.Files[k] = BuildInfoFile{Size: v.Size, Hash: v.Hash}
}
}

Expand Down
27 changes: 18 additions & 9 deletions bobtask/buildinfo/protos/buildinfo.pb.go

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

17 changes: 13 additions & 4 deletions bobtask/target/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func (t *T) BuildInfo() (bi *buildinfo.Targets, err error) {
}

func (t *T) buildinfoFiles(paths []string) (bi buildinfo.BuildInfoFiles, _ error) {

bi = *buildinfo.NewBuildInfoFiles()

h := filehash.New()
Expand All @@ -62,7 +61,7 @@ func (t *T) buildinfoFiles(paths []string) (bi buildinfo.BuildInfoFiles, _ error
return nil
}

err = h.AddFile(p)
err = h.AddFile(p) //
if err != nil {
return fmt.Errorf("failed to hash target %q: %w", f, err)
}
Expand All @@ -71,7 +70,13 @@ func (t *T) buildinfoFiles(paths []string) (bi buildinfo.BuildInfoFiles, _ error
if err != nil {
return fmt.Errorf("failed to get file info %q: %w", p, err)
}
bi.Files[p] = buildinfo.BuildInfoFile{Size: info.Size()}

contentHash, err := filehash.HashOfFile(p)
if err != nil {
return fmt.Errorf("failed to get file hash %q: %w", p, err)
}

bi.Files[p] = buildinfo.BuildInfoFile{Size: info.Size(), Hash: contentHash}

return nil
}); err != nil {
Expand All @@ -83,7 +88,11 @@ func (t *T) buildinfoFiles(paths []string) (bi buildinfo.BuildInfoFiles, _ error
if err != nil {
return buildinfo.BuildInfoFiles{}, fmt.Errorf("failed to hash target %q: %w", path, err)
}
bi.Files[path] = buildinfo.BuildInfoFile{Size: targetInfo.Size()}
contentHash, err := filehash.HashOfFile(path)
if err != nil {
return buildinfo.BuildInfoFiles{}, fmt.Errorf("failed to get file hash %q: %w", path, err)
}
bi.Files[path] = buildinfo.BuildInfoFile{Size: targetInfo.Size(), Hash: contentHash}
}
}

Expand Down
6 changes: 3 additions & 3 deletions bobtask/target/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (t *T) VerifyShallow() bool {
return t.verifyFilesystemShallow() && t.verifyDocker()
}

// Verify existence and integrity of targets against a expected buildinfo.
// In case the expect buildinfo does not exist Verify checks against filesystemEntriesRaw.
// Verify existence and integrity of targets against an expected buildinfo.
// In case the expected buildinfo does not exist Verify checks against filesystemEntriesRaw.
//
// Verify returns true when no targets are defined.
// Verify returns when there is nothing to compare against.
Expand All @@ -38,7 +38,7 @@ func (t *T) preConditionsFilesystem() bool {
// In case there was no previous local build
// verify returns false indicating that there can't
// exist a valid target from a previous build.
// Loading from the cash must be handled by the calling function.
// Loading from the cache must be handled by the calling function.
if t.expected == nil {
return false
}
Expand Down
3 changes: 2 additions & 1 deletion buildinfo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ message Meta {
}

message Targets {
BuildInfoFiles Filesystem = 1;
BuildInfoFiles Filesystem = 1;
map<string, BuildInfoDocker> Docker = 2;
}

Expand All @@ -25,6 +25,7 @@ message BuildInfoFiles {

message BuildInfoFile {
int64 Size = 1;
string Hash = 2;
}

message BuildInfoDocker {
Expand Down
38 changes: 38 additions & 0 deletions cli/cmd_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"sort"

"github.com/benchkram/bob/bob"
Expand All @@ -25,6 +26,7 @@ func init() {
inspectCmd.AddCommand(envCmd)
inspectArtifactCmd.AddCommand(inspectArtifactListCmd)
inspectCmd.AddCommand(inspectArtifactCmd)
inspectCmd.AddCommand(inspectBuildInfoCmd)
rootCmd.AddCommand(inspectCmd)
}

Expand Down Expand Up @@ -174,3 +176,39 @@ func runInspectInputs(taskname string) {

fmt.Printf("Task %s has %d inputs\n", taskname, len(inputs))
}

var inspectBuildInfoCmd = &cobra.Command{
Use: "buildinfo",
Short: "Inspect build info",
Args: cobra.ExactArgs(1),
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
inspectBuildInfo(args[0])
},
}

func inspectBuildInfo(hash string) {
var exitCode int
defer func() { os.Exit(exitCode) }()

bs, err := bob.DefaultBuildinfoStore()
if err != nil {
panic(err)
}

bi, err := bs.GetBuildInfo(hash)
if err != nil {
panic(err)
}

fmt.Println("Meta:")
fmt.Println("\ttask:", bi.Meta.Task)
fmt.Println("\tinput hash", bi.Meta.InputHash)

fmt.Println("Filesystem:")
fmt.Println("\thash of all files", bi.Target.Filesystem.Hash)
fmt.Println("\tfiles:")
for k, v := range bi.Target.Filesystem.Files {
fmt.Println("\t", k, v.Size, v.Hash)
}
}
11 changes: 11 additions & 0 deletions pkg/filehash/filehash.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filehash

import (
"encoding/hex"
"fmt"
"hash"
"io"
Expand Down Expand Up @@ -41,3 +42,13 @@ func (h *H) AddBytes(r io.Reader) error {
func (h *H) Sum() []byte {
return h.hash.Sum(nil)
}

// HashOfFile gives hash of a file content
func HashOfFile(path string) (string, error) {
h := New()
err := h.AddFile(path)
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum()), nil
}

0 comments on commit d9d9ffc

Please sign in to comment.