From 373287b452f2d3cf27c9d32648c3393ca45ded3a Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Tue, 2 Apr 2024 12:30:08 +0200 Subject: [PATCH] feat(server): allow JSON & YAML output in reset-password (#716) This PR adds the ability to output the root password after a password reset in JSON or YAML format. Closes #715 --- internal/cmd/server/reset_password.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/cmd/server/reset_password.go b/internal/cmd/server/reset_password.go index f216fb5f..8942aabe 100644 --- a/internal/cmd/server/reset_password.go +++ b/internal/cmd/server/reset_password.go @@ -7,22 +7,28 @@ import ( "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/cmd/cmpl" + "github.com/hetznercloud/cli/internal/cmd/output" + "github.com/hetznercloud/cli/internal/cmd/util" "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) var ResetPasswordCmd = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { - return &cobra.Command{ - Use: "reset-password ", + cmd := &cobra.Command{ + Use: "reset-password [options] ", Short: "Reset the root password of a server", Args: cobra.ExactArgs(1), ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Server().Names)), TraverseChildren: true, DisableFlagsInUseLine: true, } + output.AddFlag(cmd, output.OptionJSON(), output.OptionYAML()) + return cmd }, Run: func(s state.State, cmd *cobra.Command, args []string) error { + outputFlags := output.FlagsForCommand(cmd) + idOrName := args[0] server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { @@ -41,6 +47,16 @@ var ResetPasswordCmd = base.Cmd{ return err } + if outputFlags.IsSet("json") || outputFlags.IsSet("yaml") { + schema := make(map[string]interface{}) + schema["root_password"] = result.RootPassword + if outputFlags.IsSet("json") { + return util.DescribeJSON(schema) + } else { + return util.DescribeYAML(schema) + } + } + cmd.Printf("Password of server %d reset to: %s\n", server.ID, result.RootPassword) return nil },