Skip to content

Commit

Permalink
pull common json marshal code into optJsonStrings
Browse files Browse the repository at this point in the history
  • Loading branch information
soulware committed Jun 6, 2023
1 parent fae4942 commit d5c4f27
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions internal/command/machine/status.go
Expand Up @@ -43,6 +43,15 @@ func newStatus() *cobra.Command {
return cmd
}

func optJsonStrings(v []string) string {
if len(v) > 0 {
bytes, _ := json.Marshal(v)
return string(bytes)
} else {
return ""
}
}

func runMachineStatus(ctx context.Context) (err error) {
io := iostreams.FromContext(ctx)

Expand All @@ -57,24 +66,6 @@ func runMachineStatus(ctx context.Context) (err error) {
fmt.Fprintf(io.Out, "Instance ID: %s\n", machine.InstanceID)
fmt.Fprintf(io.Out, "State: %s\n\n", machine.State)

entrypoint := func() string {
if len(machine.Config.Init.Entrypoint) > 0 {
bytes, _ := json.Marshal(machine.Config.Init.Entrypoint)
return string(bytes)
} else {
return ""
}
}()

cmd := func() string {
if len(machine.Config.Init.Cmd) > 0 {
bytes, _ := json.Marshal(machine.Config.Init.Cmd)
return string(bytes)
} else {
return ""
}
}()

obj := [][]string{
{
machine.ID,
Expand All @@ -90,8 +81,8 @@ func runMachineStatus(ctx context.Context) (err error) {
fmt.Sprint(machine.Config.Guest.MemoryMB),
machine.CreatedAt,
machine.UpdatedAt,
entrypoint,
cmd,
optJsonStrings(machine.Config.Init.Entrypoint),
optJsonStrings(machine.Config.Init.Cmd),
},
}

Expand Down

0 comments on commit d5c4f27

Please sign in to comment.