Skip to content

Commit

Permalink
Add version option to display app version
Browse files Browse the repository at this point in the history
  • Loading branch information
catatsuy committed Mar 31, 2024
1 parent d303075 commit 3317578
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ all: bin/purl
go.mod go.sum:
go mod tidy

bin/purl: main.go cli/*.go
go build -o bin/purl main.go
bin/purl: main.go cli/*.go go.mod go.sum
go build -ldflags "-X github.com/catatsuy/purl/cli.Version=`git rev-list HEAD -n1`" -o bin/purl main.go

.PHONY: vet
vet:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Purl is a versatile text processing tool designed to easily and efficiently modi
- **`-replace`**: This option requires a replacement expression to specify the text you intend to change. Format your command as "@search@replace@", with "search" being the text to find and "replace" the text to insert.
- **`-color`** and **`-no-color`**: By default, Purl's output colorization is set to auto, determining the best mode based on your environment. Use `-no-color` if you prefer the output without colorization, regardless of the environment.
- **`-help`**: Display information about Purl and its various options.
- **`-version`**: Display version.

## Usage Examples

Expand Down
31 changes: 29 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"os"
"regexp"
"runtime"
"runtime/debug"

"golang.org/x/term"
)
Expand All @@ -17,6 +19,22 @@ const (
ExitCodeFail = 1
)

var (
Version string
)

func version() string {
if Version != "" {
return Version
}

info, ok := debug.ReadBuildInfo()
if !ok {
return "(devel)"
}
return info.Main.Version
}

type rawStrings []string

func (i *rawStrings) String() string {
Expand All @@ -39,10 +57,13 @@ type CLI struct {
excludes rawStrings
help bool
color bool
version bool

appVersion string
}

func NewCLI(outStream, errStream io.Writer, inputStream io.Reader) *CLI {
return &CLI{outStream: outStream, errStream: errStream, inputStream: inputStream}
return &CLI{appVersion: version(), outStream: outStream, errStream: errStream, inputStream: inputStream}
}

func (c *CLI) Run(args []string) int {
Expand All @@ -52,6 +73,11 @@ func (c *CLI) Run(args []string) int {
return ExitCodeParseFlagError
}

if c.version {
fmt.Fprintf(c.errStream, "purl version %s; %s\n", c.appVersion, runtime.Version())
return ExitCodeOK
}

if c.help {
flags.Usage()
return ExitCodeOK
Expand Down Expand Up @@ -150,9 +176,10 @@ func (c *CLI) parseFlags(args []string) (*flag.FlagSet, error) {
flags.BoolVar(&c.color, "color", false, "Colored output. Default auto.")
flags.BoolVar(&noColor, "no-color", false, "Disable colored output.")
flags.BoolVar(&c.help, "help", false, `Show help`)
flags.BoolVar(&c.version, "version", false, "Print version and quit")

flags.Usage = func() {
fmt.Fprintln(c.errStream, "Usage: purl [options] [file]")
fmt.Fprintf(c.errStream, "purl version %s; %s\nUsage: purl [options] [file]\n", c.appVersion, runtime.Version())
flags.PrintDefaults()
}

Expand Down
5 changes: 5 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func TestRun_success(t *testing.T) {
args: []string{"purl", "-help"},
expectedCode: 0,
},
{
desc: "version option",
args: []string{"purl", "-version"},
expectedCode: 0,
},
{
desc: "filter",
args: []string{"purl", "-filter", "search"},
Expand Down

0 comments on commit 3317578

Please sign in to comment.