go-torrent-helper/command/main.go

289 lines
5.9 KiB
Go
Raw Normal View History

2023-06-24 04:33:24 +02:00
package command
import (
"errors"
"flag"
"fmt"
"os"
2024-07-26 10:14:41 +02:00
"strconv"
2023-06-24 04:33:24 +02:00
2024-03-30 06:23:56 +01:00
"codeberg.org/hyperreal/go-torrent-helper/almalinux"
"codeberg.org/hyperreal/go-torrent-helper/debian"
"codeberg.org/hyperreal/go-torrent-helper/devuan"
"codeberg.org/hyperreal/go-torrent-helper/fedora"
"codeberg.org/hyperreal/go-torrent-helper/freebsd"
"codeberg.org/hyperreal/go-torrent-helper/kali"
2024-04-04 00:13:28 +02:00
"codeberg.org/hyperreal/go-torrent-helper/netbsd"
2024-03-30 06:23:56 +01:00
"codeberg.org/hyperreal/go-torrent-helper/nixos"
"codeberg.org/hyperreal/go-torrent-helper/parrot"
"codeberg.org/hyperreal/go-torrent-helper/qubes"
"codeberg.org/hyperreal/go-torrent-helper/rocky"
"codeberg.org/hyperreal/go-torrent-helper/tails"
2023-07-18 04:54:36 +02:00
"github.com/hekmon/transmissionrpc"
2023-06-24 04:33:24 +02:00
)
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
}
2023-06-24 04:33:24 +02:00
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
}
2023-06-24 04:33:24 +02:00
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()
}
2023-06-24 04:33:24 +02:00
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)
}
2023-06-24 04:33:24 +02:00
func (a *AddCmd) Run(d Distro) error {
2024-07-26 10:14:41 +02:00
txRpcUrl := os.Getenv("TX_RPC_URL")
txRpcUser := os.Getenv("TX_RPC_USER")
txRpcPass := os.Getenv("TX_RPC_PASS")
txRpcPort, err := strconv.ParseUint(os.Getenv("TX_RPC_PORT"), 10, 16)
if err != nil {
return err
}
txRpcUseHTTPS, err := strconv.ParseBool(os.Getenv("TX_RPC_USE_HTTPS"))
if err != nil {
return err
}
client, err := transmissionrpc.New(txRpcUrl, txRpcUser, txRpcPass,
&transmissionrpc.AdvancedConfig{
HTTPS: txRpcUseHTTPS,
Port: uint16(txRpcPort),
},
)
2023-06-24 04:33:24 +02:00
if err != nil {
return err
}
if err := d.AddNewTorrents(client); err != nil {
return err
}
return nil
}
func (r *RemoveCmd) Run(d Distro) error {
2024-07-26 10:14:41 +02:00
txRpcUrl := os.Getenv("TX_RPC_URL")
txRpcUser := os.Getenv("TX_RPC_USER")
txRpcPass := os.Getenv("TX_RPC_PASS")
txRpcPort, err := strconv.ParseUint(os.Getenv("TX_RPC_PORT"), 10, 16)
if err != nil {
return err
}
txRpcUseHTTPS, err := strconv.ParseBool(os.Getenv("TX_RPC_USE_HTTPS"))
if err != nil {
return err
}
client, err := transmissionrpc.New(txRpcUrl, txRpcUser, txRpcPass,
&transmissionrpc.AdvancedConfig{
HTTPS: txRpcUseHTTPS,
Port: uint16(txRpcPort),
},
)
2023-06-24 04:33:24 +02:00
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{
2023-07-19 04:48:09 +02:00
"almalinux",
"debian",
2023-12-11 09:03:48 +01:00
"devuan",
2023-07-19 04:48:09 +02:00
"fedora",
"freebsd",
2024-03-30 01:59:29 +01:00
"kali",
2024-04-04 00:13:28 +02:00
"netbsd",
2023-07-19 04:48:09 +02:00
"nixos",
"parrot",
"qubes",
"rocky",
2023-12-04 14:41:32 +01:00
"tails",
}
fmt.Println("Supported distributions:")
for _, v := range supportedDistros {
fmt.Printf("* %s\n", v)
}
return nil
}
2023-06-24 04:33:24 +02:00
type Runner interface {
Init([]string) error
Run(d Distro) error
Name() string
}
func Root(args []string) error {
if len(args) < 1 {
2024-07-26 10:14:41 +02:00
return errors.New("you must pass a sub-command")
2023-06-24 04:33:24 +02:00
}
cmds := []Runner{
NewAddCmd(),
NewRemoveCmd(),
NewListCmd(),
2023-06-24 04:33:24 +02:00
}
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 {
2024-07-26 10:14:41 +02:00
return fmt.Errorf("see -h for usage options")
}
2023-06-24 04:33:24 +02:00
cmd.Init(os.Args[2:])
if len(os.Args[4:]) == 0 && subcmd == "remove" {
relver = "*"
} else if len(os.Args[4:]) == 0 && subcmd == "add" {
2024-07-26 10:14:41 +02:00
return fmt.Errorf("please enter a release version")
} else {
relver = os.Args[5]
}
distro = os.Args[3]
2023-06-24 04:33:24 +02:00
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)
2023-12-11 09:03:48 +01:00
case "devuan":
dv := &devuan.Devuan{NameSubstr: "devuan", Relver: relver}
return cmd.Run(dv)
2023-06-24 04:33:24 +02:00
case "fedora":
f := &fedora.Fedora{NameSubstr: "Fedora", Relver: relver}
return cmd.Run(f)
2023-07-19 04:48:09 +02:00
case "freebsd":
fb := &freebsd.FreeBSD{NameSubstr: "FreeBSD", Relver: relver}
return cmd.Run(fb)
2024-03-30 01:59:29 +01:00
case "kali":
k := &kali.Kali{NameSubstr: "kali-linux", Relver: relver}
return cmd.Run(k)
2024-04-04 00:13:28 +02:00
case "netbsd":
nb := &netbsd.NetBSD{NameSubstr: "NetBSD", Relver: relver}
return cmd.Run(nb)
2023-06-24 04:33:24 +02:00
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)
2023-12-04 14:41:32 +01:00
case "tails":
t := &tails.Tails{NameSubstr: "Tails", Relver: relver}
return cmd.Run(t)
2023-06-24 04:33:24 +02:00
default:
2024-07-26 10:14:41 +02:00
return fmt.Errorf("unknown distro: %s", distro)
2023-06-24 04:33:24 +02:00
}
}
}
2024-07-26 10:14:41 +02:00
return fmt.Errorf("unknown subcommand: %s", subcmd)
2023-06-24 04:33:24 +02:00
}