Skip to content

Commit

Permalink
Merge pull request #21 from thealamu/iss#17
Browse files Browse the repository at this point in the history
Support "--from-clipboard" option for copy command - Closes #17
  • Loading branch information
nakabonne authored Dec 13, 2020
2 parents 5db914a + 4c95f4a commit 2bb3509
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
25 changes: 19 additions & 6 deletions commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (
"strings"
"time"

"github.com/atotto/clipboard"
"github.com/spf13/cobra"

pbcrypto "github.com/nakabonne/pbgopy/crypto"
)

type copyRunner struct {
timeout time.Duration
password string
basicAuth string
maxBufSize string
timeout time.Duration
password string
basicAuth string
maxBufSize string
fromClipboard bool

stdout io.Writer
stderr io.Writer
Expand All @@ -41,6 +43,7 @@ func NewCopyCommand(stdout, stderr io.Writer) *cobra.Command {
cmd.Flags().StringVarP(&r.password, "password", "p", "", "Password for encryption/decryption")
cmd.Flags().StringVarP(&r.basicAuth, "basic-auth", "a", "", "Basic authentication, username:password")
cmd.Flags().StringVar(&r.maxBufSize, "max-size", "500mb", "Max data size with unit")
cmd.Flags().BoolVarP(&r.fromClipboard, "from-clipboard", "c", false, "Put the data stored at clipboard into pbgopy server")
return cmd
}

Expand All @@ -50,13 +53,23 @@ func (r *copyRunner) run(_ *cobra.Command, _ []string) error {
return fmt.Errorf("put the pbgopy server's address into %s environment variable", pbgopyServerEnv)
}

var source io.Reader = os.Stdin

if r.fromClipboard {
clipboardData, err := clipboard.ReadAll()
if err != nil {
return err
}
source = strings.NewReader(clipboardData)
}

sizeInBytes, err := datasizeToBytes(r.maxBufSize)
if err != nil {
return fmt.Errorf("failed to parse data size: %w", err)
}
data, err := readNoMoreThan(os.Stdin, sizeInBytes)
data, err := readNoMoreThan(source, sizeInBytes)
if err != nil {
return fmt.Errorf("failed to read from STDIN: %w", err)
return fmt.Errorf("failed to read from source: %w", err)
}

client := &http.Client{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/nakabonne/pbgopy
go 1.15

require (
github.com/atotto/clipboard v0.1.2
github.com/spf13/cobra v1.1.1
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
Expand Down

0 comments on commit 2bb3509

Please sign in to comment.