Skip to content

Commit

Permalink
extracted context creation function
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Nov 12, 2022
1 parent cfbaaa1 commit 4a482c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
15 changes: 1 addition & 14 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package carapace
import (
"fmt"
"io"
"os"
"strings"

"github.com/rsteube/carapace/internal/common"
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "="
Expand Down

0 comments on commit 4a482c2

Please sign in to comment.