arnaucube
arnaucube/pad2ipfsPublic Self-hosted repository
pad2ipfs/pad2ipfs-cli/main.go
pad2ipfs-cli/main.goRaw
  1package main
  2
  3import (
  4	"bufio"
  5	"fmt"
  6	"os"
  7	"strings"
  8
  9	pad2ipfs ".."
 10	"github.com/fatih/color"
 11)
 12
 13const checkIcon = "\xE2\x9C\x94 "
 14
 15func main() {
 16	asciiart := `
 17                  _   ___    _____ _____  ______ _____
 18                 | | |__ \  |_   _|  __ \|  ____/ ____|
 19  _ __   __ _  __| |    ) |   | | | |__) | |__ | (___
 20 | '_ \ / _  |/ _  |   / /    | | |  ___/|  __| \___ \
 21 | |_) | (_| | (_| |  / /_   _| |_| |    | |    ____) |
 22 | .__/ \__,_|\__,_| |____| |_____|_|    |_|   |_____/  - cli
 23 | |
 24 |_|
 25		`
 26	color.Blue(asciiart)
 27	fmt.Println("							v0.0.1")
 28	color.Blue("https://github.com/arnaucode/pad2ipfs")
 29	fmt.Println("")
 30	fmt.Println("")
 31	fmt.Println("")
 32
 33	newcommand := bufio.NewReader(os.Stdin)
 34	fmt.Print("Please select command number")
 35	options := `
 36	1 - Pad Link to IPFS
 37	2 - IPFS hash to file
 38	0 - Exit cli
 39option to select: `
 40	for {
 41		fmt.Print(options)
 42
 43		option, _ := newcommand.ReadString('\n')
 44		option = strings.TrimSpace(option)
 45
 46		switch option {
 47		case "1":
 48			fmt.Println("selected 1 - Pad Link to IPFS")
 49			padlinkToIPFS()
 50			break
 51		case "2":
 52			fmt.Println("selected 2 - IPFS hash to file")
 53			hashToFile()
 54			break
 55		case "0":
 56			fmt.Println("selected 0 - exit cli")
 57			os.Exit(3)
 58			break
 59		default:
 60			fmt.Println("Invalid option")
 61			break
 62		}
 63	}
 64}
 65func padlinkToIPFS() {
 66	newcommand := bufio.NewReader(os.Stdin)
 67	fmt.Print("	Enter the pad link: ")
 68	link, _ := newcommand.ReadString('\n')
 69	link = strings.Replace(link, "\n", "", -1)
 70
 71	newcommand = bufio.NewReader(os.Stdin)
 72	formats := `	Available formats:
 73		- md (by default)
 74		- txt
 75		- html
 76		- pdf
 77		- odt`
 78	fmt.Println(formats)
 79	fmt.Print("	Enter the pad format: ")
 80	format, _ := newcommand.ReadString('\n')
 81	format = strings.Replace(format, "\n", "", -1)
 82	if format == "" {
 83		format = "md"
 84	}
 85	if format != "md" && format != "txt" && format != "html" && format != "pdf" && format != "odt" {
 86		fmt.Println("		wrong format, using md format")
 87		format = "md"
 88	}
 89
 90	hash, err := pad2ipfs.Add(link, format)
 91	if err != nil {
 92		color.Red(err.Error())
 93	} else {
 94		color.Green(checkIcon + "File added to IPFS network")
 95		fmt.Print("IPFS hash: ")
 96		color.Blue(hash)
 97	}
 98}
 99func hashToFile() {
100	newcommand := bufio.NewReader(os.Stdin)
101	fmt.Print("	Enter the IPFS hash: ")
102	hash, _ := newcommand.ReadString('\n')
103	hash = strings.Replace(hash, "\n", "", -1)
104	err := pad2ipfs.Get(hash, hash+".md")
105	if err != nil {
106		color.Red(err.Error())
107	} else {
108
109		color.Green(checkIcon + "File downloaded from IPFS network")
110		fmt.Print("File stored in: ")
111		color.Blue(pad2ipfs.GettedPads + "/" + hash + ".md")
112	}
113}