oh-my-posh/src/cli/cache.go

78 lines
1.6 KiB
Go
Raw Normal View History

2022-03-19 11:32:40 -07:00
package cli
import (
"fmt"
"oh-my-posh/environment"
"os"
2022-03-19 11:48:37 -07:00
"os/exec"
2022-03-19 11:32:40 -07:00
"path/filepath"
2022-03-19 11:48:37 -07:00
"strings"
2022-03-19 11:32:40 -07:00
"github.com/spf13/cobra"
)
// getCmd represents the get command
var getCache = &cobra.Command{
2022-03-19 11:48:37 -07:00
Use: "cache [path|clear|edit]",
2022-03-19 11:32:40 -07:00
Short: "Interact with the oh-my-posh cache",
Long: `Interact with the oh-my-posh cache.
2022-03-19 11:32:40 -07:00
You can do the following:
2022-03-19 11:48:37 -07:00
- path: list cache path
- clear: remove all cache values
- edit: edit cache values`,
2022-03-19 11:32:40 -07:00
ValidArgs: []string{
"path",
"clear",
2022-03-19 11:48:37 -07:00
"edit",
2022-03-19 11:32:40 -07:00
},
Args: NoArgsOrOneValidArg,
2022-03-19 11:32:40 -07:00
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
return
}
2022-03-19 11:32:40 -07:00
env := &environment.ShellEnvironment{
Version: cliVersion,
}
env.Init()
2022-03-19 11:32:40 -07:00
defer env.Close()
switch args[0] {
case "path":
fmt.Print(env.CachePath())
case "clear":
cacheFilePath := filepath.Join(env.CachePath(), environment.CacheFile)
err := os.Remove(cacheFilePath)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("removed cache file at %s\n", cacheFilePath)
2022-03-19 11:48:37 -07:00
case "edit":
cacheFilePath := filepath.Join(env.CachePath(), environment.CacheFile)
editFileWithEditor(cacheFilePath)
2022-03-19 11:32:40 -07:00
}
},
}
func init() { // nolint:gochecknoinits
rootCmd.AddCommand(getCache)
}
2022-03-19 11:48:37 -07:00
func editFileWithEditor(file string) {
editor := os.Getenv("EDITOR")
var args []string
if strings.Contains(editor, " ") {
splitted := strings.Split(editor, " ")
editor = splitted[0]
args = splitted[1:]
}
args = append(args, file)
cmd := exec.Command(editor, args...)
err := cmd.Run()
if err != nil {
fmt.Println(err.Error())
}
}