underbbs/cli/cli.go

76 lines
1.4 KiB
Go

package cli
import (
"errors"
"io/ioutil"
"log"
"strings"
"forge.lightcrystal.systems/nilix/underbbs/adapter"
"forge.lightcrystal.systems/nilix/underbbs/models"
)
func Process(gsettings models.GlobalSettings, args ...string) error {
adapterName := args[0]
args = args[1:]
var s *models.Settings
for _, x := range gsettings.Adapters {
if x.Nickname == adapterName {
s = &x
break
}
}
if s == nil {
return errors.New("given adapter " + adapterName + " is not in the config file")
}
// 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{}
default:
break
}
a.Init(*s, nil)
// process remaining args and execute
switch args[0] {
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)
default:
log.Print(args)
}
return nil
}