mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-02 05:41:10 -08:00
feat(cli): add edit commands
This commit is contained in:
parent
4f50517784
commit
465f074f70
|
@ -8,23 +8,27 @@ import (
|
|||
"fmt"
|
||||
"oh-my-posh/environment"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// getCmd represents the get command
|
||||
var getCache = &cobra.Command{
|
||||
Use: "cache [path|clear]",
|
||||
Use: "cache [path|clear|edit]",
|
||||
Short: "Interact with the oh-my-posh cache",
|
||||
Long: `Interact with the oh-my-posh cache.
|
||||
You can do the following:
|
||||
|
||||
- path: list the cache path
|
||||
- clear: remove all cache values`,
|
||||
- path: list cache path
|
||||
- clear: remove all cache values
|
||||
- edit: edit cache values`,
|
||||
ValidArgs: []string{
|
||||
"path",
|
||||
"clear",
|
||||
"edit",
|
||||
},
|
||||
Args: cobra.OnlyValidArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
@ -44,6 +48,9 @@ You can do the following:
|
|||
return
|
||||
}
|
||||
fmt.Printf("removed cache file at %s\n", cacheFilePath)
|
||||
case "edit":
|
||||
cacheFilePath := filepath.Join(env.CachePath(), environment.CacheFile)
|
||||
editFileWithEditor(cacheFilePath)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -51,3 +58,19 @@ You can do the following:
|
|||
func init() { // nolint:gochecknoinits
|
||||
rootCmd.AddCommand(getCache)
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,16 +5,34 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
|||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// configCmd represents the config command
|
||||
var configCmd = &cobra.Command{
|
||||
Use: "config [export|migrate|get]",
|
||||
Use: "config [export|migrate|edit]",
|
||||
Short: "Interact with the configuration",
|
||||
Long: `Interact with the configuration
|
||||
It allows to export, migrate or get a configuration value.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {},
|
||||
It allows to export, migrate or edit the configuration.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
_ = cmd.Help()
|
||||
return
|
||||
}
|
||||
switch args[0] {
|
||||
case "edit":
|
||||
editFileWithEditor(os.Getenv("POSH_THEME"))
|
||||
case "get":
|
||||
// only here for backwards compatibility
|
||||
fmt.Print(time.Now().UnixNano() / 1000000)
|
||||
default:
|
||||
_ = cmd.Help()
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() { // nolint:gochecknoinits
|
||||
|
|
|
@ -37,8 +37,8 @@ This command is used to get the value of the following variables:
|
|||
fmt.Print(time.Now().UnixNano() / 1000000)
|
||||
case "shell":
|
||||
fmt.Println(env.Shell())
|
||||
// case "cache-path":
|
||||
// fmt.Print(env.CachePath())
|
||||
default:
|
||||
_ = cmd.Help()
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
@ -14,7 +14,9 @@ var promptCmd = &cobra.Command{
|
|||
Short: "Set up the prompt for your shell",
|
||||
Long: `Set up the prompt for your shell
|
||||
Allows to initialize one of the supported shells, or to set the prompt manually for a custom shell.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
_ = cmd.Help()
|
||||
},
|
||||
}
|
||||
|
||||
func init() { // nolint:gochecknoinits
|
||||
|
|
|
@ -104,6 +104,8 @@ var printCmd = &cobra.Command{
|
|||
fmt.Print(eng.PrintExtraPrompt(engine.Valid))
|
||||
case "error":
|
||||
fmt.Print(eng.PrintExtraPrompt(engine.Error))
|
||||
default:
|
||||
_ = cmd.Help()
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ on getting started, have a look at the docs at https://ohmyposh.dev`,
|
|||
fmt.Println(cliVersion)
|
||||
return
|
||||
}
|
||||
_ = cmd.Help()
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue