From 19872eee702af5e56813873b7373f7605ba3e0a3 Mon Sep 17 00:00:00 2001 From: rsteube Date: Thu, 30 Nov 2023 23:06:20 +0100 Subject: [PATCH] added experimental functions --- experimental.go | 32 ++++++++++++++++++++++++++++++++ pkg/x/x.go | 10 ++++++++++ 2 files changed, 42 insertions(+) create mode 100644 experimental.go create mode 100644 pkg/x/x.go diff --git a/experimental.go b/experimental.go new file mode 100644 index 000000000..4d0645c9f --- /dev/null +++ b/experimental.go @@ -0,0 +1,32 @@ +package carapace + +import ( + "encoding/json" + + "github.com/rsteube/carapace/internal/config" + "github.com/rsteube/carapace/internal/export" + "github.com/rsteube/carapace/pkg/x" + "github.com/spf13/cobra" +) + +func init() { + x.ClearStorage = func() { + storage = make(_storage) + } + + x.Complete = func(cmd *cobra.Command, args ...string) (*export.Export, error) { + initHelpCompletion(cmd) + action, context := traverse(cmd, args[2:]) + + if err := config.Load(); err != nil { + return nil, err + } + + output := action.Invoke(context).value("export", "") + var e export.Export + if err := json.Unmarshal([]byte(output), &e); err != nil { + return nil, err + } + return &e, nil + } +} diff --git a/pkg/x/x.go b/pkg/x/x.go new file mode 100644 index 000000000..06d73009a --- /dev/null +++ b/pkg/x/x.go @@ -0,0 +1,10 @@ +// Package x contains experimental functions +package x + +import ( + "github.com/rsteube/carapace/internal/export" + "github.com/spf13/cobra" +) + +var ClearStorage func() +var Complete func(cmd *cobra.Command, args ...string) (*export.Export, error)