underbbs/cli/cli.go

72 lines
1.3 KiB
Go
Raw Normal View History

2024-10-02 01:49:13 +00:00
package cli
import (
"encoding/json"
2024-10-03 00:54:14 +00:00
"errors"
2024-10-02 01:49:13 +00:00
"io/ioutil"
"log"
"forge.lightcrystal.systems/lightcrystal/underbbs/adapter"
"forge.lightcrystal.systems/lightcrystal/underbbs/models"
)
func Process(args ...string) error {
// allocate storage for the settings array
var settings []models.Settings
2024-10-03 00:54:14 +00:00
var s *models.Settings
if len(args) < 3 {
return errors.New("CLI requires at least 3 args: ADAPTER ACTION DATA...")
}
2024-10-02 01:49:13 +00:00
// get adapter from first arg
adapterName := args[0]
args = args[1:]
// get config from config fle based on adapter
content, err := ioutil.ReadFile("./config.json")
if err != nil {
return err
}
err = json.Unmarshal(content, settings)
if err != nil {
return err
}
for _, x := range settings {
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{}
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":
a.Do(args[1], args[2:])
2024-10-02 01:49:13 +00:00
default:
log.Print(args)
}
return nil
}