Skip to content

Commit

Permalink
Add freeze/thaw apis to cli (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdegat01 authored Sep 13, 2023
1 parent c640673 commit 9bdadbf
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
49 changes: 49 additions & 0 deletions cmd/backups_freeze.go
Original file line number Diff line number Diff line change
@@ -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)
}
40 changes: 40 additions & 0 deletions cmd/backups_thaw.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9bdadbf

Please sign in to comment.