feat: add aliases

This commit is contained in:
Kevin Franklin Kim 2023-01-09 09:58:43 +01:00
parent 8b1224ca22
commit 5973c15034
10 changed files with 41 additions and 15 deletions

View File

@ -19,7 +19,7 @@ var brewCmd = &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l, flagConfig); err != nil {
if err := intconfig.Load(l); err != nil {
return err
}
return nil

View File

@ -15,7 +15,7 @@ var execCmd = &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l, flagConfig); err != nil {
if err := intconfig.Load(l); err != nil {
return err
}
return nil

View File

@ -15,7 +15,7 @@ var promptCmd = &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l, flagConfig); err != nil {
if err := intconfig.Load(l); err != nil {
return err
}
return nil

View File

@ -15,7 +15,7 @@ var requireCmd = &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := intconfig.Load(l, flagConfig); err != nil {
if err := intconfig.Load(l); err != nil {
return err
}
return nil

View File

@ -15,7 +15,6 @@ import (
var (
l log.Logger
flagLevel string
flagConfig string
flagNoColor bool
)
@ -60,7 +59,6 @@ func init() {
cobra.OnInitialize(initialize)
rootCmd.PersistentFlags().BoolVar(&flagNoColor, "no-color", false, "disabled colors (default is false)")
rootCmd.PersistentFlags().StringVar(&flagLevel, "level", "info", "set log level (default is warn)")
rootCmd.PersistentFlags().StringVar(&flagConfig, "config", "", "config file (default is $HOME/.posh.yml)")
}
// initialize reads in config file and ENV variables if set.

View File

@ -17,7 +17,7 @@ type Node struct {
PassThroughArgs Args
PassThroughFlags func(fs *readline.FlagSet)
Description string
Commands []*Node
Nodes []*Node
Execute func(ctx context.Context, args *readline.Readline) error
}
@ -48,8 +48,8 @@ func (c *Node) setFlags(r *readline.Readline, parse bool) error {
func (c *Node) completeArguments(ctx context.Context, p *Root, r *readline.Readline, i int) []prompt.Suggest {
var suggest []prompt.Suggest
localArgs := r.Args()[i:]
if len(c.Commands) > 0 && len(localArgs) <= 1 {
for _, command := range c.Commands {
if len(c.Nodes) > 0 && len(localArgs) <= 1 {
for _, command := range c.Nodes {
suggest = append(suggest, prompt.Suggest{Text: command.Name, Description: command.Description})
}
} else if len(c.Args) >= len(localArgs) {
@ -84,7 +84,7 @@ func (c *Node) completePassThroughFlags(r *readline.Readline) []prompt.Suggest {
func (c *Node) execute(ctx context.Context, r *readline.Readline, i int) error {
localArgs := r.Args()[i:]
if len(c.Commands) > 0 && len(localArgs) == 0 {
if len(c.Nodes) > 0 && len(localArgs) == 0 {
return errors.New("missing [command] argument")
} else if len(c.Args) > 0 {
for j, arg := range c.Args {

View File

@ -91,7 +91,7 @@ func (p *Root) find(cmds []*Node, r *readline.Readline, i int) (*Node, int) {
arg := r.Args().At(i)
for _, cmd := range cmds {
if cmd.Name == arg {
if subCmd, j := p.find(cmd.Commands, r, i+1); subCmd != nil {
if subCmd, j := p.find(cmd.Nodes, r, i+1); subCmd != nil {
return subCmd, j
}
return cmd, i
@ -99,7 +99,7 @@ func (p *Root) find(cmds []*Node, r *readline.Readline, i int) (*Node, int) {
if cmd.Names != nil {
for _, name := range cmd.Names() {
if name == arg {
if subCmd, j := p.find(cmd.Commands, r, i+1); subCmd != nil {
if subCmd, j := p.find(cmd.Nodes, r, i+1); subCmd != nil {
return subCmd, j
}
}

View File

@ -2,9 +2,10 @@ package config
type (
Prompt struct {
Title string `json:"title" yaml:"title"`
Prefix string `json:"prefix" yaml:"prefix"`
History PromptHistory `json:"history" yaml:"history"`
Title string `json:"title" yaml:"title"`
Prefix string `json:"prefix" yaml:"prefix"`
History PromptHistory `json:"history" yaml:"history"`
Aliases map[string]string `json:"aliases" yaml:"aliases"`
}
PromptHistory struct {
Limit int `json:"limit" yaml:"limit"`

View File

@ -30,6 +30,7 @@ type (
readline *readline.Readline
history history.History
commands command.Commands
aliases map[string]string
// inputRegex - split cmd into args
promptOptions []prompt.Option
}
@ -75,6 +76,13 @@ func WithCheckers(v ...check.Checker) Option {
}
}
func WithAliases(v map[string]string) Option {
return func(o *Prompt) error {
o.aliases = v
return nil
}
}
func WithContext(v context.Context) Option {
return func(o *Prompt) error {
o.ctx = v
@ -214,6 +222,18 @@ func (s *Prompt) Run() error {
// ~ Private methods
// ------------------------------------------------------------------------------------------------
func (s *Prompt) alias(input string, aliases map[string]string) string {
if len(aliases) == 0 {
return input
} else if parts := strings.Split(input, " "); len(parts) == 0 {
return input
} else if value, ok := aliases[parts[0]]; !ok {
return input
} else {
return value + " " + strings.Join(parts[1:], " ")
}
}
func (s *Prompt) execute(input string) {
input = strings.TrimSpace(input)
if input == "" {
@ -222,6 +242,8 @@ func (s *Prompt) execute(input string) {
s.history.Persist(s.ctx, input)
input = s.alias(input, s.aliases)
if err := s.readline.Parse(input); err != nil {
s.l.Error("failed to parse line:", err.Error())
return
@ -254,6 +276,8 @@ func (s *Prompt) complete(d prompt.Document) []prompt.Suggest {
return nil
}
input = s.alias(input, s.aliases)
if err := s.readline.Parse(input); err != nil {
s.l.Debug("failed to parse line:", err.Error())
return nil
@ -263,6 +287,9 @@ func (s *Prompt) complete(d prompt.Document) []prompt.Suggest {
// return root completion
if s.readline.IsModeDefault() && s.readline.Args().LenIs(0) {
var suggests []prompt.Suggest
for key, value := range s.aliases {
suggests = append(suggests, prompt.Suggest{Text: key, Description: "alias: " + value})
}
for _, inst := range s.Commands().List() {
suggests = append(suggests, prompt.Suggest{Text: inst.Name(), Description: inst.Description()})
}