From 8cc9885471a09adab12e9cfc2b3651206f978e53 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Mon, 29 May 2023 23:35:38 +0900 Subject: [PATCH] feat: add flag to print version (#117) * feat: add flag to print version * fix: immediately exit when given -version flag https://github.com/google/yamlfmt/pull/117#pullrequestreview-1449429049 * chore: remove outdated code * refactor: simplify error handling https://github.com/google/yamlfmt/pull/117#discussion_r1209328549 https://github.com/google/yamlfmt/pull/117#discussion_r1209330747 --- cmd/yamlfmt/flags.go | 1 + cmd/yamlfmt/main.go | 8 ++++++++ docs/command-usage.md | 15 ++++++++------- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/cmd/yamlfmt/flags.go b/cmd/yamlfmt/flags.go index a377200..cb7aac6 100644 --- a/cmd/yamlfmt/flags.go +++ b/cmd/yamlfmt/flags.go @@ -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") diff --git a/cmd/yamlfmt/main.go b/cmd/yamlfmt/main.go index 93756e4..e2ba177 100644 --- a/cmd/yamlfmt/main.go +++ b/cmd/yamlfmt/main.go @@ -16,6 +16,7 @@ package main import ( "flag" + "fmt" "log" "github.com/google/yamlfmt" @@ -23,6 +24,8 @@ import ( "github.com/google/yamlfmt/formatters/basic" ) +var version string = "0.10.0" + func main() { if err := run(); err != nil { log.Fatal(err) @@ -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(), diff --git a/docs/command-usage.md b/docs/command-usage.md index b62aa6a..9611e9d 100644 --- a/docs/command-usage.md +++ b/docs/command-usage.md @@ -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