Skip to content

Commit dcd5e07

Browse files
authored
(release): v0.6.1 (#33)
* (feat): config format and choose parameter to print * (release): v0.6.1
1 parent b8f872f commit dcd5e07

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [v0.6.1] - 2020-06-16
10+
11+
## Added
12+
13+
- `config` command can print the "global" parameters in `json` or `yaml`
14+
- `config` now accepts a argument, which is the name of the parameter,
15+
when informed only this parameter will be printed
16+
917
## [v0.6.0] - 2020-06-16
1018

1119
## Added

cmd/config.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,55 @@
1515
package cmd
1616

1717
import (
18+
"encoding/json"
19+
"errors"
1820
"fmt"
21+
"strings"
1922

2023
"github.com/spf13/viper"
2124
"gopkg.in/yaml.v2"
2225

2326
"github.com/spf13/cobra"
2427
)
2528

29+
const FORMAT_YAML = "yaml"
30+
const FORMAT_JSON = "json"
31+
2632
// configCmd represents the config command
2733
var configCmd = &cobra.Command{
28-
Use: "config",
34+
Use: "config [config-name]",
2935
Short: "Manages configuration file parameters",
36+
Args: cobra.MaximumNArgs(1),
3037
Run: func(cmd *cobra.Command, args []string) {
31-
b, _ := yaml.Marshal(viper.AllSettings())
38+
format, _ := cmd.Flags().GetString("format")
39+
40+
var b []byte
41+
42+
var v interface{}
43+
if len(args) == 0 {
44+
v = viper.AllSettings()
45+
} else {
46+
v = viper.Get(args[0])
47+
}
48+
49+
format = strings.ToLower(format)
50+
switch format {
51+
case FORMAT_JSON:
52+
b, _ = json.Marshal(v)
53+
54+
case FORMAT_YAML:
55+
b, _ = yaml.Marshal(v)
56+
default:
57+
printError(errors.New("invalid format"))
58+
return
59+
}
60+
3261
fmt.Println(string(b))
3362
},
3463
}
3564

3665
func init() {
3766
rootCmd.AddCommand(configCmd)
67+
68+
configCmd.Flags().StringP("format", "f", FORMAT_YAML, fmt.Sprintf("format of the output can be one of: %s, %s", FORMAT_YAML, FORMAT_JSON))
3869
}

0 commit comments

Comments
 (0)