From cd7ebc81e40cda80d288d4c8e0857f79f032f196 Mon Sep 17 00:00:00 2001 From: rsteube Date: Fri, 18 Oct 2024 20:45:27 +0200 Subject: [PATCH] uid: include context --- action.go | 7 +++++-- action_test.go | 2 +- carapace_test.go | 2 +- defaultActions.go | 12 ++++++------ defaultActions_test.go | 2 +- internal/cache/cache.go | 2 +- internal/log/log.go | 3 ++- internal/shell/bash/snippet.go | 2 +- internal/shell/bash_ble/snippet.go | 3 ++- internal/shell/elvish/snippet.go | 2 +- internal/shell/fish/snippet.go | 2 +- internal/shell/nushell/snippet.go | 2 +- internal/shell/oil/snippet.go | 2 +- internal/shell/powershell/snippet.go | 2 +- internal/shell/tcsh/snippet.go | 2 +- internal/shell/xonsh/snippet.go | 2 +- internal/shell/zsh/snippet.go | 2 +- internalActions.go | 10 +++++----- {internal => pkg}/uid/uid.go | 6 ++++++ {internal => pkg}/uid/uid_test.go | 0 storage.go | 3 ++- 21 files changed, 41 insertions(+), 29 deletions(-) rename {internal => pkg}/uid/uid.go (93%) rename {internal => pkg}/uid/uid_test.go (100%) diff --git a/action.go b/action.go index 97474156..47bcea60 100644 --- a/action.go +++ b/action.go @@ -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. @@ -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() }) } diff --git a/action_test.go b/action_test.go index 6c9c24cc..d4dd6913 100644 --- a/action_test.go +++ b/action_test.go @@ -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() { diff --git a/carapace_test.go b/carapace_test.go index c8fb0e11..b9d83eb4 100644 --- a/carapace_test.go +++ b/carapace_test.go @@ -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" ) diff --git a/defaultActions.go b/defaultActions.go index 86d40aa0..6cf074b5 100644 --- a/defaultActions.go +++ b/defaultActions.go @@ -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" @@ -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 @@ -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 @@ -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") @@ -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) }) } @@ -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 diff --git a/defaultActions_test.go b/defaultActions_test.go index 4e6f663a..7afe311b 100644 --- a/defaultActions_test.go +++ b/defaultActions_test.go @@ -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" ) diff --git a/internal/cache/cache.go b/internal/cache/cache.go index d607b75a..26a96d1c 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -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" ) diff --git a/internal/log/log.go b/internal/log/log.go index f97142e8..99597b95 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -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()) diff --git a/internal/shell/bash/snippet.go b/internal/shell/bash/snippet.go index 3013bb8d..1a4beb37 100644 --- a/internal/shell/bash/snippet.go +++ b/internal/shell/bash/snippet.go @@ -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" ) diff --git a/internal/shell/bash_ble/snippet.go b/internal/shell/bash_ble/snippet.go index b3d8e94d..4c6ddcfa 100644 --- a/internal/shell/bash_ble/snippet.go +++ b/internal/shell/bash_ble/snippet.go @@ -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" ) diff --git a/internal/shell/elvish/snippet.go b/internal/shell/elvish/snippet.go index 95d18c93..dd6d2548 100644 --- a/internal/shell/elvish/snippet.go +++ b/internal/shell/elvish/snippet.go @@ -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" ) diff --git a/internal/shell/fish/snippet.go b/internal/shell/fish/snippet.go index 71efead2..c4fa2935 100644 --- a/internal/shell/fish/snippet.go +++ b/internal/shell/fish/snippet.go @@ -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" ) diff --git a/internal/shell/nushell/snippet.go b/internal/shell/nushell/snippet.go index 462c6f67..617e93c3 100644 --- a/internal/shell/nushell/snippet.go +++ b/internal/shell/nushell/snippet.go @@ -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" ) diff --git a/internal/shell/oil/snippet.go b/internal/shell/oil/snippet.go index 7699dcbd..1cc02019 100644 --- a/internal/shell/oil/snippet.go +++ b/internal/shell/oil/snippet.go @@ -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" ) diff --git a/internal/shell/powershell/snippet.go b/internal/shell/powershell/snippet.go index 5fe8740d..8eb85b51 100644 --- a/internal/shell/powershell/snippet.go +++ b/internal/shell/powershell/snippet.go @@ -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" ) diff --git a/internal/shell/tcsh/snippet.go b/internal/shell/tcsh/snippet.go index 1fa6a138..4592e813 100644 --- a/internal/shell/tcsh/snippet.go +++ b/internal/shell/tcsh/snippet.go @@ -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" ) diff --git a/internal/shell/xonsh/snippet.go b/internal/shell/xonsh/snippet.go index e9a467e7..c81386ec 100644 --- a/internal/shell/xonsh/snippet.go +++ b/internal/shell/xonsh/snippet.go @@ -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" ) diff --git a/internal/shell/zsh/snippet.go b/internal/shell/zsh/snippet.go index 94f2fb34..c2bc0e30 100644 --- a/internal/shell/zsh/snippet.go +++ b/internal/shell/zsh/snippet.go @@ -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" ) diff --git a/internalActions.go b/internalActions.go index 054f10d9..c1191e34 100644 --- a/internalActions.go +++ b/internalActions.go @@ -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" ) @@ -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]) } @@ -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 })) } } }) diff --git a/internal/uid/uid.go b/pkg/uid/uid.go similarity index 93% rename from internal/uid/uid.go rename to pkg/uid/uid.go index 04a98acb..e8051a99 100644 --- a/internal/uid/uid.go +++ b/pkg/uid/uid.go @@ -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()} diff --git a/internal/uid/uid_test.go b/pkg/uid/uid_test.go similarity index 100% rename from internal/uid/uid_test.go rename to pkg/uid/uid_test.go diff --git a/storage.go b/storage.go index fa83a5ce..b921900d 100644 --- a/storage.go +++ b/storage.go @@ -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" )