Skip to content

Commit

Permalink
Support move and alias in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mksong76 committed Nov 2, 2024
1 parent 98bc636 commit e1b0f7c
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -94,6 +95,26 @@ func argsToIDs(args []string) ([]int64, error) {
return ids, nil
}

func getLocation(vp *viper.Viper) (string, error) {
location := vp.GetString("location")
if len(location) == 0 {
return "", nil
}
if location[0] == '@' {
name := location[1:]
locations := vp.GetStringMapString("alias")
if len(locations) == 0 {
return "", errors.New("no alias information")
}
if alias, ok := locations[name]; ok {
return alias, nil
} else {
return "", fmt.Errorf("unknown location alias %s", name)
}
}
return location, nil
}

func NewAddCommand(vp *viper.Viper) *cobra.Command {
var detail bool
cmd := &cobra.Command{
Expand All @@ -103,7 +124,10 @@ func NewAddCommand(vp *viper.Viper) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
client := getClient(vp)
deleteTorrent := vp.GetBool("delete")
location := vp.GetString("location")
location, err := getLocation(vp)
if err != nil {
return err
}
for _, arg := range args {
var torrent *rpc.Torrent
var err error
Expand Down Expand Up @@ -143,9 +167,7 @@ func NewAddCommand(vp *viper.Viper) *cobra.Command {
flags := cmd.Flags()
flags.BoolVar(&detail, "detail", false, "Show details of added torrent")
flags.Bool("delete", false, "Delete torrent file on successful addition")
flags.String("location", "", "Location to put downloaded")
vp.BindPFlag("delete", flags.Lookup("delete"))
vp.BindPFlag("location", flags.Lookup("location"))
return cmd
}

Expand Down Expand Up @@ -213,6 +235,32 @@ func NewStartCommand(vp *viper.Viper) *cobra.Command {
return cmd
}

func NewMoveCommand(vp *viper.Viper) *cobra.Command {
cmd := &cobra.Command{
Use: "move",
Short: "Move download location to specified location",
RunE: func(cmd *cobra.Command, args []string) error {
client := getClient(vp)
ids, err := argsToIDs(args)
if err != nil {
return err
}
location, err := getLocation(vp)
if err != nil {
return err
}
for _, id := range ids {
err = client.TorrentSetLocation(id, location, true)
if err != nil {
return err
}
}
return nil
},
}
return cmd
}

func isDone(t *rpc.Torrent) bool {
if t.Status != nil && *t.Status == rpc.TorrentStatusStopped {
if t.LeftUntilDone != nil && *t.LeftUntilDone == 0 {
Expand Down Expand Up @@ -284,6 +332,7 @@ func main() {
flags.StringP("password", "w", "", "Transmission password")
flags.BoolP("https", "t", false, "Use TLS for connection")
flags.String("useragent", "TorrentCLI", "UserAgent name for HTTP Client")
flags.StringP("location", "l", "", "Location to put downloaded")

root.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
var config rpc.AdvancedConfig
Expand Down Expand Up @@ -346,6 +395,7 @@ func main() {
root.AddCommand(NewListCommand(vp))
root.AddCommand(NewStopCommand(vp))
root.AddCommand(NewStartCommand(vp))
root.AddCommand(NewMoveCommand(vp))
root.AddCommand(NewRemoveCommand(vp))
root.AddCommand(&cobra.Command{
Use: "save",
Expand Down

0 comments on commit e1b0f7c

Please sign in to comment.