underbbs/cli/cli.go

77 lines
1.4 KiB
Go
Raw Normal View History

2024-10-02 01:49:13 +00:00
package cli
import (
2024-10-03 00:54:14 +00:00
"errors"
2024-10-02 01:49:13 +00:00
"io/ioutil"
"log"
"strings"
2024-10-02 01:49:13 +00:00
2024-12-07 02:30:41 +00:00
"forge.lightcrystal.systems/nilix/underbbs/adapter"
"forge.lightcrystal.systems/nilix/underbbs/models"
2024-10-02 01:49:13 +00:00
)
2025-01-05 03:45:22 +00:00
func Process(gsettings models.GlobalSettings, args ...string) error {
2024-10-02 01:49:13 +00:00
adapterName := args[0]
args = args[1:]
2025-01-05 03:45:22 +00:00
var s *models.Settings
2024-10-02 01:49:13 +00:00
2025-01-05 03:45:22 +00:00
for _, x := range gsettings.Adapters {
2024-10-02 01:49:13 +00:00
if x.Nickname == adapterName {
2024-10-03 00:54:14 +00:00
s = &x
2024-10-02 01:49:13 +00:00
break
}
}
2024-10-03 00:54:14 +00:00
if s == nil {
return errors.New("given adapter " + adapterName + " is not in the config file")
}
2024-10-02 01:49:13 +00:00
// instantiate adapter with config
var a adapter.Adapter
switch s.Protocol {
case "nostr":
a = &adapter.NostrAdapter{}
case "mastodon":
a = &adapter.MastoAdapter{}
case "misskey":
a = &adapter.MisskeyAdapter{}
case "honk":
a = &adapter.HonkAdapter{}
2024-10-02 01:49:13 +00:00
default:
break
}
2024-10-03 00:54:14 +00:00
a.Init(*s, nil)
2024-10-02 01:49:13 +00:00
// process remaining args and execute
switch args[0] {
2024-10-03 00:54:14 +00:00
case "fetch":
a.Fetch(args[1], args[2:])
case "do":
data := map[string]string{}
for _, a := range args[2:] {
if !strings.Contains(a, "=") {
return errors.New("args are in the form KEY=VALUE")
} else {
aa := strings.Split(a, "=")
k := aa[0]
v := strings.Join(aa[1:], "=")
if k == "file" {
b, err := ioutil.ReadFile(v)
if err != nil {
return err
}
v = string(b)
if err != nil {
return err
}
}
data[k] = v
}
}
a.Do(args[1], data)
2024-10-02 01:49:13 +00:00
default:
log.Print(args)
}
return nil
}