Honestly why is even the flag package but whatever

This commit is contained in:
Jeffrey Serio 2023-06-25 06:29:13 -05:00
parent c0380f9977
commit 77e94969d6
4 changed files with 101 additions and 10 deletions

View File

@ -2,8 +2,6 @@
This is a helper program to add and remove torrents from a transmission-daemon instance.
WIP: Other people should probably not use this yet, as there are still some rough edges I need to smooth out with regard to command-line argument handling and removing torrents functionality.
To use, make sure `TRANSMISSION_RPC_URL` is exported in the shell:
#+begin_src shell
export TRANSMISSION_RPC_URL=10.0.0.42
@ -27,4 +25,7 @@ go-torrent-helper add -distro rocky -relver 9.2
# Remove older Rocky Linux torrents
go-torrent-helper remove -distro rocky -relver 8.1
# If not -relver flag is supplied for the remove subcommand, remove all torrents for given distro
go-torrent-helper remove -distro debian
#+end_src

View File

@ -43,6 +43,16 @@ func NewRemoveCmd() *RemoveCmd {
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
@ -57,6 +67,10 @@ type RemoveCmd struct {
relver string
}
type ListCmd struct {
fs *flag.FlagSet
}
func (a *AddCmd) Name() string {
return a.fs.Name()
}
@ -65,6 +79,10 @@ 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)
}
@ -73,6 +91,10 @@ 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 {
@ -99,6 +121,25 @@ func (r *RemoveCmd) Run(d Distro) error {
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
@ -113,16 +154,35 @@ func Root(args []string) error {
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:])
distro := os.Args[3]
relver := os.Args[5]
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":

View File

@ -3,6 +3,7 @@ package common
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
@ -41,21 +42,45 @@ func GetResponse(url string) ([]byte, error) {
return dataInBytes, nil
}
func values[M ~map[K]V, K comparable, V any](m M) []V {
r := make([]V, 0, len(m))
for _, v := range m {
r = append(r, v)
}
return r
}
func keys[M ~map[K]V, K comparable, V any](m M) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
func RemoveTorrents(nameSubstr string, relver string, transmissionbt *transmissionrpc.Client) error {
var oldTorrents []int64
torrents, err := transmissionbt.TorrentGet([]string{"id", "name"}, nil)
if err != nil {
return err
} else {
for _, torrent := range torrents {
}
torrentsMap := map[string]int64{}
for _, torrent := range torrents {
if relver != "*" {
if strings.Contains(*torrent.Name, nameSubstr) && strings.Contains(*torrent.Name, relver) {
oldTorrents = append(oldTorrents, *torrent.ID)
torrentsMap[*torrent.Name] = *torrent.ID
}
} else {
if strings.Contains(*torrent.Name, nameSubstr) {
torrentsMap[*torrent.Name] = *torrent.ID
}
}
}
rmPayload := &transmissionrpc.TorrentRemovePayload{
IDs: oldTorrents,
IDs: values(torrentsMap),
DeleteLocalData: false,
}
@ -63,5 +88,9 @@ func RemoveTorrents(nameSubstr string, relver string, transmissionbt *transmissi
return err
}
for _, key := range keys(torrentsMap) {
log.Printf("%s removed\n", key)
}
return nil
}

View File

@ -10,6 +10,7 @@ import (
type Nixos struct {
NameSubstr string
Relver string
URL string
}
@ -49,7 +50,7 @@ func (n Nixos) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
}
func (n Nixos) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
if err := common.RemoveTorrents(n.NameSubstr, "", transmissionbt); err != nil {
if err := common.RemoveTorrents(n.NameSubstr, n.Relver, transmissionbt); err != nil {
return err
}