Skip to content

Commit

Permalink
add 'run' command which accepts the whole ptool cmdline as single arg
Browse files Browse the repository at this point in the history
  • Loading branch information
sagan committed Jan 29, 2024
1 parent 471fdbe commit ec41dae
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/alias/alias.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package versioncmd
package alias

import (
"fmt"
Expand Down
1 change: 1 addition & 0 deletions cmd/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
_ "github.com/sagan/ptool/cmd/removetags"
_ "github.com/sagan/ptool/cmd/removetrackers"
_ "github.com/sagan/ptool/cmd/resume"
_ "github.com/sagan/ptool/cmd/run"
_ "github.com/sagan/ptool/cmd/search"
_ "github.com/sagan/ptool/cmd/setcategory"
_ "github.com/sagan/ptool/cmd/setsavepath"
Expand Down
10 changes: 5 additions & 5 deletions cmd/iyuu/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func IyuuApiHash(token string, infoHashes []string) (map[string][]IyuuTorrentInf
return infoHashes[i] < infoHashes[j]
})
hash, _ := json.Marshal(&infoHashes)
apiUrl := util.ParseRelativeUrl("index.php?s=App.Api.Hash", config.Get().IyuuDomain)
apiUrl := util.ParseRelativeUrl("index.php?s=App.Api.Hash", config.Get().GetIyuuDomain())
data := url.Values{
"sign": {token},
"timestamp": {fmt.Sprint(util.Now())},
Expand All @@ -114,14 +114,14 @@ func IyuuApiHash(token string, infoHashes []string) (map[string][]IyuuTorrentInf
}

func IyuuApiGetUser(token string) (data map[string]any, err error) {
err = util.FetchJson(util.ParseRelativeUrl("index.php?s=App.Api.GetUser&sign="+token, config.Get().IyuuDomain), &data, nil)
err = util.FetchJson(util.ParseRelativeUrl("index.php?s=App.Api.GetUser&sign="+token, config.Get().GetIyuuDomain()), &data, nil)
return
}

func IyuuApiSites(token string) ([]IyuuApiSite, error) {
resData := &IyuuApiSitesResponse{}
err := util.FetchJson(util.ParseRelativeUrl("index.php?s=App.Api.Sites&version="+
IYUU_VERSION+"&sign="+token, config.Get().IyuuDomain), resData, nil)
IYUU_VERSION+"&sign="+token, config.Get().GetIyuuDomain()), resData, nil)
if err != nil {
return nil, err
}
Expand All @@ -133,7 +133,7 @@ func IyuuApiSites(token string) ([]IyuuApiSite, error) {

func IyuuApiBind(token string, site string, uid int64, passkey string) (map[string]any, error) {
apiUrl := util.ParseRelativeUrl("index.php?s=App.Api.Bind&token="+token+
"&site="+site+"&id="+fmt.Sprint(uid)+"&passkey="+util.Sha1String(passkey), config.Get().IyuuDomain)
"&site="+site+"&id="+fmt.Sprint(uid)+"&passkey="+util.Sha1String(passkey), config.Get().GetIyuuDomain())

resData := &IyuuApiResponse{}
err := util.FetchJson(apiUrl, &resData, nil)
Expand All @@ -147,7 +147,7 @@ func IyuuApiBind(token string, site string, uid int64, passkey string) (map[stri
}

func IyuuApiGetRecommendSites() ([]IyuuApiRecommendSite, error) {
apiUrl := util.ParseRelativeUrl("index.php?s=App.Api.GetRecommendSites", config.Get().IyuuDomain)
apiUrl := util.ParseRelativeUrl("index.php?s=App.Api.GetRecommendSites", config.Get().GetIyuuDomain())

var resData *IyuuGetRecommendSitesResponse
err := util.FetchJson(apiUrl, &resData, nil)
Expand Down
35 changes: 35 additions & 0 deletions cmd/run/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package run

import (
"fmt"
"os"

"github.com/google/shlex"
"github.com/spf13/cobra"

"github.com/sagan/ptool/cmd"
)

var command = &cobra.Command{
Use: "run {cmdline}",
Short: `Run cmdline. Accept the whole cmdline as single arg, e.g.: 'ptool run "status local -t"'.`,
Long: `Run cmdline. Accept the whole cmdline as single arg, e.g.: 'ptool run "status local -t"'.`,
DisableFlagParsing: true,
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: run,
}

func init() {
cmd.RootCmd.AddCommand(command)
}

func run(_ *cobra.Command, args []string) error {
cmdline := args[0]
cmdlineArgs, err := shlex.Split(cmdline)
if err != nil {
return fmt.Errorf("failed to parse cmdline '%s': %v", cmdline, err)
}
os.Args = append([]string{os.Args[0]}, cmdlineArgs...)
fmt.Printf("Run cmdline: %v\n", os.Args[1:])
return cmd.RootCmd.Execute()
}
10 changes: 7 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ func Get() *ConfigStruct {
if err != nil {
configData = &ConfigStruct{}
}
if configData.IyuuDomain == "" {
configData.IyuuDomain = DEFAULT_IYUU_DOMAIN
}
if configData.ShellMaxSuggestions == 0 {
configData.ShellMaxSuggestions = DEFAULT_SHELL_MAX_SUGGESTIONS
} else if configData.ShellMaxSuggestions < 0 {
Expand Down Expand Up @@ -550,3 +547,10 @@ func (configData *ConfigStruct) UpdateSitesDerivative() {
return !s.Disabled
})
}

func (configData *ConfigStruct) GetIyuuDomain() string {
if configData.IyuuDomain == "" {
return DEFAULT_IYUU_DOMAIN
}
return configData.IyuuDomain
}

0 comments on commit ec41dae

Please sign in to comment.