Skip to content

Commit

Permalink
feat: add flag to print version (#117)
Browse files Browse the repository at this point in the history
* feat: add flag to print version

* fix: immediately exit when given -version flag

#117 (review)

* chore: remove outdated code

* refactor: simplify error handling

#117 (comment)
#117 (comment)
  • Loading branch information
kachick authored May 29, 2023
1 parent 5e63c21 commit 8cc9885
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/yamlfmt/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ source yaml and formatted yaml.`)
flagDry *bool = flag.Bool("dry", false, `Perform a dry run; show the output of a formatting
operation without performing it.`)
flagIn *bool = flag.Bool("in", false, "Format yaml read from stdin and output to stdout")
flagVersion *bool = flag.Bool("version", false, "Print yamlfmt version")
flagConf *string = flag.String("conf", "", "Read yamlfmt config from this path")
flagDoublestar *bool = flag.Bool("dstar", false, "Use doublestar globs for include and exclude")
flagQuiet *bool = flag.Bool("quiet", false, "Print minimal output to stdout")
Expand Down
8 changes: 8 additions & 0 deletions cmd/yamlfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ package main

import (
"flag"
"fmt"
"log"

"github.com/google/yamlfmt"
"github.com/google/yamlfmt/command"
"github.com/google/yamlfmt/formatters/basic"
)

var version string = "0.10.0"

func main() {
if err := run(); err != nil {
log.Fatal(err)
Expand All @@ -34,6 +37,11 @@ func run() error {
configureHelp()
flag.Parse()

if *flagVersion {
fmt.Printf("%s\n", version)
return nil
}

c := &command.Command{
Operation: getOperationFromFlag(),
Registry: getFullRegistry(),
Expand Down
15 changes: 8 additions & 7 deletions docs/command-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ All flags must be specified **before** any path arguments.

These flags adjust the command's mode of operation. All of these flags are booleans.

| Name | Flag | Example | Description |
|:-----------|:---------|:---------------------------|:------------|
| Help | `-help` | `yamlfmt -help` | Print the command usage information. |
| Dry Run | `-dry` | `yamlfmt -dry .` | Use [Dry Run](#dry-run) mode |
| Lint | `-lint` | `yamlfmt -lint .` | Use [Lint](#lint) mode |
| Quiet Mode | `-quiet` | `yamlfmt -dry -quiet .` | Use quiet mode. Only has effect in Dry Run or Lint modes. |
| Read Stdin | `-in` | `cat x.yaml | yamlfmt -in` | Read input from stdin and output result to stdout. |
| Name | Flag | Example | Description |
| :------------ | :--------- | :-------------------------- | :-------------------------------------------------------- |
| Help | `-help` | `yamlfmt -help` | Print the command usage information. |
| Print Version | `-version` | `yamlfmt -version` | Print the yamlfmt version. |
| Dry Run | `-dry` | `yamlfmt -dry .` | Use [Dry Run](#dry-run) mode |
| Lint | `-lint` | `yamlfmt -lint .` | Use [Lint](#lint) mode |
| Quiet Mode | `-quiet` | `yamlfmt -dry -quiet .` | Use quiet mode. Only has effect in Dry Run or Lint modes. |
| Read Stdin | `-in` | `cat x.yaml \| yamlfmt -in` | Read input from stdin and output result to stdout. |

### Configuration Flags

Expand Down

0 comments on commit 8cc9885

Please sign in to comment.