Skip to content

Commit

Permalink
bob inspect: add buildinfo diff (#317)
Browse files Browse the repository at this point in the history
* add buildinfo diff
* update flake
  • Loading branch information
Equanox authored Mar 31, 2023
1 parent 80f1b1e commit 8ec09da
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
30 changes: 30 additions & 0 deletions bobtask/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package buildinfo

import (
"bytes"
"fmt"
"sort"

"github.com/benchkram/bob/bobtask/buildinfo/protos"
)

Expand All @@ -18,6 +22,32 @@ func New() *I {
}
}

func (i *I) Describe() string {
buf := bytes.NewBufferString("")

fmt.Fprintln(buf, "Meta:")
fmt.Fprintln(buf, "\ttask:", i.Meta.Task)
fmt.Fprintln(buf, "\tinput hash", i.Meta.InputHash)

fmt.Fprintln(buf, "Filesystem:")
fmt.Fprintln(buf, "\thash of all files", i.Target.Filesystem.Hash)
fmt.Fprintln(buf, "\t# of files", len(i.Target.Filesystem.Files))
fmt.Fprintln(buf, "\tfiles:")

sortedFiles := []string{}
for filename := range i.Target.Filesystem.Files {
sortedFiles = append(sortedFiles, filename)
}
sort.Strings(sortedFiles)

for _, filename := range sortedFiles {
v := i.Target.Filesystem.Files[filename]
fmt.Fprintln(buf, "\t", filename, v.Size)
}

return buf.String()
}

type Targets struct {
Filesystem BuildInfoFiles `yaml:"file"`
Docker map[string]BuildInfoDocker `yaml:"docker"`
Expand Down
67 changes: 67 additions & 0 deletions cli/cmd_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"errors"
"fmt"
"os"
"sort"

"github.com/benchkram/bob/bob"
"github.com/benchkram/bob/pkg/boblog"
"github.com/benchkram/bob/pkg/usererror"
"github.com/benchkram/errz"
"github.com/logrusorgru/aurora"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/spf13/cobra"
)

Expand All @@ -23,8 +25,12 @@ func init() {

inspectCmd.AddCommand(inputCmd)
inspectCmd.AddCommand(envCmd)
// artifact
inspectArtifactCmd.AddCommand(inspectArtifactListCmd)
inspectCmd.AddCommand(inspectArtifactCmd)
//diff
inspectBuildInfoCmd.AddCommand(inspectBuildInfoDiffCmd)
inspectCmd.AddCommand(inspectBuildInfoCmd)
rootCmd.AddCommand(inspectCmd)
}

Expand Down Expand Up @@ -174,3 +180,64 @@ 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(bi.Describe())
}

var inspectBuildInfoDiffCmd = &cobra.Command{
Use: "diff",
Short: "Diff build infos",
Args: cobra.ExactArgs(2),
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
diffBuildInfo(args[0], args[1])
},
}

func diffBuildInfo(hashA, hashB string) {
var exitCode int
defer func() { os.Exit(exitCode) }()

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

biA, err := bs.GetBuildInfo(hashA)
if err != nil {
panic(err)
}

biB, err := bs.GetBuildInfo(hashB)
if err != nil {
panic(err)
}

dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(biA.Describe(), biB.Describe(), false)
fmt.Println(dmp.DiffPrettyText(diffs))
}
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# got: sha256-BzlZiAXA8wQ7RU6N1knPYH/BDX1Ae+2+4pVJ41ecK7A=*/
#
# If on `nix build` you get above error, just replace the value vendorSha256 with value from `got`
vendorSha256 = "sha256-55wiqqmd0riU0V3g2OI3yFIOfFBCU5bjk8tnDpRfHXk=";
vendorSha256 = "sha256-E4jZOLi+4QMFF1EWdOUU6P/Ek5zp2vZpL4nMg9YvwSo=";

excludedPackages = [ "example/server-db" "test/e2e" "tui-example" ];

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ require (
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sanathkr/go-yaml v0.0.0-20170819195128-ed9d249f429b // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,8 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/serialx/hashring v0.0.0-20190422032157-8b2912629002/go.mod h1:/yeG0My1xr/u+HZrFQ1tOQQQQrOawfyMUH13ai5brBc=
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lzhCH5h/3YBfyVA3VbLYjlMZZAQcW9ojMexNc=
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
Expand Down

0 comments on commit 8ec09da

Please sign in to comment.