Skip to content

Commit

Permalink
build info and commit sub command (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
samyfodil authored Jul 14, 2024
1 parent 92752b7 commit c624443
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func newApp() *cli.App {
},
},
Commands: []*cli.Command{
buildInfoCommand(),
startCommand(),
configCommand(),
},
Expand Down
78 changes: 78 additions & 0 deletions cli/app/buildinfo_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package app

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"runtime/debug"

"github.com/urfave/cli/v2"
)

var (
debugInfo = debug.ReadBuildInfo
buildInfoOutput io.Writer = os.Stdout
)

func buildInfoCommand() *cli.Command {
return &cli.Command{
Name: "info",
Description: "build information",
Subcommands: []*cli.Command{
{
Name: "build",
Description: "show detailed build information",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "json",
},
&cli.BoolFlag{
Name: "deps",
},
},
Action: func(ctx *cli.Context) error {
info, ok := debugInfo()
if !ok {
return errors.New("no build information found")
}

if !ctx.Bool("deps") {
info.Deps = nil
}

if ctx.Bool("json") {
jenc := json.NewEncoder(buildInfoOutput)
if err := jenc.Encode(info); err != nil {
return err
}
} else {
fmt.Fprintln(buildInfoOutput, info)
}

return nil
},
},
{
Name: "commit",
Description: "show commit",
Action: func(ctx *cli.Context) error {
info, ok := debugInfo()
if !ok {
return errors.New("no build information found")
}

for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
fmt.Fprintln(buildInfoOutput, setting.Value)
return nil
}
}

return errors.New("no commit information found")
},
},
},
}
}
173 changes: 173 additions & 0 deletions cli/app/buildinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package app

import (
"bytes"
"context"
"encoding/json"
"os"
"runtime/debug"
"sync"
"testing"
"time"

"gotest.tools/v3/assert"
)

func TestBuildInfo(t *testing.T) {
ctx, ctxC := context.WithTimeout(context.Background(), time.Second*15)
defer ctxC()

debugInfo = func() (info *debug.BuildInfo, ok bool) {
return &debug.BuildInfo{
GoVersion: "go1.22.0",
Path: "github.com/taubyte/tau",
Main: debug.Module{
Path: "github.com/taubyte/tau",
},
Deps: []*debug.Module{
{
Path: "atomicgo.dev/cursor",
Version: "v0.2.0",
Sum: "h1:H6XN5alUJ52FZZUkI7AlJbUc1aW38GWZalpYRPpoPOw=",
},
{
Path: "atomicgo.dev/keyboard",
Version: "v0.2.9",
Sum: "h1:tOsIid3nlPLZ3lwgG8KZMp/SFmr7P0ssEN5JUsm78K8=",
},
},
Settings: []debug.BuildSetting{
{
Key: "-buildmode",
Value: "exe",
},
{
Key: "-compiler",
Value: "gc",
},
{
Key: "GOARCH",
Value: "amd64",
},
{
Key: "GOOS",
Value: "linux",
},
{
Key: "vcs",
Value: "git",
},
{
Key: "vcs.revision",
Value: "92752b7bae67ab78d1de8b6ee4a3af8c7fdbb3cd",
},
},
}, true
}

var (
fakeOutput bytes.Buffer
fakeOutputLock sync.Mutex
)
buildInfoOutput = &fakeOutput

t.Run("info build", func(t *testing.T) {
fakeOutputLock.Lock()
defer fakeOutputLock.Unlock()

fakeOutput.Reset()

err := newApp().RunContext(ctx, []string{os.Args[0], "info", "build"})

assert.NilError(t, err)

assert.Equal(t, fakeOutput.String(), `go go1.22.0
path github.com/taubyte/tau
mod github.com/taubyte/tau
build -buildmode=exe
build -compiler=gc
build GOARCH=amd64
build GOOS=linux
build vcs=git
build vcs.revision=92752b7bae67ab78d1de8b6ee4a3af8c7fdbb3cd
`)
})

t.Run("info build --deps", func(t *testing.T) {
fakeOutputLock.Lock()
defer fakeOutputLock.Unlock()

fakeOutput.Reset()

err := newApp().RunContext(ctx, []string{os.Args[0], "info", "build", "--deps"})

assert.NilError(t, err)

assert.Equal(t, fakeOutput.String(), `go go1.22.0
path github.com/taubyte/tau
mod github.com/taubyte/tau
dep atomicgo.dev/cursor v0.2.0 h1:H6XN5alUJ52FZZUkI7AlJbUc1aW38GWZalpYRPpoPOw=
dep atomicgo.dev/keyboard v0.2.9 h1:tOsIid3nlPLZ3lwgG8KZMp/SFmr7P0ssEN5JUsm78K8=
build -buildmode=exe
build -compiler=gc
build GOARCH=amd64
build GOOS=linux
build vcs=git
build vcs.revision=92752b7bae67ab78d1de8b6ee4a3af8c7fdbb3cd
`)
})

t.Run("info build --json", func(t *testing.T) {
fakeOutputLock.Lock()
defer fakeOutputLock.Unlock()

fakeOutput.Reset()
err := newApp().RunContext(ctx, []string{os.Args[0], "info", "build", "--json"})

assert.NilError(t, err)

fout := fakeOutput.Bytes()

assert.Equal(t, string(fout), `{"GoVersion":"go1.22.0","Path":"github.com/taubyte/tau","Main":{"Path":"github.com/taubyte/tau","Version":"","Sum":"","Replace":null},"Deps":null,"Settings":[{"Key":"-buildmode","Value":"exe"},{"Key":"-compiler","Value":"gc"},{"Key":"GOARCH","Value":"amd64"},{"Key":"GOOS","Value":"linux"},{"Key":"vcs","Value":"git"},{"Key":"vcs.revision","Value":"92752b7bae67ab78d1de8b6ee4a3af8c7fdbb3cd"}]}
`)

var output any
err = json.Unmarshal(fout, &output)
assert.NilError(t, err)
})

t.Run("info commit", func(t *testing.T) {
fakeOutputLock.Lock()
defer fakeOutputLock.Unlock()

fakeOutput.Reset()
err := newApp().RunContext(ctx, []string{os.Args[0], "info", "commit"})

assert.NilError(t, err)

fout := fakeOutput.Bytes()

assert.Equal(t, string(fout), `92752b7bae67ab78d1de8b6ee4a3af8c7fdbb3cd
`)

})

}

func TestBuildInfoFail(t *testing.T) {
ctx, ctxC := context.WithTimeout(context.Background(), time.Second*15)
defer ctxC()

debugInfo = func() (info *debug.BuildInfo, ok bool) {
return nil, false
}
var fakeOutput bytes.Buffer
buildInfoOutput = &fakeOutput

err := newApp().RunContext(ctx, []string{os.Args[0], "info", "build"})

assert.Error(t, err, "no build information found")

}

0 comments on commit c624443

Please sign in to comment.