Skip to content

Commit

Permalink
Merge pull request #62 from blp1526/rename_format_to_output
Browse files Browse the repository at this point in the history
Rename format to output
  • Loading branch information
blp1526 authored Dec 7, 2019
2 parents 007a28d + 082fd53 commit dc2d107
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion blkinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (

// BlkInfo shows block device information.
type BlkInfo struct {
MajorMinor string `json:"major_minor" `
Path string `json:"path" `
RealPath string `json:"real_path" `
ParentPath string `json:"parent_path" `
ChildPaths []string `json:"child_paths" `
SysPath string `json:"sys_path" `
Sys *Sys `json:"sys" `
MajorMinor string `json:"major_minor" `
UdevDataPath string `json:"udev_data_path" `
UdevData []string `json:"udev_data" `
MountInfoPath string `json:"mount_info_path"`
Expand Down
27 changes: 14 additions & 13 deletions cmd/blkinfo/blkinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ func main() { // nolint: funlen
},
}
app.HideHelp = true
allowedFormat := "[json|yaml]"
allowedOutput := "[json|yaml]"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "help, h",
Usage: "show help",
},
cli.StringFlag{
Name: "format, f",
Name: "output, o",
Value: "json",
Usage: fmt.Sprintf("output format %s", allowedFormat),
Usage: fmt.Sprintf("output as %s", allowedOutput),
},
}

Expand All @@ -46,7 +46,8 @@ func main() { // nolint: funlen
cli.ShowAppHelpAndExit(c, 0)
}

if len(c.Args()) != 1 {
allowedArgsSize := 1
if len(c.Args()) != allowedArgsSize {
cli.ShowAppHelpAndExit(c, 0)
}

Expand All @@ -57,26 +58,26 @@ func main() { // nolint: funlen
return cli.NewExitError(err, exitCodeNG)
}

b, err := json.MarshalIndent(bi, "", " ")
bytes, err := json.MarshalIndent(bi, "", " ")
if err != nil {
return cli.NewExitError(err, exitCodeNG)
}

format := c.String("format")
switch format {
output := c.String("output")
switch output {
case "json":
break
case "yaml":
b, err = yaml.JSONToYAML(b)
if err != nil {
return cli.NewExitError(err, exitCodeNG)
}
bytes, err = yaml.JSONToYAML(bytes)
default:
err = fmt.Errorf("unknown format '%s', expected %s", format, allowedFormat)
err = fmt.Errorf("unknown output '%s', expected %s", output, allowedOutput)
}

if err != nil {
return cli.NewExitError(err, exitCodeNG)
}

s := strings.TrimSpace(string(b))
s := strings.TrimSpace(string(bytes))
fmt.Printf("%s\n", s)

return nil
Expand Down

0 comments on commit dc2d107

Please sign in to comment.