Added get command

This commit is contained in:
darmiel 2021-03-27 13:49:13 +01:00
parent 8bd79f3751
commit f90dbb0c9c
5 changed files with 168 additions and 12 deletions

53
cmd/get.go Normal file
View file

@ -0,0 +1,53 @@
/*
Copyright © 2021 darmiel <hi@d2a.io>
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")
}

65
cmd/paste.go Normal file
View file

@ -0,0 +1,65 @@
/*
Copyright © 2021 darmiel <hi@d2a.io>
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")
}

View file

@ -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.

35
internal/api/get.go Normal file
View file

@ -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
}

View file

@ -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,
}
}