Skip to content

Commit

Permalink
feat: improve help message (#32)
Browse files Browse the repository at this point in the history
* feat: improve help message

This PR will improve the help message for the command. It also moves the
flag definitions to a separate file, since the flag functionality is
likely to increase in complexity.

* fix slight formatting issue in help
  • Loading branch information
braydonk committed Aug 25, 2022
1 parent fc7dca4 commit fe69346
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 43 deletions.
86 changes: 86 additions & 0 deletions cmd/yamlfmt/flags.go
@@ -0,0 +1,86 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"flag"
"fmt"
"os"
"path"

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

var (
flagLint *bool = flag.Bool("lint", false, `Check if there are any differences between
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.`)
flagConf *string = flag.String("conf", "", "Read yamlfmt config from this path")
)

func getOperation() command.Operation {
if len(flag.Args()) == 1 && isStdin(flag.Args()[0]) {
return command.OperationStdin
}
if *flagLint {
return command.OperationLint
}
if *flagDry {
return command.OperationDry
}
return command.OperationFormat
}

func isStdin(arg string) bool {
return arg == "-" || arg == "/dev/stdin"
}

func getConfigPath() (string, error) {
configPath := *flagConf
if configPath == "" {
configPath = defaultConfigName
}

if path.IsAbs(configPath) {
return configPath, nil
}

wd, err := os.Getwd()
if err != nil {
return "", err
}
return path.Join(wd, configPath), nil
}

func configureHelp() {
flag.Usage = printHelpMessage
}

func printHelpMessage() {
fmt.Println(`yamlfmt is a simple command line tool for formatting yaml files.
Arguments:
Glob paths to yaml files
Send any number of paths to yaml files specified in doublestar glob format (see: https://github.com/bmatcuk/doublestar).
Any flags must be specified before the paths.
- or /dev/stdin
Passing in a single - or /dev/stdin will read the yaml from stdin and output the formatted result to stdout
Flags:`)
flag.PrintDefaults()
}
44 changes: 1 addition & 43 deletions cmd/yamlfmt/main.go
Expand Up @@ -19,22 +19,13 @@ import (
"flag"
"log"
"os"
"path"

"github.com/google/yamlfmt"
"github.com/google/yamlfmt/command"
"github.com/google/yamlfmt/formatters/basic"
"gopkg.in/yaml.v3"
)

var (
flagLint *bool = flag.Bool("lint", false, `Check if there are any differences between
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.`)
flagConf *string = flag.String("conf", "", "Read yamlfmt config from this path")
)

const defaultConfigName = ".yamlfmt"

func main() {
Expand All @@ -44,6 +35,7 @@ func main() {
}

func run() error {
configureHelp()
flag.Parse()

operation := getOperation()
Expand All @@ -69,23 +61,6 @@ func run() error {
return command.RunCommand(operation, getFullRegistry(), configData)
}

func getConfigPath() (string, error) {
configPath := *flagConf
if configPath == "" {
configPath = defaultConfigName
}

if path.IsAbs(configPath) {
return configPath, nil
}

wd, err := os.Getwd()
if err != nil {
return "", err
}
return path.Join(wd, configPath), nil
}

func readConfig(path string) (map[string]interface{}, error) {
if _, err := os.Stat(path); err != nil {
return nil, err
Expand All @@ -102,23 +77,6 @@ func readConfig(path string) (map[string]interface{}, error) {
return configData, nil
}

func getOperation() command.Operation {
if len(flag.Args()) == 1 && isStdin(flag.Args()[0]) {
return command.OperationStdin
}
if *flagLint {
return command.OperationLint
}
if *flagDry {
return command.OperationDry
}
return command.OperationFormat
}

func isStdin(arg string) bool {
return arg == "-" || arg == "/dev/stdin"
}

func getFullRegistry() *yamlfmt.Registry {
return yamlfmt.NewFormatterRegistry(&basic.BasicFormatterFactory{})
}

0 comments on commit fe69346

Please sign in to comment.