-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathotelcomponentmapping_edit.go
More file actions
95 lines (79 loc) · 3.22 KB
/
otelcomponentmapping_edit.go
File metadata and controls
95 lines (79 loc) · 3.22 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package otelcomponentmapping
import (
"fmt"
"strings"
"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"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
const LongDescription = `Edit an OTel Component Mapping.
The edit command allows you to directly edit any OTel Component Mapping. It will open
the editor defined by your EDITOR environment variables.
The OTel Component Mapping will be presented as JSON format for editing.
`
type EditArgs struct {
Identifier string
}
func OtelComponentMappingEditCommand(deps *di.Deps) *cobra.Command {
args := &EditArgs{}
cmd := &cobra.Command{
Use: "edit",
Short: "Edit an OTel Component Mapping using $EDITOR",
Long: LongDescription,
Example: `# edit a component mapping using your editor
sts otel-component-mapping edit --identifier urn:stackpack:stackpack-name:shared:otel-component-mapping:service`,
RunE: deps.CmdRunEWithApi(RunEditComponentMappingCommand(args)),
}
common.AddRequiredIdentifierFlagVar(cmd, &args.Identifier, "Identifier (URN) of the Component Mapping")
return cmd
}
func RunEditComponentMappingCommand(args *EditArgs) 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)
}
originalYAML, err := yaml.Marshal(mapping)
if err != nil {
return common.NewExecutionError(fmt.Errorf("failed to marshal mapping to YAML: %v", err))
}
editedContent, err := cli.Editor.Edit("otel-component-mapping-", ".yaml", strings.NewReader(string(originalYAML)))
if err != nil {
return common.NewExecutionError(fmt.Errorf("failed to open editor: %v", err))
}
// Check if changes were actually made
if string(originalYAML) == string(editedContent) {
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{"message": "No changes made"})
} else {
cli.Printer.PrintWarn("No changes made")
}
return nil
}
var editedMapping stackstate_api.OtelComponentMapping
if err := yaml.Unmarshal(editedContent, &editedMapping); err != nil {
return common.NewExecutionError(fmt.Errorf("failed to parse edited YAML: %v", err))
}
reqObj := stackstate_api.UpsertOtelComponentMappingsRequest{
Identifier: editedMapping.Identifier,
Name: editedMapping.Name,
Description: editedMapping.Description,
Input: editedMapping.Input,
Output: editedMapping.Output,
Vars: editedMapping.Vars,
ExpireAfter: editedMapping.ExpireAfter,
}
upserted, resp, err := api.OtelMappingApi.UpsertOtelComponentMappings(cli.Context).UpsertOtelComponentMappingsRequest(reqObj).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{"otel_component_mapping": upserted})
} else {
cli.Printer.Success(fmt.Sprintf("OTel Component Mapping updated successfully! Identifier: %s, Name: %s", upserted.GetIdentifier(), upserted.GetName()))
}
return nil
}
}