Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add flag to print version #117

Merged
merged 5 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions cmd/yamlfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ package main

import (
"flag"
"fmt"
"log"
"os"

"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 +38,15 @@ func run() error {
configureHelp()
flag.Parse()

if *flagVersion {
_, err := fmt.Printf("%s\n", version)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By convention in this repo I'm not too worried about errors from fmt.Printf, so just doing the print here is fine.

I think this can also be simplified to fmt.Println(version) if I'm not mistaken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes simple code 👍

f616842

if err != nil {
return err
}

os.Exit(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just return nil here; it exits the program the same way.

Suggested change
os.Exit(0)
return nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I'm learning golang again from this project... 🙏

f616842

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate you making the effort to contribute in something that isn't your main language! Means a lot 😄

}

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. |
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line includes fix of #119

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, didn't notice this


### Configuration Flags

Expand Down