2023-11-06 07:33:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-10 17:01:28 +00:00
|
|
|
"errors"
|
2023-11-10 16:42:56 +00:00
|
|
|
"fmt"
|
2023-11-10 17:01:28 +00:00
|
|
|
"io"
|
2023-11-06 07:33:52 +00:00
|
|
|
"log"
|
2023-11-10 16:42:56 +00:00
|
|
|
"os"
|
|
|
|
|
"path"
|
2023-11-06 07:33:52 +00:00
|
|
|
|
2023-11-08 07:33:19 +00:00
|
|
|
"github.com/AYM1607/ccclip/internal/configfile"
|
2023-11-10 17:01:28 +00:00
|
|
|
|
|
|
|
|
"github.com/mattn/go-isatty"
|
2023-11-06 07:33:52 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
2023-11-10 17:01:28 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 07:33:52 +00:00
|
|
|
// 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`,
|
2023-11-10 17:01:28 +00:00
|
|
|
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)
|
|
|
|
|
},
|
2023-11-06 07:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
2023-11-10 16:42:56 +00:00
|
|
|
homeDir, err := os.UserHomeDir()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Sprintf("could not locate home directory: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
defualtConfigPath := path.Join(homeDir, ".config", "ccclip")
|
2023-11-06 07:33:52 +00:00
|
|
|
|
2023-11-10 16:42:56 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&configfile.Path, "config-dir", defualtConfigPath, "location of the ccclip.yaml configuration file")
|
2023-11-06 07:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|