glow/stash.go

134 lines
2.8 KiB
Go
Raw Normal View History

2020-04-23 22:05:38 +00:00
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
2020-04-23 22:55:51 +00:00
"strconv"
2020-04-23 22:05:38 +00:00
"github.com/charmbracelet/charm"
"github.com/spf13/cobra"
)
var (
identityFile string
forceKey bool
memo string
stashCmd = &cobra.Command{
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")
}
err = cc.StashMarkdown(memo, string(b))
if err != nil {
return fmt.Errorf("error stashing markdown")
}
2020-04-23 22:36:58 +00:00
fmt.Println("Stashed!")
2020-04-23 22:05:38 +00:00
return nil
},
}
2020-04-23 22:47:04 +00:00
stashListCmd = &cobra.Command{
Use: "stash-list",
Hidden: false,
Short: "list your stashed markdowns",
Long: "",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cc := initCharmClient()
2020-05-12 22:18:48 +00:00
mds, err := cc.GetStash(1)
2020-04-23 22:47:04 +00:00
if err != nil {
return fmt.Errorf("error getting stash")
}
fmt.Println("ID\tNote")
fmt.Println("--\t----")
for _, md := range mds {
fmt.Printf("%d\t%s\n", md.ID, md.Note)
}
return nil
},
}
2020-04-23 22:55:51 +00:00
stashGetCmd = &cobra.Command{
Use: "stash-get",
Hidden: false,
Short: "get a stashed markdown by id",
Long: "",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("invalid markdown id")
}
cc := initCharmClient()
2020-05-12 22:18:48 +00:00
md, err := cc.GetStashMarkdown(id)
2020-04-23 22:55:51 +00:00
if err != nil {
return fmt.Errorf("error getting markdown")
}
fmt.Println(md.Body)
return nil
},
}
2020-04-23 23:02:59 +00:00
stashDeleteCmd = &cobra.Command{
Use: "stash-delete",
Hidden: false,
Short: "get a stashed markdown by id",
Long: "",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("invalid markdown id")
}
cc := initCharmClient()
err = cc.DeleteMarkdown(id)
if err != nil {
return fmt.Errorf("error deleting markdown")
}
fmt.Println("Deleted!")
return nil
},
}
2020-04-23 22:05:38 +00:00
)
func getCharmConfig() *charm.Config {
cfg, err := charm.ConfigFromEnv()
if err != nil {
log.Fatal(err)
}
if identityFile != "" {
cfg.SSHKeyPath = identityFile
cfg.ForceKey = true
}
if forceKey {
cfg.ForceKey = true
}
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
}