mirror of
https://codeberg.org/hyperreal/go-torrent-helper
synced 2024-11-01 16:53:09 +01:00
224 lines
4.3 KiB
Go
224 lines
4.3 KiB
Go
package command
|
|
|
|
import (
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/hekmon/transmissionrpc"
|
|
"tildegit.org/hyperreal/go-torrent-helper/almalinux"
|
|
"tildegit.org/hyperreal/go-torrent-helper/debian"
|
|
"tildegit.org/hyperreal/go-torrent-helper/fedora"
|
|
"tildegit.org/hyperreal/go-torrent-helper/nixos"
|
|
"tildegit.org/hyperreal/go-torrent-helper/parrot"
|
|
"tildegit.org/hyperreal/go-torrent-helper/qubes"
|
|
"tildegit.org/hyperreal/go-torrent-helper/rocky"
|
|
)
|
|
|
|
type Distro interface {
|
|
AddNewTorrents(client *transmissionrpc.Client) error
|
|
RemoveOldTorrents(client *transmissionrpc.Client) error
|
|
}
|
|
|
|
func NewAddCmd() *AddCmd {
|
|
addCmd := &AddCmd{
|
|
fs: flag.NewFlagSet("add", flag.ExitOnError),
|
|
}
|
|
|
|
addCmd.fs.StringVar(&addCmd.distro, "distro", "", "Distro to add torrents for")
|
|
addCmd.fs.StringVar(&addCmd.relver, "relver", "", "Release version to add")
|
|
|
|
return addCmd
|
|
}
|
|
|
|
func NewRemoveCmd() *RemoveCmd {
|
|
removeCmd := &RemoveCmd{
|
|
fs: flag.NewFlagSet("remove", flag.ExitOnError),
|
|
}
|
|
|
|
removeCmd.fs.StringVar(&removeCmd.distro, "distro", "", "Distro to remove torrents for")
|
|
removeCmd.fs.StringVar(&removeCmd.relver, "relver", "", "Release version to remove")
|
|
|
|
return removeCmd
|
|
}
|
|
|
|
func NewListCmd() *ListCmd {
|
|
listCmd := &ListCmd{
|
|
fs: flag.NewFlagSet("list", flag.ExitOnError),
|
|
}
|
|
|
|
listCmd.fs.Bool("list", false, "List supported distributions")
|
|
|
|
return listCmd
|
|
}
|
|
|
|
type AddCmd struct {
|
|
fs *flag.FlagSet
|
|
|
|
distro string
|
|
relver string
|
|
}
|
|
|
|
type RemoveCmd struct {
|
|
fs *flag.FlagSet
|
|
|
|
distro string
|
|
relver string
|
|
}
|
|
|
|
type ListCmd struct {
|
|
fs *flag.FlagSet
|
|
}
|
|
|
|
func (a *AddCmd) Name() string {
|
|
return a.fs.Name()
|
|
}
|
|
|
|
func (r *RemoveCmd) Name() string {
|
|
return r.fs.Name()
|
|
}
|
|
|
|
func (l *ListCmd) Name() string {
|
|
return l.fs.Name()
|
|
}
|
|
|
|
func (a *AddCmd) Init(args []string) error {
|
|
return a.fs.Parse(args)
|
|
}
|
|
|
|
func (r *RemoveCmd) Init(args []string) error {
|
|
return r.fs.Parse(args)
|
|
}
|
|
|
|
func (l *ListCmd) Init(args []string) error {
|
|
return l.fs.Parse(args)
|
|
}
|
|
|
|
func (a *AddCmd) Run(d Distro) error {
|
|
client, err := transmissionrpc.New(os.Getenv("TRANSMISSION_RPC_URL"), "", "", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := d.AddNewTorrents(client); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *RemoveCmd) Run(d Distro) error {
|
|
client, err := transmissionrpc.New(os.Getenv("TRANSMISSION_RPC_URL"), "", "", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := d.RemoveOldTorrents(client); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *ListCmd) Run(d Distro) error {
|
|
supportedDistros := []string{
|
|
"AlmaLinux",
|
|
"Debian",
|
|
"Fedora",
|
|
"NixOS",
|
|
"Parrot OS",
|
|
"Qubes OS",
|
|
"Rocky Linux",
|
|
}
|
|
|
|
fmt.Println("Supported distributions:")
|
|
for _, v := range supportedDistros {
|
|
fmt.Printf("* %s\n", v)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type Runner interface {
|
|
Init([]string) error
|
|
Run(d Distro) error
|
|
Name() string
|
|
}
|
|
|
|
func Root(args []string) error {
|
|
if len(args) < 1 {
|
|
return errors.New("You must pass a sub-command")
|
|
}
|
|
|
|
cmds := []Runner{
|
|
NewAddCmd(),
|
|
NewRemoveCmd(),
|
|
NewListCmd(),
|
|
}
|
|
|
|
subcmd := os.Args[1]
|
|
|
|
for _, cmd := range cmds {
|
|
if cmd.Name() == subcmd {
|
|
|
|
if subcmd == "list" {
|
|
return cmd.Run(nil)
|
|
}
|
|
|
|
var distro, relver string
|
|
|
|
if len(os.Args[2:]) == 0 {
|
|
return fmt.Errorf("See -h for usage options")
|
|
}
|
|
|
|
cmd.Init(os.Args[2:])
|
|
|
|
if len(os.Args[4:]) == 0 && subcmd == "remove" {
|
|
relver = "*"
|
|
} else if len(os.Args[4:]) == 0 && subcmd == "add" {
|
|
return fmt.Errorf("Please enter a release version")
|
|
} else {
|
|
relver = os.Args[5]
|
|
}
|
|
|
|
distro = os.Args[3]
|
|
|
|
switch distro {
|
|
case "almalinux":
|
|
a := &almalinux.AlmaLinux{NameSubstr: "AlmaLinux", Relver: relver}
|
|
return cmd.Run(a)
|
|
|
|
case "debian":
|
|
d := &debian.Debian{NameSubstr: "debian", Relver: relver}
|
|
return cmd.Run(d)
|
|
|
|
case "fedora":
|
|
f := &fedora.Fedora{NameSubstr: "Fedora", Relver: relver}
|
|
return cmd.Run(f)
|
|
|
|
case "nixos":
|
|
n := &nixos.Nixos{NameSubstr: "nixos"}
|
|
return cmd.Run(n)
|
|
|
|
case "parrot":
|
|
p := &parrot.Parrot{NameSubstr: "Parrot", Relver: relver}
|
|
return cmd.Run(p)
|
|
|
|
case "qubes":
|
|
q := &qubes.Qubes{NameSubstr: "Qubes", Relver: relver}
|
|
return cmd.Run(q)
|
|
|
|
case "rocky":
|
|
r := &rocky.Rocky{NameSubstr: "Rocky", Relver: relver}
|
|
return cmd.Run(r)
|
|
|
|
default:
|
|
return fmt.Errorf("Unknown distro: %s", distro)
|
|
}
|
|
}
|
|
}
|
|
|
|
return fmt.Errorf("Unknown subcommand: %s", subcmd)
|
|
}
|