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

Implement label.get_labels, core.enable_plugin and core.disable_plugin #40

Merged
merged 15 commits into from Dec 26, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -72,8 +72,8 @@ Both deluge v2.0+ and v1.3+ are supported with the two different constructors `N
* [ ] `core.connect_peer`
* [x] `core.create_account`
* [ ] `core.create_torrent`
* [ ] `core.disable_plugin`
* [ ] `core.enable_plugin`
* [x] `core.disable_plugin`
* [x] `core.enable_plugin`
* [x] `core.force_reannounce`
* [ ] `core.force_recheck`
* [ ] `core.get_auth_levels_mappings`
Expand Down Expand Up @@ -146,7 +146,7 @@ Plugins can be used by calling the relative method and checking if the result is

* [x] `label.add`
* [ ] `label.get_config`
* [ ] `label.get_labels`
* [x] `label.get_labels`
* [ ] `label.get_options`
* [x] `label.remove`
* [ ] `label.set_config`
Expand Down
44 changes: 43 additions & 1 deletion delugecli/cli.go
Expand Up @@ -36,16 +36,19 @@ var (
listTorrents bool
listAvailablePlugins bool
listEnabledPlugins bool
enablePlugin string
disablePlugin string
listAccounts bool
torrentHash string
setLabel string
addLabel string
removeLabel string
getLabels bool
listLabels bool
v2daemon bool
free bool
testListenPort bool
sessionStatus bool
sessionStatus bool

fs = flag.NewFlagSet("default", flag.ContinueOnError)
)
Expand All @@ -72,6 +75,8 @@ func init() {
fs.BoolVar(&listEnabledPlugins, "P", false, "List enabled plugins")
fs.BoolVar(&listAvailablePlugins, "list-available-plugins", false, "List available plugins")
fs.BoolVar(&listAvailablePlugins, "A", false, "List available plugins")
fs.StringVar(&enablePlugin, "enable-plugin", "", "Enable a plugin")
fs.StringVar(&disablePlugin, "disable-plugin", "", "Disable a plugin")

fs.StringVar(&torrentHash, "torrent", "", "Operate on specified torrent hash")
fs.StringVar(&torrentHash, "t", "", "Operate on specified torrent hash")
Expand All @@ -81,6 +86,7 @@ func init() {
fs.StringVar(&addLabel, "c", "", "Add label on torrent")
fs.StringVar(&removeLabel, "remove-label", "", "Remove label on torrent")
fs.StringVar(&removeLabel, "r", "", "Remove label on torrent")
fs.BoolVar(&getLabels, "get-labels", false, "List all labels")
fs.BoolVar(&listLabels, "list-labels", false, "List all torrents' labels")
fs.BoolVar(&listLabels, "g", false, "List all torrents' labels")

Expand Down Expand Up @@ -205,6 +211,22 @@ func main() {
fmt.Println("enabled plugins:", plugins)
}

if enablePlugin != "" {
err := deluge.EnablePlugin(enablePlugin)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: enable plugin %s: %v\n", enablePlugin, err)
os.Exit(5)
}
}

if disablePlugin != "" {
err := deluge.DisablePlugin(disablePlugin)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: disable plugin %s: %v\n", disablePlugin, err)
os.Exit(5)
}
}

if setLabel != "" {
if torrentHash == "" {
fmt.Fprintf(os.Stderr, "ERROR: no torrent hash specified\n")
Expand Down Expand Up @@ -256,6 +278,26 @@ func main() {
}
}

if getLabels {
p, err := deluge.LabelPlugin()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: label plugin: %v\n", err)
os.Exit(5)
}

labels, err := p.GetLabels()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: retrieving labels: %v\n", err)
os.Exit(5)
}

je := json.NewEncoder(os.Stdout)
je.SetIndent("", "\t")
if err := je.Encode(labels); err != nil {
os.Exit(7)
}
}

if listLabels {
p, err := deluge.LabelPlugin()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions delugeclient.go
Expand Up @@ -85,6 +85,8 @@ type DelugeClient interface {
ForceReannounce(ids []string) error
GetAvailablePlugins() ([]string, error)
GetEnabledPlugins() ([]string, error)
EnablePlugin(name string) error
DisablePlugin(name string) error
TestListenPort() (bool, error)
GetListenPort() (uint16, error)
GetSessionStatus() (*SessionStatus, error)
Expand Down
40 changes: 40 additions & 0 deletions integration/integration_plugins_test.go
@@ -0,0 +1,40 @@
// +build integration

package main

import (
delugeclient "github.com/gdm85/go-libdeluge"
"testing"
)


func enablePlugin(t *testing.T, name string) {
err := deluge.EnablePlugin(name)
if err != nil {
t.Fatal(err)
}
}

func disablePlugin(t *testing.T, name string) {
err := deluge.DisablePlugin(name)
if err != nil {
t.Fatal(err)
}

// cleanup DebugServerResponses after plugin disable
c.DebugServerResponses = nil
}

func TestLabelPluginGetLabels(t *testing.T) {
enablePlugin(t, "Label")
defer disablePlugin(t, "Label")

var labelPlugin = &delugeclient.LabelPlugin{Client: c}

_, err := labelPlugin.GetLabels()
if err != nil {
t.Fatal(err)
}

printServerResponse(t, "LabelPlugin.GetLabels")
}
22 changes: 19 additions & 3 deletions integration/integration_test.go
Expand Up @@ -121,6 +121,22 @@ func TestGetEnabledPlugins(t *testing.T) {
printServerResponse(t, "GetEnabledPlugins")
}

func TestEnablePlugin(t *testing.T) {
err := deluge.EnablePlugin("Label")
if err != nil {
t.Fatal(err)
}
printServerResponse(t, "EnablePlugin")
}

func TestDisablePlugin(t *testing.T) {
err := deluge.DisablePlugin("Label")
if err != nil {
t.Fatal(err)
}
printServerResponse(t, "DisablePlugin")
}

func TestAddMagnetAndCheckStatus(t *testing.T) {
torrentHash, err := deluge.AddTorrentMagnet(testMagnetURI, nil)
if err != nil {
Expand Down Expand Up @@ -193,12 +209,12 @@ func TestAddPauseAndRemoveTorrentFile(t *testing.T) {
}

func printServerResponse(t *testing.T, methodName string) {
if len(c.DebugServerResponses) != 1 {
panic("BUG: expected exactly one response")
if len(c.DebugServerResponses) == 0 {
panic("BUG: expected at least one response")
}

// store response for testing/development
buf := c.DebugServerResponses[0]
buf := c.DebugServerResponses[len(c.DebugServerResponses)-1]
if t != nil {
t.Logf("%s: received %d compressed bytes: %X\n", methodName, buf.Len(), buf.Bytes())
}
Expand Down
32 changes: 32 additions & 0 deletions methods.go
Expand Up @@ -458,6 +458,38 @@ func (c *Client) GetAvailablePlugins() ([]string, error) {
return c.rpcWithStringsResult("core.get_available_plugins")
}

// EnablePlugin enables the plugin with the given name.
func (c *Client) EnablePlugin(name string) error {
var args rencode.List
args.Add(name)

resp, err := c.rpc("core.enable_plugin", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}

return nil
}

// DisablePlugin disables the plugin with the given name.
func (c *Client) DisablePlugin(name string) error {
var args rencode.List
args.Add(name)

resp, err := c.rpc("core.disable_plugin", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}

return nil
}

func sliceToRencodeList(s []string) rencode.List {
var list rencode.List
for _, v := range s {
Expand Down
22 changes: 22 additions & 0 deletions methods_test.go
Expand Up @@ -87,6 +87,28 @@ func TestGetEnabledPlugins(t *testing.T) {
}
}

func TestEnablePlugin(t *testing.T) {
t.Parallel()

c := newMockClientV2(7, "789C3BCCC8E10C0003660110")

err := c.EnablePlugin("Label")
if err != nil {
t.Fatal(err)
}
}

func TestDisablePlugin(t *testing.T) {
t.Parallel()

c := newMockClientV2(8, "789C3BCCC8E90C0003680111")

err := c.DisablePlugin("Label")
if err != nil {
t.Fatal(err)
}
}

func TestKnownAccounts(t *testing.T) {
t.Parallel()

Expand Down
5 changes: 5 additions & 0 deletions plugins.go
Expand Up @@ -41,6 +41,11 @@ func (c *Client) LabelPlugin() (*LabelPlugin, error) {
return nil, nil
}

// GetLabels returns a list of the available labels that can be assigned to torrents.
func (p LabelPlugin) GetLabels() ([]string, error) {
return p.rpcWithStringsResult("label.get_labels")
}

// SetTorrentLabel adds or replaces the label for the specified torrent.
func (p LabelPlugin) SetTorrentLabel(hash, label string) error {
var args rencode.List
Expand Down
36 changes: 36 additions & 0 deletions plugins_test.go
@@ -0,0 +1,36 @@
package delugeclient

import "testing"

func TestLabelPlugin_GetLabels(t *testing.T) {
t.Parallel()

c := newMockClient(1, "789C3BCCC874A839B338BF29BF18001B7504BB")

plugin := &LabelPlugin{
Client: c.(*Client),
}

labels, err := plugin.GetLabels()
if err != nil {
t.Fatal(err)
}

expect := []string{
"iso",
"os",
}

if len(labels) != len(expect) {
t.Fatalf("mismatch in expected label count: expected %d, got %d", len(expect), len(labels))
}

for i := 0; i < len(labels); i++ {
expectLabel := expect[i]
givenLabel := labels[i]

if expectLabel != givenLabel {
t.Fatalf("wrong label: expected %s; got %s", expectLabel, givenLabel)
}
}
}