all repos — underbbs @ 9195bba7eda7c68dc578581ace51da60a5967529

decentralized social media client

cli/cli.go (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cli

import (
	"encoding/json"
	"errors"
	"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
	var s *models.Settings

	if len(args) < 3 {
		return errors.New("CLI requires at least 3 args: ADAPTER ACTION DATA...")
	}

	// 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 {
			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{}
	default:
		break
	}
	a.Init(*s, nil)
	// process remaining args and execute

	switch args[0] {
	case "fetch":
		a.Fetch(args[1], args[2:])
	case "do":
		a.Do(args[1], args[2:])
	default:
		log.Print(args)
	}
	return nil
}