Skip to content

Commit cbf22ef

Browse files
committed
add function to convert servers userdata bytearray to base64
1 parent 9dfc1f3 commit cbf22ef

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

internal/cmd/server/describe/describe.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ type inputModel struct {
3030
ServerId string
3131
}
3232

33+
// convertServerForYAML creates a map with UserData as base64 string for YAML output
34+
func convertServerForYAML(server *iaas.Server) map[string]interface{} {
35+
if server == nil {
36+
return nil
37+
}
38+
39+
// Marshal to JSON first to get the correct format for UserData
40+
jsonData, err := json.Marshal(server)
41+
if err != nil {
42+
return nil
43+
}
44+
45+
var serverMap map[string]interface{}
46+
if err := json.Unmarshal(jsonData, &serverMap); err != nil {
47+
return nil
48+
}
49+
50+
return serverMap
51+
}
52+
3353
func NewCmd(params *params.CmdParams) *cobra.Command {
3454
cmd := &cobra.Command{
3555
Use: fmt.Sprintf("describe %s", serverIdArg),
@@ -118,7 +138,7 @@ func outputResult(p *print.Printer, outputFormat string, server *iaas.Server) er
118138

119139
return nil
120140
case print.YAMLOutputFormat:
121-
details, err := yaml.MarshalWithOptions(server, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
141+
details, err := yaml.MarshalWithOptions(convertServerForYAML(server), yaml.IndentSequence(true), yaml.UseJSONMarshaler())
122142
if err != nil {
123143
return fmt.Errorf("marshal server: %w", err)
124144
}

0 commit comments

Comments
 (0)