Automatically detect piped stdin on the root command; it doubles as input and output

This commit is contained in:
Mariano Uvalle 2023-11-10 17:01:28 +00:00
parent 3470117c31
commit 505e0e4b41
8 changed files with 71 additions and 125 deletions

View file

@ -1,42 +0,0 @@
package main
import (
"errors"
"fmt"
"os"
"github.com/AYM1607/ccclip/internal/configfile"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(getClipboardCmd)
}
var getClipboardCmd = &cobra.Command{
Use: "get-clipboard",
Short: "get the currently stored clipboard",
RunE: func(cmd *cobra.Command, args []string) error {
cc, err := configfile.EnsureAndGet()
if err != nil {
return err
}
if cc.DeviceId == "" {
return errors.New("you must log in and register your device")
}
pvk, err := configfile.LoadPrivateKey()
if err != nil {
return fmt.Errorf("could not load this device's private key: %w", err)
}
plain, err := apiclient.GetClipboard(cc.DeviceId, pvk)
if err != nil {
return fmt.Errorf("could not set clipboard: %w", err)
}
fmt.Printf("Your current clipbard is:")
fmt.Fprintf(os.Stdout, plain)
return nil
},
}

View file

@ -1,34 +0,0 @@
package main
import (
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(getDevicesCmd)
}
var getDevicesCmd = &cobra.Command{
Use: "get-devices",
Short: "Register a user with a given email and password",
RunE: func(cmd *cobra.Command, args []string) error {
// cc, err := configfile.EnsureAndGet()
// if err != nil {
// return err
// }
// if cc.DeviceId == "" {
// return errors.New("your device is not registered")
// }
// pvk, err := configfile.LoadPrivateKey()
// if err != nil {
// return err
// }
// devices, err := apiclient.GetDevices(cc.DeviceId, pvk)
// if err != nil {
// return err
// }
// return json.NewEncoder(os.Stdout).Encode(devices)
return nil
},
}

View file

@ -1,23 +1,84 @@
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"path"
"github.com/AYM1607/ccclip/internal/configfile"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
)
func getClipboard() (string, error) {
cc, err := configfile.EnsureAndGet()
if err != nil {
return "", err
}
if cc.DeviceId == "" {
return "", errors.New("you must log in and register your device")
}
pvk, err := configfile.LoadPrivateKey()
if err != nil {
return "", fmt.Errorf("could not load this device's private key: %w", err)
}
plain, err := apiclient.GetClipboard(cc.DeviceId, pvk)
if err != nil {
return "", fmt.Errorf("could not set clipboard: %w", err)
}
return plain, nil
}
func setClipboard(clip []byte) error {
cc, err := configfile.EnsureAndGet()
if err != nil {
return err
}
if cc.DeviceId == "" {
return errors.New("you must log in and register your device")
}
pvk, err := configfile.LoadPrivateKey()
if err != nil {
return fmt.Errorf("could not load this device's private key: %w", err)
}
err = apiclient.SetClipboard(clip, cc.DeviceId, pvk)
if err != nil {
return fmt.Errorf("could not set clipboard: %w", err)
}
return nil
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ccclip",
Short: "copy strings to and from your end to end encrypted cloud clipboard",
Long: `copy strings to and from your end to end encrypted cloud clipboard`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
RunE: func(cmd *cobra.Command, args []string) error {
if isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()) {
// Nothing piped through stdin. Reading clipboard.
clip, err := getClipboard()
if err != nil {
return err
}
_, err = os.Stdout.Write([]byte(clip))
return err
}
clip, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
return setClipboard(clip)
},
}
func init() {

View file

@ -1,43 +0,0 @@
package main
import (
"errors"
"fmt"
"github.com/AYM1607/ccclip/internal/configfile"
"github.com/spf13/cobra"
)
var clipboard string
func init() {
rootCmd.AddCommand(setClipboardCmd)
setClipboardCmd.Flags().StringVar(&clipboard, "clip", "", "the string to send")
setClipboardCmd.MarkFlagRequired("clip")
}
var setClipboardCmd = &cobra.Command{
Use: "set-clipboard",
Short: "set the given string as the cloud clipboard",
RunE: func(cmd *cobra.Command, args []string) error {
cc, err := configfile.EnsureAndGet()
if err != nil {
return err
}
if cc.DeviceId == "" {
return errors.New("you must log in and register your device")
}
pvk, err := configfile.LoadPrivateKey()
if err != nil {
return fmt.Errorf("could not load this device's private key: %w", err)
}
err = apiclient.SetClipboard(clipboard, cc.DeviceId, pvk)
if err != nil {
return fmt.Errorf("could not set clipboard: %w", err)
}
return nil
},
}