Skip to content

Commit

Permalink
feat(cli): add machine inspect command. redacts sensitive and hidden …
Browse files Browse the repository at this point in the history
…fields and then dumps the machine config as json
  • Loading branch information
pascalbreuninger committed Jul 12, 2024
1 parent b2ae08d commit 098a4b6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cmd/machine/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package machine

import (
"context"
"encoding/json"
"fmt"

"github.com/loft-sh/devpod/cmd/flags"
"github.com/loft-sh/devpod/pkg/config"
"github.com/loft-sh/devpod/pkg/provider"
"github.com/loft-sh/devpod/pkg/workspace"
"github.com/loft-sh/log"
"github.com/spf13/cobra"
)

type InspectCmd struct {
*flags.GlobalFlags
}

func NewInspectCmd(flags *flags.GlobalFlags) *cobra.Command {
cmd := &InspectCmd{
GlobalFlags: flags,
}
stopCmd := &cobra.Command{
Use: "inspect",
Short: "Inspects an existing machine",
RunE: func(_ *cobra.Command, args []string) error {
return cmd.Run(context.Background(), args)
},
}

return stopCmd
}

func (cmd *InspectCmd) Run(ctx context.Context, args []string) error {
devPodConfig, err := config.LoadConfig(cmd.Context, cmd.Provider)
if err != nil {
return err
}

machineClient, err := workspace.GetMachine(devPodConfig, args, log.Default)
if err != nil {
return err
}
p, err := provider.LoadProviderConfig(devPodConfig.DefaultContext, machineClient.Provider())
if err != nil {
return err
}

machineConfig := machineClient.MachineConfig()
for k := range machineConfig.Provider.Options {
optConfig := p.Options[k]
if optConfig.Hidden {
delete(machineConfig.Provider.Options, k)
continue
}

if optConfig.Password {
opt := machineConfig.Provider.Options[k]
opt.Value = "********"
}
}

out, err := json.MarshalIndent(machineConfig, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))

return nil
}
1 change: 1 addition & 0 deletions cmd/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ func NewMachineCmd(flags *flags.GlobalFlags) *cobra.Command {
machineCmd.AddCommand(NewStatusCmd(flags))
machineCmd.AddCommand(NewDeleteCmd(flags))
machineCmd.AddCommand(NewCreateCmd(flags))
machineCmd.AddCommand(NewInspectCmd(flags))
return machineCmd
}

0 comments on commit 098a4b6

Please sign in to comment.