Skip to content

Commit

Permalink
feat: add build info flag
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Braun <[email protected]>
  • Loading branch information
bluebrown committed Nov 24, 2024
1 parent 0e9f34f commit 5ed11bc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ else
fi

mkdir -p "$dest"

for build in "${targets[@]}"; do
os="$(echo "$build" | cut -d'/' -f1)"
arch="$(echo "$build" | cut -d'/' -f2)"

echo "Building for $os/$arch"

GOOS="$os" GOARCH="$arch" go build \
-o "$dest/tpl-$os-$arch" \
-ldflags "-w -s -X main.version=$vers" \
./cmd/tpl

done
27 changes: 23 additions & 4 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"runtime/debug"
"strings"
"text/template"

Expand All @@ -15,9 +16,9 @@ import (
"gopkg.in/yaml.v3"
)

var version = "dev"

type commandLine struct {
versionTag string

// options
defaultTemplateName string
files []string
Expand All @@ -27,6 +28,7 @@ type commandLine struct {
decoder decoder
noNewline bool
showVersion bool
showBuildInfo bool

// internal state
flagSet *pflag.FlagSet
Expand All @@ -35,12 +37,13 @@ type commandLine struct {

// create a New cli instance and bind flags to it
// flag.Parse is called on run
func New(fs *pflag.FlagSet) *commandLine {
func New(tag string, fs *pflag.FlagSet) *commandLine {
if fs == nil {
fs = pflag.CommandLine
}

cli := &commandLine{
versionTag: tag,
flagSet: fs,
decoder: decodeJson,
defaultTemplateName: "_gotpl_default",
Expand All @@ -53,18 +56,34 @@ func New(fs *pflag.FlagSet) *commandLine {
fs.StringArrayVar(&cli.options, "option", cli.options, "option to pass to the template engine. Can be specified multiple times")
fs.BoolVar(&cli.noNewline, "no-newline", cli.noNewline, "do not print newline at the end of the output")
fs.BoolVar(&cli.showVersion, "version", cli.showVersion, "show version information and exit")
fs.BoolVar(&cli.showBuildInfo, "info", cli.showBuildInfo, "show debug build info and exit")

return cli
}

func (cli *commandLine) WriteInfo(w io.Writer) {
fmt.Fprintf(w, "version: %s\n", cli.versionTag)
fmt.Fprintf(w, "build:\n")
if info, ok := debug.ReadBuildInfo(); ok {
for _, s := range info.Settings {
fmt.Fprintf(w, "\t%s=%s\n", s.Key, s.Value)
}
}
}

// parse the options and input, decode the input and render the result
func (cli *commandLine) Run(args []string, r io.Reader, w io.Writer) (err error) {
if err := cli.parse(args); err != nil {
return fmt.Errorf("parse: %w", err)
}

if cli.showVersion {
fmt.Fprintln(w, version)
fmt.Fprintf(w, "%s", cli.versionTag)
return nil
}

if cli.showBuildInfo {
cli.WriteInfo(w)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func Test_state_run(t *testing.T) {
in = strings.NewReader(tt.giveInput)
}

err := New(pflag.NewFlagSet(tt.name, pflag.ContinueOnError)).Run(tt.giveArgs, in, output)
err := New("test", pflag.NewFlagSet(tt.name, pflag.ContinueOnError)).Run(tt.giveArgs, in, output)

if len(tt.wantErrorMatch) > 0 {
if err == nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/tpl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
cli "github.com/bluebrown/go-template-cli"
)

var version = "dev"

func main() {
// reader may be used, if possible
var input io.Reader
Expand All @@ -20,7 +22,7 @@ func main() {
}

// try to run the program
err := cli.New(nil).Run(os.Args[1:], input, os.Stdout)
err := cli.New(version, nil).Run(os.Args[1:], input, os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(2)
Expand Down

0 comments on commit 5ed11bc

Please sign in to comment.