diff options
author | Joop Kiefte <ikojba@gmail.com> | 2021-12-10 18:57:39 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 18:57:39 +0000 |
commit | cc93575fe67dc63d90b4d1bd3f4479ecdcbdd1d6 (patch) | |
tree | c699247da590019025056ca1ba033f9b157d7aff /main.go | |
parent | 01016428cf4c1f72857680dbad1972aa3898e8ea (diff) |
Ready for first local tests
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 83 |
1 files changed, 60 insertions, 23 deletions
@@ -2,36 +2,73 @@ package main import ( + "bufio" "fmt" "os" - "strconv" - "strings" "git.kiefte.eu/lapingvino/infodump/message" + _ "modernc.org/sqlite" ) -// pubsub "github.com/libp2p/go-libp2p-pubsub" -// ipfs "github.com/ipfs/go-ipfs-api" +var LocalMessages = message.Messages{} + +// Readline reads from a buffered stdin and returns the line +func Readline() string { + reader := bufio.NewReader(os.Stdin) + line, _ := reader.ReadString('\n') + return line +} -// Main function: get the minimal Proof of Work for a message as the first argument, the message as the rest of the argument and return the stamp func main() { - // Check if the user provided a number and a message - if len(os.Args) < 3 { - fmt.Println("Please provide a number and a message") - os.Exit(1) - } - // Get the number of initial zeroes from the user - n, err := strconv.Atoi(os.Args[1]) - if err != nil { - fmt.Println("Please provide a number") - os.Exit(1) + // Set message IPFS client to use the local IPFS node + message.IPFSGateway = "http://localhost:5001" + + // Run a loop and present a menu to the user to read messages, write messages or quit the program + for { + // Present the user with a menu + fmt.Println("Welcome to Infodump") + fmt.Println("1. Read Messages") + fmt.Println("2. Write Messages") + fmt.Println("3. Sync messages") + fmt.Println("4. Quit") + fmt.Println("Enter your choice: ") + var choice int + fmt.Scanln(&choice) + switch choice { + case 1: + // Read the messages from PubSub topic "OLN" + fmt.Println("Reading messages...") + // TODO: Read messages from PubSub topic "OLN" + case 2: + // Write Messages + fmt.Println("Writing Messages") + // Get a message and an urgency from the user. + // The urgency is used to set the strength of the Proof of Work + fmt.Println("Enter a message: ") + m := Readline() + fmt.Println("Enter an urgency (higher is stronger but takes longer to produce): ") + var urgency int + fmt.Scanln(&urgency) + // Create a Message object + msg := message.New(m, urgency) + // Add the message to LocalMessages + LocalMessages.Add(msg) + case 3: + // Sync messages + fmt.Println("Syncing messages...") + // Upload the LocalMessages to IPFS + hash, err := LocalMessages.AddToIPFS() + if err != nil { + fmt.Println(err) + } + // TODO: Publish the hash to the PubSub topic "OLN" + fmt.Println("Hash: ", hash) + case 4: + // Quit + fmt.Println("Quitting") + os.Exit(0) + default: + fmt.Println("Invalid Choice") + } } - // Get the message from the user - msg := strings.Join(os.Args[2:], " ") - // Create a new message - m := message.New(msg, n) - // Print the stamp - fmt.Println(m.Stamp()) - // Print the message, nonce and number of leading zeroes - fmt.Println(m.Message, m.Nonce, m.Lead()) } |