feat(cli): add edit commands

This commit is contained in:
Jan De Dobbeleer 2022-03-19 19:48:37 +01:00 committed by Jan De Dobbeleer
parent 4f50517784
commit 465f074f70
6 changed files with 55 additions and 9 deletions

View file

@ -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())
}
}

View file

@ -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

View file

@ -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()
}
},
}

View file

@ -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

View file

@ -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()
}
},
}

View file

@ -30,6 +30,7 @@ on getting started, have a look at the docs at https://ohmyposh.dev`,
fmt.Println(cliVersion)
return
}
_ = cmd.Help()
},
}