diff --git a/README.md b/README.md index d0bd8ed..296c763 100644 --- a/README.md +++ b/README.md @@ -5,18 +5,18 @@ Simply Go lang library to get the content from a pad (etherpad) and put into IPF 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 @@ Supported formats: -##### - 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: + +![pad2ipfs-cli-screenshot](https://raw.githubusercontent.com/arnaucode/pad2ipfs/master/pad2ipfs-cli-screenshot.png "pad2ipfs-cli-screenshot") diff --git a/pad2ipfs-cli-screenshot.png b/pad2ipfs-cli-screenshot.png new file mode 100644 index 0000000..c97dfbf Binary files /dev/null and b/pad2ipfs-cli-screenshot.png differ diff --git a/pad2ipfs-cli/main.go b/pad2ipfs-cli/main.go new file mode 100644 index 0000000..a0e190b --- /dev/null +++ b/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") + } +} diff --git a/pad2ipfs-cli/pad2ipfs-cli b/pad2ipfs-cli/pad2ipfs-cli new file mode 100755 index 0000000..5e7fd0a Binary files /dev/null and b/pad2ipfs-cli/pad2ipfs-cli differ diff --git a/pad2ipfs.go b/pad2ipfs.go index 9a660d3..0593f05 100644 --- a/pad2ipfs.go +++ b/pad2ipfs.go @@ -11,8 +11,11 @@ import ( 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 @@ func Add(link string, extension string) (string, error) { } //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 @@ func Add(link string, extension string) (string, error) { } //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 @@ func Add(link string, extension string) (string, error) { //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 @@ func Add(link string, extension string) (string, error) { //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