diff --git a/cmd/pause.go b/cmd/pause.go new file mode 100644 index 0000000..4fed1e6 --- /dev/null +++ b/cmd/pause.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/ludviglundgren/qbittorrent-cli/internal/config" + "github.com/ludviglundgren/qbittorrent-cli/pkg/qbittorrent" + + "github.com/spf13/cobra" +) + +// RunPause cmd to pause torrents +func RunPause() *cobra.Command { + var command = &cobra.Command{ + Use: "pause", + Short: "Pause all torrents", + Long: `Pause all torrents`, + } + command.Run = func(cmd *cobra.Command, args []string) { + qbtSettings := qbittorrent.Settings{ + Hostname: config.Qbit.Host, + Port: config.Qbit.Port, + Username: config.Qbit.Login, + Password: config.Qbit.Password, + } + qb := qbittorrent.NewClient(qbtSettings) + + err := qb.Login() + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: connection failed: %v\n", err) + os.Exit(1) + } + + err = qb.Pause(nil) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: could not pause torrents %v\n", err) + os.Exit(1) + } + } + + return command +} diff --git a/cmd/qbt/main.go b/cmd/qbt/main.go index 2cf48d1..2e08d2d 100644 --- a/cmd/qbt/main.go +++ b/cmd/qbt/main.go @@ -30,6 +30,8 @@ Documentation is available at https://github.com/ludviglundgren/qbittorrent-cli` rootCmd.AddCommand(cmd.RunList()) rootCmd.AddCommand(cmd.RunAdd()) rootCmd.AddCommand(cmd.RunImport()) + rootCmd.AddCommand(cmd.RunPause()) + rootCmd.AddCommand(cmd.RunResume()) if err := rootCmd.Execute(); err != nil { os.Exit(1) diff --git a/cmd/resume.go b/cmd/resume.go new file mode 100644 index 0000000..181b18d --- /dev/null +++ b/cmd/resume.go @@ -0,0 +1,44 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/ludviglundgren/qbittorrent-cli/internal/config" + "github.com/ludviglundgren/qbittorrent-cli/pkg/qbittorrent" + + "github.com/spf13/cobra" +) + +// RunResume cmd to resume torrents +func RunResume() *cobra.Command { + var command = &cobra.Command{ + Use: "resume", + Short: "Resume all torrents", + Long: `Resume all torrents`, + } + + command.Run = func(cmd *cobra.Command, args []string) { + qbtSettings := qbittorrent.Settings{ + Hostname: config.Qbit.Host, + Port: config.Qbit.Port, + Username: config.Qbit.Login, + Password: config.Qbit.Password, + } + qb := qbittorrent.NewClient(qbtSettings) + + err := qb.Login() + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: connection failed: %v\n", err) + os.Exit(1) + } + + err = qb.Resume(nil) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: could not resume torrents %v\n", err) + os.Exit(1) + } + } + + return command +} diff --git a/pkg/qbittorrent/methods.go b/pkg/qbittorrent/methods.go index af5a3db..e0f7433 100644 --- a/pkg/qbittorrent/methods.go +++ b/pkg/qbittorrent/methods.go @@ -205,3 +205,51 @@ func (c *Client) ReAnnounceTorrents(hashes []string) error { return nil } + +func (c *Client) Pause(hashes []string) error { + v := url.Values{} + encodedHashes := "all" + + if len(hashes) > 0 { + // Add hashes together with | separator + hv := strings.Join(hashes, "|") + v.Add("hashes", hv) + + encodedHashes = v.Encode() + } + + resp, err := c.get("torrents/pause?"+encodedHashes, nil) + if err != nil { + log.Fatalf("error pausing torrents: %v", err) + } else if resp.StatusCode != http.StatusOK { + return err + } + + defer resp.Body.Close() + + return nil +} + +func (c *Client) Resume(hashes []string) error { + v := url.Values{} + encodedHashes := "all" + + if len(hashes) > 0 { + // Add hashes together with | separator + hv := strings.Join(hashes, "|") + v.Add("hashes", hv) + + encodedHashes = v.Encode() + } + + resp, err := c.get("torrents/resume?"+encodedHashes, nil) + if err != nil { + log.Fatalf("error resuming torrents: %v", err) + } else if resp.StatusCode != http.StatusOK { + return err + } + + defer resp.Body.Close() + + return nil +}