-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathotelcomponentmapping_describe.go
More file actions
75 lines (64 loc) · 2.41 KB
/
otelcomponentmapping_describe.go
File metadata and controls
75 lines (64 loc) · 2.41 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package otelcomponentmapping
import (
"fmt"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
"github.com/stackvista/stackstate-cli/internal/util"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
type DescribeArgs struct {
Identifier string
FilePath string
}
func OtelComponentMappingDescribeCommand(deps *di.Deps) *cobra.Command {
args := &DescribeArgs{}
cmd := &cobra.Command{
Use: "describe",
Short: "Describe an Otel Component Mapping",
Long: "Describe an Otel Component Mapping by identifier (URN). Optionally write to output file.",
Example: `# describe an OTel component mapping by identifier
sts otel-component-mapping describe --identifier urn:stackpack:stackpack-name:shared:otel-component-mapping:service
# output mapping to a file
sts otel-component-mapping describe --identifier urn:stackpack:stackpack-name:shared:otel-component-mapping:service --file exported.yaml`,
RunE: deps.CmdRunEWithApi(RunDescribeComponentMappingCommand(args)),
}
common.AddRequiredIdentifierFlagVar(cmd, &args.Identifier, "Identifier (URN) of the Component Mapping")
common.AddFileFlagVar(cmd, &args.FilePath, "Path to the output file")
return cmd
}
func RunDescribeComponentMappingCommand(args *DescribeArgs) di.CmdWithApiFn {
return func(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError {
mapping, resp, err := api.OtelMappingApi.GetOtelComponentMapping(cli.Context, args.Identifier).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
yamlData, err := yaml.Marshal(mapping)
if err != nil {
return common.NewExecutionError(fmt.Errorf("failed to marshal mapping to YAML: %v", err))
}
data := string(yamlData)
if args.FilePath != "" {
if err := util.WriteFile(args.FilePath, []byte(data)); err != nil {
return common.NewWriteFileError(err, args.FilePath)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{"filepath": args.FilePath})
} else {
cli.Printer.Success(fmt.Sprintf("OTel Component Mapping exported to: %s", args.FilePath))
}
return nil
} else {
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"data": mapping,
"format": "json",
})
} else {
cli.Printer.PrintLn(data)
}
return nil
}
}
}