Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add freeze/thaw apis to cli #422

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}