-
Notifications
You must be signed in to change notification settings - Fork 301
/
example.go
107 lines (93 loc) · 2.59 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// 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.
// example cobra command with config.
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"go.thethings.network/lorawan-stack/v3/pkg/config"
yaml "gopkg.in/yaml.v2"
)
// Config contains the base configuration with shared flags.
type Config struct {
config.Base `name:",squash"`
Int int `name:"int" description:"An example int"`
String string `name:"string" description:"An example string"`
}
// SubConfig contains the flags for the sub command.
type SubConfig struct {
Config `name:",squash"`
Bar string `name:"bar" description:"The bar config flag"`
}
var (
// The mgr is a configuration manager for the example program, initialized
// with default config values.
mgr = config.InitializeWithDefaults("example", "example", &Config{
Int: 42,
String: "foo",
})
// The cfg will be the config shared by all commands.
cfg = new(Config)
root = &cobra.Command{
// PersistentPreRunE runs always, so we can use it to read in config files
// and parse the shared config.
PersistentPreRunE: func(*cobra.Command, []string) error {
err := mgr.ReadInConfig()
if err != nil {
return err
}
return mgr.Unmarshal(cfg)
},
RunE: func(*cobra.Command, []string) error {
return printYAML(cfg)
},
}
sub = &cobra.Command{
Use: "sub",
// RunE parses the sub config that is only relevant for this command.
RunE: func(*cobra.Command, []string) error {
cfg := new(SubConfig)
err := mgr.Unmarshal(cfg)
if err != nil {
return err
}
return printYAML(cfg)
},
}
)
func init() {
root.AddCommand(sub)
root.Flags().AddFlagSet(mgr.Flags())
sub.Flags().AddFlagSet(mgr.WithConfig(&SubConfig{
Bar: "baz",
}))
}
func main() {
// execute the commands and print resulting errors.
err := root.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// printYAML prints the nested config struct.
func printYAML(in any) error {
bs, err := yaml.Marshal(in)
if err != nil {
return err
}
fmt.Print(string(bs))
return nil
}