mirror of
https://github.com/darmiel/yaxc.git
synced 2025-09-07 06:55:17 +08:00
Implemented file flag for get
and paste
This commit is contained in:
parent
1797bd02c2
commit
21545bf28c
2 changed files with 53 additions and 15 deletions
27
cmd/get.go
27
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")
|
||||
}
|
||||
|
|
41
cmd/paste.go
41
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")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue