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 resolved config #179

Merged
merged 5 commits into from
May 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Sometimes use these for testing
.yamlfmt
!integrationtest/**/.yamlfmt
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice, I didn't think of that

tmp

# Goreleaser build folder
Expand Down
4 changes: 4 additions & 0 deletions cmd/yamlfmt/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ operation without performing it.`)
flagConf *string = flag.String("conf", "", "Read yamlfmt config from this path")
flagGlobalConf *bool = flag.Bool("global_conf", false, fmt.Sprintf("Use global yamlfmt config from %s", globalConfFlagVar()))
flagDisableGlobalConf *bool = flag.Bool("no_global_conf", false, fmt.Sprintf("Disabled usage of global yamlfmt config from %s", globalConfFlagVar()))
flagPrintConf *bool = flag.Bool("print_conf", false, "Print config")
flagDoublestar *bool = flag.Bool("dstar", false, "Use doublestar globs for include and exclude")
flagQuiet *bool = flag.Bool("quiet", false, "Print minimal output to stdout")
flagContinueOnError *bool = flag.Bool("continue_on_error", false, "Continue to format files that didn't fail instead of exiting with code 1.")
Expand Down Expand Up @@ -97,6 +98,9 @@ func getOperationFromFlag() yamlfmt.Operation {
if *flagDry {
return yamlfmt.OperationDry
}
if *flagPrintConf {
return yamlfmt.OperationPrintConfig
}
return yamlfmt.OperationFormat
}

Expand Down
27 changes: 27 additions & 0 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (

"github.com/google/yamlfmt"
"github.com/google/yamlfmt/engine"
"github.com/mitchellh/mapstructure"

"github.com/braydonk/yaml"
)

type FormatterConfig struct {
Expand Down Expand Up @@ -159,6 +162,30 @@ func (c *Command) Run() error {
return err
}
fmt.Print(string(out))
case yamlfmt.OperationPrintConfig:
commandConfig := map[string]any{}
err = mapstructure.Decode(c.Config, &commandConfig)
if err != nil {
return err
}
delete(commandConfig, "formatter")
out, err := yaml.Marshal(commandConfig)
if err != nil {
return err
}
fmt.Print(string(out))

formatterConfigMap, err := formatter.ConfigMap()
if err != nil {
return err
}
out, err = yaml.Marshal(map[string]any{
"formatter": formatterConfigMap,
})
if err != nil {
return err
}
fmt.Print(string(out))
}

return nil
Expand Down
17 changes: 9 additions & 8 deletions docs/command-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ 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. |
| 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. |
| Name | Flag | Example | Description |
| :------------ | :------------ | :-------------------------- | :-------------------------------------------------------- |
| Help | `-help` | `yamlfmt -help` | Print the command usage information. |
| Print Version | `-version` | `yamlfmt -version` | Print the yamlfmt version. |
| Print Config | `-print_conf` | `yamlfmt -print_conf` | Print the merged configuration to use. |
| 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
2 changes: 2 additions & 0 deletions docs/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ If the flag `-global_conf` is passed, all other steps will be circumvented and t

In the `-conf` flag, the config file can be named anything. As long as it's valid yaml, yamlfmt will read it as a config file. This can be useful for applying unique configs to different directories in a project. The automatic discovery paths do need to use one of the known names.

In the `-print_conf` flag, merged config values will be printed.

## Command

The command package defines the main command engine that `cmd/yamlfmt` uses. It uses the top level configuration that any run of the yamlfmt command will use.
Expand Down
1 change: 1 addition & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
OperationLint
OperationDry
OperationStdin
OperationPrintConfig
)

type Engine interface {
Expand Down
1 change: 1 addition & 0 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import "fmt"
type Formatter interface {
Type() string
Format(yamlContent []byte) ([]byte, error)
ConfigMap() (map[string]any, error)
}

type Factory interface {
Expand Down
11 changes: 11 additions & 0 deletions formatters/basic/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/braydonk/yaml"
"github.com/google/yamlfmt"
"github.com/mitchellh/mapstructure"
)

const BasicFormatterType string = "basic"
Expand Down Expand Up @@ -113,3 +114,13 @@ func (f *BasicFormatter) getNewEncoder(buf *bytes.Buffer) *yaml.Encoder {

return e
}

func (f *BasicFormatter) ConfigMap() (map[string]any, error) {
configMap := map[string]any{}
err := mapstructure.Decode(f.Config, &configMap)
if err != nil {
return nil, err
}
configMap["type"] = BasicFormatterType
return configMap, err
}
23 changes: 23 additions & 0 deletions integrationtest/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,26 @@ func TestDryQuiet(t *testing.T) {
Update: *updateFlag,
}.Run(t)
}

func TestPrintConfFlags(t *testing.T) {
TestCase{
Dir: "print_conf_flags",
Command: yamlfmtWithArgs("-print_conf -continue_on_error=true -formatter retain_line_breaks=true"),
Update: *updateFlag,
}.Run(t)
}

func TestPrintConfFile(t *testing.T) {
TestCase{
Dir: "print_conf_file",
Command: yamlfmtWithArgs("-print_conf"),
Update: *updateFlag,
}.Run(t)
}
func TestPrintConfFlagsAndFile(t *testing.T) {
TestCase{
Dir: "print_conf_flags_and_file",
Command: yamlfmtWithArgs("-print_conf -continue_on_error=true -formatter retain_line_breaks=true"),
Update: *updateFlag,
}.Run(t)
}
11 changes: 11 additions & 0 deletions integrationtest/command/testdata/print_conf_file/after/.yamlfmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
continue_on_error: false
doublestar: true
exclude:
- "**/templates/*.yaml"
gitignore_excludes: false
gitignore_path: .my_gitignore
line_ending: crlf
formatter:
type: basic
retain_line_breaks_single: true
include_document_start: true
2 changes: 2 additions & 0 deletions integrationtest/command/testdata/print_conf_file/after/a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a:
b: 1
11 changes: 11 additions & 0 deletions integrationtest/command/testdata/print_conf_file/before/.yamlfmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
continue_on_error: false
doublestar: true
exclude:
- "**/templates/*.yaml"
gitignore_excludes: false
gitignore_path: .my_gitignore
line_ending: crlf
formatter:
type: basic
retain_line_breaks_single: true
include_document_start: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a:
b: 1
Empty file.
26 changes: 26 additions & 0 deletions integrationtest/command/testdata/print_conf_file/stdout/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
continue_on_error: false
doublestar: true
exclude:
- '**/templates/*.yaml'
extensions:
- yaml
- yml
gitignore_excludes: false
gitignore_path: .my_gitignore
include: []
line_ending: crlf
output_format: default
regex_exclude: []
formatter:
disallow_anchors: false
drop_merge_tag: false
include_document_start: true
indent: 2
indentless_arrays: false
line_ending: crlf
max_line_length: 0
pad_line_comments: 1
retain_line_breaks: false
retain_line_breaks_single: true
scan_folded_as_literal: false
type: basic
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a:
b: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a:
b: 1
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
continue_on_error: true
doublestar: false
exclude: []
extensions:
- yaml
- yml
gitignore_excludes: false
gitignore_path: .gitignore
include: []
line_ending: lf
output_format: default
regex_exclude: []
formatter:
disallow_anchors: false
drop_merge_tag: false
include_document_start: false
indent: 2
indentless_arrays: false
line_ending: lf
max_line_length: 0
pad_line_comments: 1
retain_line_breaks: true
retain_line_breaks_single: false
scan_folded_as_literal: false
type: basic
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
continue_on_error: false
doublestar: true
exclude:
- "**/templates/*.yaml"
gitignore_excludes: false
gitignore_path: .my_gitignore
line_ending: crlf
formatter:
type: basic
retain_line_breaks_single: true
include_document_start: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a:
Copy link
Collaborator

@braydonk braydonk May 13, 2024

Choose a reason for hiding this comment

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

These yaml files can be removed for these -print_conf tests. They should be able to operate without them.

b: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
continue_on_error: false
doublestar: true
exclude:
- "**/templates/*.yaml"
gitignore_excludes: false
gitignore_path: .my_gitignore
line_ending: crlf
formatter:
type: basic
retain_line_breaks_single: true
include_document_start: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a:
b: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
continue_on_error: true
doublestar: true
exclude:
- '**/templates/*.yaml'
extensions:
- yaml
- yml
gitignore_excludes: false
gitignore_path: .my_gitignore
include: []
line_ending: crlf
output_format: default
regex_exclude: []
formatter:
disallow_anchors: false
drop_merge_tag: false
include_document_start: true
indent: 2
indentless_arrays: false
line_ending: crlf
max_line_length: 0
pad_line_comments: 1
retain_line_breaks: true
retain_line_breaks_single: true
scan_folded_as_literal: false
type: basic
Loading