-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample.go
More file actions
40 lines (33 loc) · 905 Bytes
/
example.go
File metadata and controls
40 lines (33 loc) · 905 Bytes
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
package cmd
import (
"encoding/json"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
var exampleIsJson, exampleIsYaml bool
var exampleCmd = &cobra.Command{
Use: "example",
Short: "Examples of OpsLevel resources",
Long: "Examples of OpsLevel resources in different formats",
}
func getExample[T any](v T) string {
var out []byte
var err error
if exampleIsJson {
out, err = json.Marshal(v)
} else {
out, err = yaml.Marshal(v)
}
if err != nil {
panic("unexpected error getting example")
}
return string(out)
}
func init() {
rootCmd.AddCommand(exampleCmd)
exampleCmd.PersistentFlags().BoolVar(&exampleIsJson, "json", false, "Output example in JSON format")
exampleCmd.PersistentFlags().BoolVar(&exampleIsYaml, "yaml", true, "Output example in YAML format")
exampleCmd.MarkFlagsMutuallyExclusive("json", "yaml")
viper.BindPFlags(exampleCmd.Flags())
}