Skip to content

Commit

Permalink
feat: print version info using rbmk version (#40)
Browse files Browse the repository at this point in the history
Useful to know which version we're using (e.g., in bug reports).
  • Loading branch information
bassosimone authored Dec 8, 2024
1 parent f35ca36 commit dbde41f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/rbmk-project/rbmk/pkg/cli/tar"
"github.com/rbmk-project/rbmk/pkg/cli/timestamp"
"github.com/rbmk-project/rbmk/pkg/cli/tutorial"
"github.com/rbmk-project/rbmk/pkg/cli/version"
)

//go:embed README.md
Expand All @@ -46,5 +47,6 @@ func NewCommand() cliutils.Command {
"tar": tar.NewCommand(),
"timestamp": timestamp.NewCommand(),
"tutorial": tutorial.NewCommand(),
"version": version.NewCommand(),
})
}
22 changes: 22 additions & 0 deletions pkg/cli/version/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# rbmk version - Print Version Information

## Usage

```
rbmk version
```

## Description

Prints on the stdout version information. We add version information
when compiling `rbmk` using the `GNUMakefile`.

Possible values for the version information are:

- `dev` if we did not compile using the `GNUMakefile`.

- `vX.Y.Z` if using `GNUMakefile` to build a specific tag.

- `vX.Y.Z-<N>-g<SHA>` if using `GNUMakefile` to build
a commit not associated with a tag.
44 changes: 44 additions & 0 deletions pkg/cli/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: GPL-3.0-or-later

// Package version implements the `rbmk version` command.
package version

import (
"context"
_ "embed"
"fmt"

"github.com/rbmk-project/common/cliutils"
"github.com/rbmk-project/rbmk/internal/markdown"
)

// Version is the program version.
var Version string = "dev"

//go:embed README.md
var readme string

// NewCommand creates the `rbmk version` Command.
func NewCommand() cliutils.Command {
return command{}
}

type command struct{}

// Help implements [cliutils.Command].
func (cmd command) Help(env cliutils.Environment, argv ...string) error {
fmt.Fprintf(env.Stdout(), "%s\n", markdown.MaybeRender(readme))
return nil
}

// Main implements [cliutils.Command].
func (cmd command) Main(ctx context.Context, env cliutils.Environment, argv ...string) error {
// 1. honour requests for printing the help
if cliutils.HelpRequested(argv...) {
return cmd.Help(env, argv...)
}

// 2. print the version
fmt.Fprintln(env.Stdout(), Version)
return nil
}

0 comments on commit dbde41f

Please sign in to comment.