mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-20 18:48:09 -08:00
feat(cli): add edit commands
This commit is contained in:
parent
4f50517784
commit
465f074f70
|
@ -8,23 +8,27 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"oh-my-posh/environment"
|
"oh-my-posh/environment"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getCmd represents the get command
|
// getCmd represents the get command
|
||||||
var getCache = &cobra.Command{
|
var getCache = &cobra.Command{
|
||||||
Use: "cache [path|clear]",
|
Use: "cache [path|clear|edit]",
|
||||||
Short: "Interact with the oh-my-posh cache",
|
Short: "Interact with the oh-my-posh cache",
|
||||||
Long: `Interact with the oh-my-posh cache.
|
Long: `Interact with the oh-my-posh cache.
|
||||||
You can do the following:
|
You can do the following:
|
||||||
|
|
||||||
- path: list the cache path
|
- path: list cache path
|
||||||
- clear: remove all cache values`,
|
- clear: remove all cache values
|
||||||
|
- edit: edit cache values`,
|
||||||
ValidArgs: []string{
|
ValidArgs: []string{
|
||||||
"path",
|
"path",
|
||||||
"clear",
|
"clear",
|
||||||
|
"edit",
|
||||||
},
|
},
|
||||||
Args: cobra.OnlyValidArgs,
|
Args: cobra.OnlyValidArgs,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
@ -44,6 +48,9 @@ You can do the following:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("removed cache file at %s\n", cacheFilePath)
|
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
|
func init() { // nolint:gochecknoinits
|
||||||
rootCmd.AddCommand(getCache)
|
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
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// configCmd represents the config command
|
// configCmd represents the config command
|
||||||
var configCmd = &cobra.Command{
|
var configCmd = &cobra.Command{
|
||||||
Use: "config [export|migrate|get]",
|
Use: "config [export|migrate|edit]",
|
||||||
Short: "Interact with the configuration",
|
Short: "Interact with the configuration",
|
||||||
Long: `Interact with the configuration
|
Long: `Interact with the configuration
|
||||||
It allows to export, migrate or get a configuration value.`,
|
It allows to export, migrate or edit the configuration.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {},
|
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
|
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)
|
fmt.Print(time.Now().UnixNano() / 1000000)
|
||||||
case "shell":
|
case "shell":
|
||||||
fmt.Println(env.Shell())
|
fmt.Println(env.Shell())
|
||||||
// case "cache-path":
|
default:
|
||||||
// fmt.Print(env.CachePath())
|
_ = cmd.Help()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,9 @@ var promptCmd = &cobra.Command{
|
||||||
Short: "Set up the prompt for your shell",
|
Short: "Set up the prompt for your shell",
|
||||||
Long: `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.`,
|
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
|
func init() { // nolint:gochecknoinits
|
||||||
|
|
|
@ -104,6 +104,8 @@ var printCmd = &cobra.Command{
|
||||||
fmt.Print(eng.PrintExtraPrompt(engine.Valid))
|
fmt.Print(eng.PrintExtraPrompt(engine.Valid))
|
||||||
case "error":
|
case "error":
|
||||||
fmt.Print(eng.PrintExtraPrompt(engine.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)
|
fmt.Println(cliVersion)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
_ = cmd.Help()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue