Skip to content

Commit

Permalink
feat: add a --version flag. (#3)
Browse files Browse the repository at this point in the history
Release builds will have the correct `version` and `commit` set at build
time using `ldflags`. The README is updated to describe setting the
`ldflags` correctly to pick up your local build SHA for development
builds from source that don't use `goreleaser`. This should be helpful
for knowing which version of the tool you're using.
  • Loading branch information
cpu committed Dec 5, 2021
1 parent e1e106b commit a3286ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ git clone https://github.com/dune-mud/dumpdb.git && cd dumpdb
```
* Build and install
```bash
go install ./...
go install -ldflags "-X main.commit=$(git rev-parse HEAD)" ./...
```
* Run the `dumpdb` executable from your Go bin.
```bash
Expand Down
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import (
"os"
)

// goreleaser updates these at build time. These are the defaults used
// for a native Go build with no ldflags overrides.
var (
version = "dev"
commit = "unknown"
)

// errPrint prints a formatted message to stderr and exits non-zero.
func errPrint(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "ERROR: "+msg, args...)
Expand All @@ -20,8 +27,14 @@ func infoPrint(msg string, args ...interface{}) {
func main() {
dbFile := flag.String("db", "OBJ_DUMP.sqlite", "SQLite Database file path to populate")
forceFlag := flag.Bool("force", false, "Add OBJ_DUMP data to an existing database")
versionFlag := flag.Bool("version", false, "Print the version and quit")
flag.Parse()

if *versionFlag {
infoPrint("dumpdb version %s - commit %s\n", version, commit)
os.Exit(0)
}

if _, err := os.Stat(*dbFile); err == nil && !(*forceFlag) {
errPrint(
"db %q exists and -force was not specified. Remove the database or add -force\n",
Expand Down

0 comments on commit a3286ca

Please sign in to comment.