gime/cmd/editor.go

48 lines
917 B
Go
Raw Normal View History

2022-01-19 18:56:31 +00:00
/*
2022-01-19 21:30:30 +00:00
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
2022-01-19 18:56:31 +00:00
*/
package cmd
import (
"fmt"
2022-01-21 22:49:05 +00:00
"os"
"os/exec"
2022-01-19 18:56:31 +00:00
2022-01-21 22:49:05 +00:00
"git.bullercodeworks.com/brian/gime/cli"
2022-01-19 18:56:31 +00:00
"github.com/spf13/cobra"
)
// editorCmd represents the editor command
var editorCmd = &cobra.Command{
Use: "editor",
Short: "Open either the timer.txt file or the done.txt file in the default editor ",
RunE: opEditor,
2022-01-19 18:56:31 +00:00
}
func init() {
rootCmd.AddCommand(editorCmd)
2022-01-19 21:30:30 +00:00
}
2022-01-19 18:56:31 +00:00
2022-01-19 21:30:30 +00:00
func opEditor(cmd *cobra.Command, args []string) error {
2022-01-21 22:49:05 +00:00
p := cli.Program{}
if err := p.Initialize(); err != nil {
return err
}
file := p.GetTimerFilePath()
if len(args) > 0 {
if args[0] == "d" || args[0] == "done" {
file = p.GetDoneFilePath()
}
}
editor := os.Getenv("EDITOR")
if editor == "" {
return fmt.Errorf("No EDITOR set")
}
fmt.Println("Starting", editor, file)
c := exec.Command(editor, file)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
return c.Run()
2022-01-19 18:56:31 +00:00
}