diff --git a/complete.go b/complete.go index 1da0517b6..d91afb42d 100644 --- a/complete.go +++ b/complete.go @@ -3,7 +3,6 @@ package carapace import ( "fmt" "io" - "os" "strings" "github.com/rsteube/carapace/internal/common" @@ -42,19 +41,7 @@ func complete(cmd *cobra.Command, args []string) (string, error) { return ActionMessage(err.Error()).Invoke(Context{CallbackValue: current}).value(shell, current), nil } - wd, err := os.Getwd() - if err != nil { - return ActionMessage(err.Error()).Invoke(Context{CallbackValue: current}).value(shell, current), nil - } - context := Context{ - CallbackValue: current, - Args: targetArgs, - Env: os.Environ(), - Dir: wd, - } - if err != nil { - return ActionMessage(err.Error()).Invoke(context).value(shell, current), nil - } + context := newContext(append(targetArgs, current)) // TODO needs more cleanup and tests var targetAction Action diff --git a/context.go b/context.go index 55c86c6d2..b939a3924 100644 --- a/context.go +++ b/context.go @@ -25,6 +25,20 @@ type Context struct { Dir string } +func newContext(args []string) Context { + context := Context{ + CallbackValue: args[len(args)-1], + Args: args[:len(args)-1], + Parts: []string{}, + Env: os.Environ(), + } + + if wd, err := os.Getwd(); err == nil { + context.Dir = wd + } + return context +} + // LookupEnv retrieves the value of the environment variable named by the key. func (c *Context) LookupEnv(key string) (string, bool) { prefix := key + "="