From 150cee16e8e6a5e1f4332e9de9395eb2cc719792 Mon Sep 17 00:00:00 2001 From: Mike Degatano Date: Thu, 7 Sep 2023 21:43:04 -0400 Subject: [PATCH] Add freeze/thaw apis to cli --- cmd/backups_freeze.go | 49 +++++++++++++++++++++++++++++++++++++++++++ cmd/backups_thaw.go | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 cmd/backups_freeze.go create mode 100644 cmd/backups_thaw.go diff --git a/cmd/backups_freeze.go b/cmd/backups_freeze.go new file mode 100644 index 00000000..8189c80c --- /dev/null +++ b/cmd/backups_freeze.go @@ -0,0 +1,49 @@ +package cmd + +import ( + "fmt" + + helper "github.com/home-assistant/cli/client" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var backupFreezeCmd = &cobra.Command{ + Use: "freeze", + Aliases: []string{"frz"}, + Short: "Freeze supervisor for external backup", + Long: ` +This command tells Supervisor to prepare Home Assistant and addons for a backup +or snapshot taken by external software. Caller should call thaw when done.`, + Example: ` + ha backups freeze --timeout 300`, + ValidArgsFunction: cobra.NoFileCompletions, + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + log.WithField("args", args).Debug("backups freeze") + + section := "backups" + command := "freeze" + + options := make(map[string]interface{}) + + timeout, err := cmd.Flags().GetInt("timeout") + if timeout != 0 && err == nil && cmd.Flags().Changed("timeout") { + options["timeout"] = timeout + } + + resp, err := helper.GenericJSONPost(section, command, options) + if err != nil { + fmt.Println(err) + ExitWithError = true + } else { + ExitWithError = !helper.ShowJSONResponse(resp) + } + }, +} + +func init() { + backupFreezeCmd.Flags().Int("timeout", 0, "Seconds before freeze times out and thaw begins") + backupFreezeCmd.RegisterFlagCompletionFunc("timeout", cobra.NoFileCompletions) + backupsCmd.AddCommand(backupFreezeCmd) +} diff --git a/cmd/backups_thaw.go b/cmd/backups_thaw.go new file mode 100644 index 00000000..03f8f9ea --- /dev/null +++ b/cmd/backups_thaw.go @@ -0,0 +1,40 @@ +package cmd + +import ( + "fmt" + + helper "github.com/home-assistant/cli/client" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var backupsThawCmd = &cobra.Command{ + Use: "thaw", + Aliases: []string{"th"}, + Short: "Thaw supervisor after an external backup", + Long: ` +End a freeze initiated by the freeze command after an external backup or snapshot +has completed.`, + Example: ` + ha backups thaw`, + ValidArgsFunction: cobra.NoFileCompletions, + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + log.WithField("args", args).Debug("backups thaw") + + section := "backups" + command := "thaw" + + resp, err := helper.GenericJSONPost(section, command, nil) + if err != nil { + fmt.Println(err) + ExitWithError = true + } else { + ExitWithError = !helper.ShowJSONResponse(resp) + } + }, +} + +func init() { + backupsCmd.AddCommand(backupsThawCmd) +}