Use base name when stashing from the CLI if no memo is set

This commit is contained in:
Christian Rocha 2020-08-27 14:26:38 -04:00 committed by Christian Muehlhaeuser
parent a5b67ba721
commit b3909354aa

View file

@ -5,6 +5,8 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"path"
"strings"
"github.com/charmbracelet/charm" "github.com/charmbracelet/charm"
"github.com/charmbracelet/charm/ui/common" "github.com/charmbracelet/charm/ui/common"
@ -18,7 +20,8 @@ var (
forceKey bool forceKey bool
*/ */
memo string memo string
stashCmd = &cobra.Command{ stashCmd = &cobra.Command{
Use: "stash SOURCE", Use: "stash SOURCE",
Hidden: false, Hidden: false,
@ -26,20 +29,29 @@ var (
Long: "", Long: "",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
filePath := args[0]
if memo == "" {
memo = strings.Replace(path.Base(filePath), path.Ext(filePath), "", 1)
}
cc := initCharmClient() cc := initCharmClient()
f, err := os.Open(args[0]) f, err := os.Open(filePath)
if err != nil { if err != nil {
return fmt.Errorf("bad filename") return fmt.Errorf("bad filename")
} }
defer f.Close() defer f.Close()
b, err := ioutil.ReadAll(f) b, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
return fmt.Errorf("error reading file") return fmt.Errorf("error reading file")
} }
_, err = cc.StashMarkdown(memo, string(b)) _, err = cc.StashMarkdown(memo, string(b))
if err != nil { if err != nil {
return fmt.Errorf("error stashing markdown") return fmt.Errorf("error stashing markdown")
} }
dot := termenv.String("•").Foreground(common.Green.Color()).String() dot := termenv.String("•").Foreground(common.Green.Color()).String()
fmt.Println(dot + " Stashed!") fmt.Println(dot + " Stashed!")
return nil return nil