added cli
arnaucode arnaucode@gmail.com
Wed, 04 Apr 2018 21:29:31 +0200
5 files changed,
134 insertions(+),
11 deletions(-)
M
README.md
→
README.md
@@ -5,18 +5,18 @@
Needs to have installed IPFS (https://ipfs.io), and the daemon running ('> ipfs daemon'). -### Install +## Install ``` go get github.com/arnaucode/pad2ipfs ``` -### Usage +## Usage The added pads are stored in 'addedPads' directory. The getted pads are stored in 'gettedPads' directory. -##### - Add +#### - Add Adds the content from a pad to IPFS ```go@@ -37,7 +37,7 @@ - odt
-##### - Get +#### - Get Gets the content from IPFS and stores it into a file ```go@@ -49,3 +49,10 @@ if err!=nil {
fmt.Println(err) } ``` + + + +### CLI +In the directory /pad2ipfs-cli is placed the cli to interact directly with the library from the command line. Here is a screenshot: + +
A
pad2ipfs-cli/main.go
@@ -0,0 +1,113 @@
+package main + +import ( + "bufio" + "fmt" + "os" + "strings" + + pad2ipfs ".." + "github.com/fatih/color" +) + +const checkIcon = "\xE2\x9C\x94 " + +func main() { + asciiart := ` + _ ___ _____ _____ ______ _____ + | | |__ \ |_ _| __ \| ____/ ____| + _ __ __ _ __| | ) | | | | |__) | |__ | (___ + | '_ \ / _ |/ _ | / / | | | ___/| __| \___ \ + | |_) | (_| | (_| | / /_ _| |_| | | | ____) | + | .__/ \__,_|\__,_| |____| |_____|_| |_| |_____/ - cli + | | + |_| + ` + color.Blue(asciiart) + fmt.Println(" v0.0.1") + color.Blue("https://github.com/arnaucode/pad2ipfs") + fmt.Println("") + fmt.Println("") + fmt.Println("") + + newcommand := bufio.NewReader(os.Stdin) + fmt.Print("Please select command number") + options := ` + 1 - Pad Link to IPFS + 2 - IPFS hash to file + 0 - Exit cli +option to select: ` + for { + fmt.Print(options) + + option, _ := newcommand.ReadString('\n') + option = strings.TrimSpace(option) + + switch option { + case "1": + fmt.Println("selected 1 - Pad Link to IPFS") + padlinkToIPFS() + break + case "2": + fmt.Println("selected 2 - IPFS hash to file") + hashToFile() + break + case "0": + fmt.Println("selected 0 - exit cli") + os.Exit(3) + break + default: + fmt.Println("Invalid option") + break + } + } +} +func padlinkToIPFS() { + newcommand := bufio.NewReader(os.Stdin) + fmt.Print(" Enter the pad link: ") + link, _ := newcommand.ReadString('\n') + link = strings.Replace(link, "\n", "", -1) + + newcommand = bufio.NewReader(os.Stdin) + formats := ` Available formats: + - md (by default) + - txt + - html + - pdf + - odt` + fmt.Println(formats) + fmt.Print(" Enter the pad format: ") + format, _ := newcommand.ReadString('\n') + format = strings.Replace(format, "\n", "", -1) + if format == "" { + format = "md" + } + if format != "md" && format != "txt" && format != "html" && format != "pdf" && format != "odt" { + fmt.Println(" wrong format, using md format") + format = "md" + } + + hash, err := pad2ipfs.Add(link, format) + if err != nil { + color.Red(err.Error()) + } else { + color.Green(checkIcon + "File added to IPFS network") + fmt.Print("IPFS hash: ") + color.Blue(hash) + } +} +func hashToFile() { + newcommand := bufio.NewReader(os.Stdin) + fmt.Print(" Enter the IPFS hash: ") + hash, _ := newcommand.ReadString('\n') + hash = strings.Replace(hash, "\n", "", -1) + err := pad2ipfs.Get(hash, hash+".md") + if err != nil { + color.Red(err.Error()) + } else { + + color.Green(checkIcon + "File downloaded from IPFS network") + fmt.Print("File stored in: ") + color.Blue(pad2ipfs.GettedPads + "/" + hash + ".md") + } +}
M
pad2ipfs.go
→
pad2ipfs.go
@@ -11,8 +11,11 @@
sh "github.com/ipfs/go-ipfs-api" ) -const addedPads = "addedPads" -const gettedPads = "gettedPads" +//AddedPads is the directory where are stored the pads that are added to IPFS +const AddedPads = "addedPads" + +//GettedPads is the directory where are stored the pads that are getted from IPFS +const GettedPads = "gettedPads" //Add gets the content from the etherpad specified in the link, and downloads it in the format of the specified extension, and then, puts it into IPFS func Add(link string, extension string) (string, error) {@@ -26,7 +29,7 @@ extension = "md"
} //create the pads directory - _ = os.Mkdir(addedPads, os.ModePerm) + _ = os.Mkdir(AddedPads, os.ModePerm) //get the pad name linkSplitted := strings.Split(link, "/")@@ -47,7 +50,7 @@ return "", err
} //save the content into a file - err = ioutil.WriteFile(addedPads+"/"+padName+"."+extension, content, 0644) + err = ioutil.WriteFile(AddedPads+"/"+padName+"."+extension, content, 0644) if err != nil { fmt.Println(err) return "", err@@ -56,7 +59,7 @@
//connect to ipfs shell s := sh.NewShell("localhost:5001") //save the file into IPFS - ipfsHash, err := s.AddDir(addedPads + "/" + padName + "." + extension) + ipfsHash, err := s.AddDir(AddedPads + "/" + padName + "." + extension) if err != nil { fmt.Println(err) return "", err@@ -67,11 +70,11 @@
//Get gets the content from IPFS for a given hash, and saves it into a file func Get(hash string, filename string) error { //create the pads directory - _ = os.Mkdir(gettedPads, os.ModePerm) + _ = os.Mkdir(GettedPads, os.ModePerm) //connect to ipfs shell s := sh.NewShell("localhost:5001") - err := s.Get(hash, gettedPads+"/"+filename) + err := s.Get(hash, GettedPads+"/"+filename) if err != nil { fmt.Println(err) return err