From 21545bf28cb6e5099f16076dee97592446c67e1d Mon Sep 17 00:00:00 2001 From: darmiel <71837281+darmiel@users.noreply.github.com> Date: Tue, 18 May 2021 18:58:48 +0200 Subject: [PATCH] Implemented file flag for `get` and `paste` --- cmd/get.go | 27 +++++++++++++++++++++++++-- cmd/paste.go | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/cmd/get.go b/cmd/get.go index bbed15f..3a096d5 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -18,9 +18,11 @@ limitations under the License. package cmd import ( + "encoding/base64" "fmt" "github.com/darmiel/yaxc/internal/api" "log" + "os" "github.com/spf13/cobra" ) @@ -28,6 +30,7 @@ import ( var ( getAnywherePath string getPassphrase string + getFile string ) // getCmd represents the get command @@ -42,8 +45,27 @@ var getCmd = &cobra.Command{ return } - log.Println("Received contents (", len(content), "bytes ):") - fmt.Println(content) + // write to file? + if getFile == "" { + log.Println("Received contents (", len(content), "bytes ):") + fmt.Println(content) + return + } + + // write to file! + var data []byte + data, err = base64.StdEncoding.DecodeString(content) + if err != nil { + fmt.Println("-- WARN: Could not decode from base64! --") + data = []byte(content) + } + + if err := os.WriteFile(getFile, data, 0666); err != nil { + log.Fatalln("Error writing file contents:", err) + return + } + + log.Println("Saved to", getFile) }, } @@ -52,4 +74,5 @@ func init() { getCmd.Flags().StringVarP(&getAnywherePath, "anywhere", "a", "", "Path (Anywhere)") getCmd.Flags().StringVarP(&getPassphrase, "passphrase", "s", "", "Encryption Key") + getCmd.Flags().StringVarP(&getFile, "out-file", "o", "", "Download contents to file") } diff --git a/cmd/paste.go b/cmd/paste.go index e273d9f..4590d14 100644 --- a/cmd/paste.go +++ b/cmd/paste.go @@ -18,10 +18,12 @@ limitations under the License. package cmd import ( + "encoding/base64" "fmt" "github.com/darmiel/yaxc/internal/api" "github.com/darmiel/yaxc/internal/common" "log" + "os" "github.com/spf13/cobra" ) @@ -29,6 +31,7 @@ import ( var ( pasteAnywherePath string pastePassphrase string + pasteFile string ) // pasteCmd represents the paste command @@ -36,21 +39,32 @@ var pasteCmd = &cobra.Command{ Use: "paste", Long: `Paste piped content to /:path`, Run: func(cmd *cobra.Command, args []string) { - // Read pipe - pipe, err := common.ReadPipe() - if err == common.ErrNotPiped { - log.Fatalln("The command is intended to work with pipes.") - return + var content string + + // read from pipe + if pasteFile == "" { + // Read pipe + pipe, err := common.ReadPipe() + if err == common.ErrNotPiped { + log.Fatalln("The command is intended to work with pipes.") + return + } + if pipe == "" { + log.Fatalln("Empty input.") + return + } + } else { + // read from file + data, err := os.ReadFile(pasteFile) + if err != nil { + log.Fatalln("Error reading file:", err) + return + } + // base64 encode + content = base64.StdEncoding.EncodeToString(data) } - if pipe == "" { - log.Fatalln("Empty input.") - return - } - - log.Println("Pipe Input: '" + pipe + "'") - - if err := api.API().SetContent(pasteAnywherePath, pastePassphrase, pipe); err != nil { + if err := api.API().SetContent(pasteAnywherePath, pastePassphrase, content); err != nil { log.Fatalln("ERROR ::", err) return } @@ -64,4 +78,5 @@ func init() { pasteCmd.Flags().StringVarP(&pasteAnywherePath, "anywhere", "a", "", "Path (Anywhere)") pasteCmd.Flags().StringVarP(&pastePassphrase, "passphrase", "s", "", "Encryption Key") + pasteCmd.Flags().StringVarP(&pasteFile, "file", "f", "", "Upload file contents") }