aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: d14b12506167a9a725e0d2bc5efea5a79848e9ae (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Infodump is a commandline social network that works over IPFS and communicates through PubSub
package main

import (
	"bufio"
	"fmt"
	"net/url"
	"os"

	"git.kiefte.eu/lapingvino/infodump/message"
	_ "modernc.org/sqlite"
)

// Readline reads from a buffered stdin and returns the line
func Readline() string {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	return line[:len(line)-1]
}

// MenuElements is a list of options for the menu, consisting of a description and a function
type MenuElements struct {
	Description string
	Function    func()
}

// Menu presents a menu to the user
func Menu(menu []MenuElements) {
	// Present the user with a menu
	for i, e := range menu {
		fmt.Println(i+1, e.Description)
	}
	fmt.Println("Enter your choice: ")
	var choice int
	fmt.Scanln(&choice)
	if choice > 0 && choice <= len(menu) {
		menu[choice-1].Function()
	} else {
		fmt.Println("Invalid Choice")
	}
}

func main() {
	fmt.Println("Welcome to Infodump")

	// Set message IPFS client to use the local IPFS node
	message.IPFSGateway = "http://localhost:5001"

	// If the first argument to the command is a valid link, use that for the IPFSGateway instead
	if len(os.Args) > 1 {
		u, err := url.Parse(os.Args[1])
		if err == nil {
			message.IPFSGateway = u.String()
		}
	}

	err := TestIPFSGateway(message.IPFSGateway)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Run a loop and present a menu to the user to
	// read messages
	// write messages
	// sync messages
	// set the IPFS gateway
	// set the database
	// configure the followed tags
	// quit the program
	for {
		// Check if the database is set, if so, show the followed tags
		if DB != nil {
			fmt.Println("Following tags:")
			for _, tag := range GetFollowedTags(DB) {
				fmt.Print(tag, " ")
				fmt.Println()
			}
		}
		// Present the user with a menu
		Menu([]MenuElements{
			{"Start OLN Listener", StartOLNListener},
			{"Read Messages", ReadMessages},
			{"Write Message", WriteMessage},
			{"Sync Messages", SyncMenu},
			{"Trim Messages", TrimMessages},
			{"Trim Database", TrimDatabase},
			{"Settings", SettingsMenu},
			{"Quit", func() { os.Exit(0) }},
		})

		// After the user has selected an option and before the user gets to chose again, ask to press enter to continue
		fmt.Println("Press enter to continue...")
		Readline()
	}
}

// A submenu for all settings
func SettingsMenu() {
	// Present the user with a menu
	Menu([]MenuElements{
		{"Set IPFS Gateway", SetIPFSGateway},
		{"Set Database", SetDatabase},
		{"Configure Followed Tags", ConfigureFollowedTags},
		{"Back", func() {}},
	})
}