2020-04-23 22:05:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/charm"
|
2020-07-21 21:38:37 +00:00
|
|
|
"github.com/charmbracelet/charm/ui/common"
|
|
|
|
"github.com/muesli/termenv"
|
2020-04-23 22:05:38 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-08-24 12:53:55 +00:00
|
|
|
/*
|
|
|
|
identityFile string
|
|
|
|
forceKey bool
|
|
|
|
*/
|
|
|
|
|
|
|
|
memo string
|
|
|
|
stashCmd = &cobra.Command{
|
2020-04-23 22:05:38 +00:00
|
|
|
Use: "stash SOURCE",
|
|
|
|
Hidden: false,
|
|
|
|
Short: "stash a markdown",
|
|
|
|
Long: "",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cc := initCharmClient()
|
|
|
|
f, err := os.Open(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("bad filename")
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reading file")
|
|
|
|
}
|
2020-08-21 19:52:53 +00:00
|
|
|
_, err = cc.StashMarkdown(memo, string(b))
|
2020-04-23 22:05:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error stashing markdown")
|
|
|
|
}
|
2020-07-21 21:38:37 +00:00
|
|
|
dot := termenv.String("•").Foreground(common.Green.Color()).String()
|
|
|
|
fmt.Println(dot + " Stashed!")
|
2020-04-23 22:05:38 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func getCharmConfig() *charm.Config {
|
|
|
|
cfg, err := charm.ConfigFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-08-24 12:53:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
if identityFile != "" {
|
|
|
|
cfg.SSHKeyPath = identityFile
|
|
|
|
cfg.ForceKey = true
|
|
|
|
}
|
|
|
|
if forceKey {
|
|
|
|
cfg.ForceKey = true
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-04-23 22:05:38 +00:00
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func initCharmClient() *charm.Client {
|
|
|
|
cfg := getCharmConfig()
|
|
|
|
cc, err := charm.NewClient(cfg)
|
|
|
|
if err == charm.ErrMissingSSHAuth {
|
|
|
|
log.Fatal("Missing ssh key. Run `ssh-keygen` to make one or set the `CHARM_SSH_KEY_PATH` env var to your private key path.")
|
|
|
|
} else if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return cc
|
|
|
|
}
|