From f90dbb0c9ceffa7869e1a0e4d2eca1b61b403974 Mon Sep 17 00:00:00 2001 From: darmiel <71837281+darmiel@users.noreply.github.com> Date: Sat, 27 Mar 2021 13:49:13 +0100 Subject: [PATCH] Added `get` command --- cmd/get.go | 53 ++++++++++++++++++++++++++++++++++++ cmd/paste.go | 65 ++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 12 +------- internal/api/get.go | 35 ++++++++++++++++++++++++ internal/api/type.go | 15 +++++++++- 5 files changed, 168 insertions(+), 12 deletions(-) create mode 100644 cmd/get.go create mode 100644 cmd/paste.go create mode 100644 internal/api/get.go diff --git a/cmd/get.go b/cmd/get.go new file mode 100644 index 0000000..8ca7eda --- /dev/null +++ b/cmd/get.go @@ -0,0 +1,53 @@ +/* +Copyright © 2021 darmiel + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "fmt" + "github.com/darmiel/yaxc/internal/api" + "log" + + "github.com/spf13/cobra" +) + +var ( + getAnywherePath string + getPassphrase string +) + +// getCmd represents the get command +var getCmd = &cobra.Command{ + Use: "get", + Long: `Get (encrypted) contents from /:path`, + Run: func(cmd *cobra.Command, args []string) { + log.Println("Loading contents for path", getAnywherePath, "with passphrase", getPassphrase) + content, err := api.API().GetContent(getAnywherePath, getPassphrase) + if err != nil { + log.Fatalln("Error receiving contents:", err) + return + } + + log.Println("Received contents (", len(content), "bytes ):") + fmt.Println(content) + }, +} + +func init() { + rootCmd.AddCommand(getCmd) + + getCmd.Flags().StringVarP(&getAnywherePath, "anywhere", "a", "", "Path (Anywhere)") + getCmd.Flags().StringVarP(&getPassphrase, "passphrase", "s", "", "Encryption Key") +} diff --git a/cmd/paste.go b/cmd/paste.go new file mode 100644 index 0000000..f3b2923 --- /dev/null +++ b/cmd/paste.go @@ -0,0 +1,65 @@ +/* +Copyright © 2021 darmiel + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "fmt" + "github.com/darmiel/yaxc/internal/api" + "github.com/darmiel/yaxc/internal/common" + "log" + + "github.com/spf13/cobra" +) + +var ( + pasteAnywherePath string + pastePassphrase string +) + +// pasteCmd represents the paste command +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.NotPiped { + log.Fatalln("The command is intended to work with pipes.") + return + } + + if pipe == "" { + log.Fatalln("Empty input.") + return + } + + if err := api.API().SetContent(pasteAnywherePath, pastePassphrase, pipe); err != nil { + log.Fatalln("ERROR ::", err) + return + } + + fmt.Println("Successfully uploaded contents to", pasteAnywherePath) + }, +} + +func init() { + rootCmd.AddCommand(pasteCmd) + + pasteCmd.Flags().StringVarP(&pasteAnywherePath, "anywhere", "a", "", "Path (Anywhere)") + pasteCmd.Flags().StringVarP(&pastePassphrase, "passphrase", "s", "", "Encryption Key") + + regStrP(pasteCmd, "passphrase", "s", "", "Secret") +} diff --git a/cmd/root.go b/cmd/root.go index ff9651a..72f3d38 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,6 @@ package cmd import ( "fmt" - "github.com/darmiel/yaxc/internal/api" "github.com/spf13/cobra" "os" "time" @@ -30,11 +29,6 @@ var ( cfgFile string ) -var ( - settingServer string - Api *api.API -) - // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "yaxc", @@ -44,17 +38,13 @@ var rootCmd = &cobra.Command{ // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - Api = &api.API{ - ServerURL: settingServer, - } cobra.CheckErr(rootCmd.Execute()) } func init() { cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.yaxc.yaml)") - rootCmd.PersistentFlags().StringVar(&settingServer, "server", "", "URL of API-Server") - cobra.CheckErr(viper.BindPFlag("server", rootCmd.PersistentFlags().Lookup("server"))) + regStr(rootCmd, "server", "", "URL of API-Server") } // initConfig reads in config file and ENV variables if set. diff --git a/internal/api/get.go b/internal/api/get.go new file mode 100644 index 0000000..163d5d0 --- /dev/null +++ b/internal/api/get.go @@ -0,0 +1,35 @@ +package api + +import ( + "github.com/darmiel/yaxc/internal/common" + "github.com/imroc/req" +) + +func (a *api) GetContent(path, passphrase string) (res string, err error) { + var resp *req.Resp + if resp, err = req.Get(a.ServerURL + "/" + path); err != nil { + return + } + res = resp.String() + // encryption + if passphrase != "" { + var b []byte + if b, err = common.Decrypt(res, passphrase); err != nil { + return + } + res = string(b) + } + return +} + +func (a *api) SetContent(path, passphrase, content string) (err error) { + if passphrase != "" { + var b []byte + if b, err = common.Encrypt(content, passphrase); err != nil { + return + } + content = string(b) + } + _, err = req.Post(a.ServerURL+"/"+path, content) + return +} diff --git a/internal/api/type.go b/internal/api/type.go index 347f9f4..428ba15 100644 --- a/internal/api/type.go +++ b/internal/api/type.go @@ -1,5 +1,18 @@ package api -type API struct { +import "github.com/spf13/viper" + +type api struct { ServerURL string } + +func API() *api { + server := viper.GetString("server") + if server == "" { + server = "http://127.0.0.1:1332" + } + + return &api{ + ServerURL: server, + } +}