Skip to content

Commit

Permalink
uid: include context
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 18, 2024
1 parent a3537a5 commit cd7ebc8
Show file tree
Hide file tree
Showing 21 changed files with 41 additions and 29 deletions.
7 changes: 5 additions & 2 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/carapace-sh/carapace/pkg/match"
"github.com/carapace-sh/carapace/pkg/style"
pkgtraverse "github.com/carapace-sh/carapace/pkg/traverse"
"github.com/carapace-sh/carapace/pkg/uid"
)

// Action indicates how to complete a flag or positional argument.
Expand Down Expand Up @@ -503,9 +504,11 @@ func (a Action) Uid(scheme, host string, opts ...string) Action {
}

// UidF TODO experimental
func (a Action) UidF(f func(s string) (*url.URL, error)) Action {
func (a Action) UidF(f func(s string, uc uid.Context) (*url.URL, error)) Action {
return ActionCallback(func(c Context) Action {
return a.Invoke(c).UidF(f).ToA()
return a.Invoke(c).UidF(func(s string) (*url.URL, error) {
return f(s, c)
}).ToA()
})
}

Expand Down
2 changes: 1 addition & 1 deletion action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/carapace-sh/carapace/internal/assert"
"github.com/carapace-sh/carapace/internal/common"
"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/carapace-sh/carapace/pkg/uid"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion carapace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/carapace-sh/carapace/internal/assert"
"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
12 changes: 6 additions & 6 deletions defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"github.com/carapace-sh/carapace/internal/env"
"github.com/carapace-sh/carapace/internal/export"
"github.com/carapace-sh/carapace/internal/man"
"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/match"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/carapace-sh/carapace/third_party/github.com/acarl005/stripansi"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -138,7 +138,7 @@ func ActionDirectories() Action {
return actionPath([]string{""}, true).
MultiParts("/").
StyleF(style.ForPath).
UidF(func(s string) (*url.URL, error) { // TODO duplicated from ActionFiles
UidF(func(s string, uc uid.Context) (*url.URL, error) { // TODO duplicated from ActionFiles
abs, err := c.Abs(s)
if err != nil {
return nil, err
Expand All @@ -154,7 +154,7 @@ func ActionFiles(suffix ...string) Action {
return actionPath(suffix, false).
MultiParts("/").
StyleF(style.ForPath).
UidF(func(s string) (*url.URL, error) {
UidF(func(s string, uc uid.Context) (*url.URL, error) {
abs, err := c.Abs(s)
if err != nil {
return nil, err
Expand Down Expand Up @@ -457,7 +457,7 @@ func ActionExecutables(dirs ...string) Action {
batch = append(batch, actionDirectoryExecutables(dirs[i], c.Value, manDescriptions))
}
return batch.ToA().
UidF(func(s string) (*url.URL, error) {
UidF(func(s string, uc uid.Context) (*url.URL, error) {
return &url.URL{Scheme: "cmd", Host: s}, nil
})
}).Tag("executables")
Expand All @@ -479,7 +479,7 @@ func actionDirectoryExecutables(dir string, prefix string, manDescriptions map[s
}
}
}
return ActionStyledValuesDescribed(vals...).UidF(func(s string) (*url.URL, error) {
return ActionStyledValuesDescribed(vals...).UidF(func(s string, uc uid.Context) (*url.URL, error) {
return url.Parse(fmt.Sprintf("file://%v/%v", dir, s)) // TODO trim slash suffix from dir | backslash path possible? (windows)
})
}
Expand Down Expand Up @@ -547,7 +547,7 @@ func ActionCommands(cmd *cobra.Command) Action {
}
}
}
return batch.ToA().UidF(func(s string) (*url.URL, error) {
return batch.ToA().UidF(func(s string, uc uid.Context) (*url.URL, error) {
uid := uid.Command(cmd)
if subCommand, _, err := cmd.Find([]string{s}); err == nil {
s = subCommand.Name() // alias -> actual name
Expand Down
2 changes: 1 addition & 1 deletion defaultActions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"
"testing"

"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"github.com/carapace-sh/carapace/internal/env"
"github.com/carapace-sh/carapace/internal/export"
"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/cache/key"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/carapace-sh/carapace/pkg/xdg"
)

Expand Down
3 changes: 2 additions & 1 deletion internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"os"

"github.com/carapace-sh/carapace/internal/env"
"github.com/carapace-sh/carapace/internal/uid"

"github.com/carapace-sh/carapace/pkg/ps"
"github.com/carapace-sh/carapace/pkg/uid"
)

var LOG = log.New(io.Discard, "", log.Flags())
Expand Down
2 changes: 1 addition & 1 deletion internal/shell/bash/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package bash
import (
"fmt"

"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
3 changes: 2 additions & 1 deletion internal/shell/bash_ble/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"regexp"

"github.com/carapace-sh/carapace/internal/shell/bash"
"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"

"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/shell/elvish/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package elvish
import (
"fmt"

"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/shell/fish/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package fish
import (
"fmt"

"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/shell/nushell/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package nushell
import (
"fmt"

"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/shell/oil/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package oil
import (
"fmt"

"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/shell/powershell/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package powershell
import (
"fmt"

"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/shell/tcsh/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package tcsh
import (
"fmt"

"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/shell/xonsh/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strings"

"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/shell/zsh/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package zsh
import (
"fmt"

"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

Expand Down
10 changes: 5 additions & 5 deletions internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/carapace-sh/carapace/internal/env"
"github.com/carapace-sh/carapace/internal/pflagfork"
"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/carapace-sh/carapace/pkg/uid"
"github.com/carapace-sh/carapace/pkg/util"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -112,7 +112,7 @@ func actionFlags(cmd *cobra.Command) Action {
}
}
batch = append(batch, ActionStyledValuesDescribed(f.Shorthand, f.Usage, f.Style()).Tag("shorthand flags").
UidF(func(s string) (*url.URL, error) { return uid.Flag(cmd, f), nil }))
UidF(func(s string, uc uid.Context) (*url.URL, error) { return uid.Flag(cmd, f), nil }))
if f.IsOptarg() {
nospace = append(nospace, []rune(f.Shorthand)[0])
}
Expand All @@ -121,15 +121,15 @@ func actionFlags(cmd *cobra.Command) Action {
switch f.Mode() {
case pflagfork.NameAsShorthand:
batch = append(batch, ActionStyledValuesDescribed("-"+f.Name, f.Usage, f.Style()).Tag("longhand flags").
UidF(func(s string) (*url.URL, error) { return uid.Flag(cmd, f), nil }))
UidF(func(s string, uc uid.Context) (*url.URL, error) { return uid.Flag(cmd, f), nil }))
case pflagfork.Default:
batch = append(batch, ActionStyledValuesDescribed("--"+f.Name, f.Usage, f.Style()).Tag("longhand flags").
UidF(func(s string) (*url.URL, error) { return uid.Flag(cmd, f), nil }))
UidF(func(s string, uc uid.Context) (*url.URL, error) { return uid.Flag(cmd, f), nil }))
}

if f.Shorthand != "" && f.ShorthandDeprecated == "" {
batch = append(batch, ActionStyledValuesDescribed("-"+f.Shorthand, f.Usage, f.Style()).Tag("shorthand flags").
UidF(func(s string) (*url.URL, error) { return uid.Flag(cmd, f), nil }))
UidF(func(s string, uc uid.Context) (*url.URL, error) { return uid.Flag(cmd, f), nil }))
}
}
})
Expand Down
6 changes: 6 additions & 0 deletions internal/uid/uid.go → pkg/uid/uid.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"github.com/spf13/cobra"
)

type Context interface {
Abs(s string) (string, error)
Getenv(key string) string
LookupEnv(key string) (string, bool)
}

// Command creates a uid for given command.
func Command(cmd *cobra.Command) *url.URL {
path := []string{cmd.Name()}
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"sync"

"github.com/carapace-sh/carapace/internal/common"
"github.com/carapace-sh/carapace/internal/uid"
"github.com/carapace-sh/carapace/pkg/uid"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down

0 comments on commit cd7ebc8

Please sign in to comment.