-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample.go
More file actions
60 lines (51 loc) · 1.21 KB
/
example.go
File metadata and controls
60 lines (51 loc) · 1.21 KB
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
package cmd
import (
"encoding/json"
"github.com/opslevel/opslevel-go/v2025"
"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]() string {
if exampleIsJson {
return getJson[T]()
}
return getYaml[T]()
}
func getJson[T any]() string {
var (
out []byte
err error
)
t := opslevel.NewExampleOf[T]()
out, err = json.Marshal(t)
if err != nil {
panic("unexpected error getting example json")
}
return string(out)
}
func getYaml[T any]() string {
var (
out []byte
err error
)
t := opslevel.NewExampleOf[T]()
out, err = yaml.Marshal(t)
if err != nil {
panic("unexpected error getting example yaml")
}
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())
}