From 6d103ced0513954369de80a08cbfd5d50cf15445 Mon Sep 17 00:00:00 2001 From: Chris Quigley Date: Mon, 16 Dec 2024 12:54:58 -0500 Subject: [PATCH] First commit --- cli/cli.go | 1 + cli/keyring_list.go | 44 +++++++++++++++++++++++++++++++++++++++ internal/planner/group.go | 8 ++++--- keyring/file.go | 24 +++++++++++++++++++++ keyring/keyring.go | 2 ++ keyring/system.go | 6 ++++++ 6 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 cli/keyring_list.go diff --git a/cli/cli.go b/cli/cli.go index 8f9d3fcbd1..faeaa26d1f 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -137,6 +137,7 @@ func NewDefraCommand() *cobra.Command { MakeKeyringGenerateCommand(), MakeKeyringImportCommand(), MakeKeyringExportCommand(), + MakeKeyringListCommand(), ) identity := MakeIdentityCommand() diff --git a/cli/keyring_list.go b/cli/keyring_list.go new file mode 100644 index 0000000000..c17ccd23a6 --- /dev/null +++ b/cli/keyring_list.go @@ -0,0 +1,44 @@ +package cli + +import ( + "github.com/spf13/cobra" +) + +// MakeKeyringListCommand creates a new command to list all keys in the keyring. +func MakeKeyringListCommand() *cobra.Command { + var cmd = &cobra.Command{ + Use: "list", + Short: "List all keys in the keyring", + Long: `List all keys in the keyring. +The DEFRA_KEYRING_SECRET environment variable must be set to unlock the keyring. +This can also be done with a .env file in the working directory or at a path +defined with the --secret-file flag. + +Example: + defradb keyring list`, + Args: cobra.NoArgs, // No arguments expected + RunE: func(cmd *cobra.Command, args []string) error { + keyring, err := openKeyring(cmd) + if err != nil { + return err + } + + keyNames, err := keyring.List() + if err != nil { + return err + } + + if len(keyNames) == 0 { + cmd.Println("No keys found in the keyring.") + return nil + } + + cmd.Println("Keys in the keyring:") + for _, keyName := range keyNames { + cmd.Println("- " + keyName) + } + return nil + }, + } + return cmd +} diff --git a/internal/planner/group.go b/internal/planner/group.go index 900f0ff412..f4afe4a88f 100644 --- a/internal/planner/group.go +++ b/internal/planner/group.go @@ -11,6 +11,8 @@ package planner import ( + "fmt" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/client/request" "github.com/sourcenetwork/defradb/internal/core" @@ -59,9 +61,6 @@ type groupExecInfo struct { } // Creates a new group node. - -// The function is recursive and will construct the node-chain for any child (`_group`) collections. -// `groupSelect` is optional and will typically be nil if the child `_group` is not requested. func (p *Planner) GroupBy(n *mapper.GroupBy, parsed *mapper.Select, childSelects []*mapper.Select) (*groupNode, error) { if n == nil { return nil, nil @@ -83,6 +82,9 @@ func (p *Planner) GroupBy(n *mapper.GroupBy, parsed *mapper.Select, childSelects // group by fields have to be propagated downwards to ensure correct sub-grouping, otherwise child // groups will only group on the fields they explicitly reference childSelect.GroupBy.Fields = append(childSelect.GroupBy.Fields, n.Fields...) + for _, field := range childSelect.GroupBy.Fields { + fmt.Println("field", field) + } } dataSources = append(dataSources, newDataSource(childSelect.Index)) } diff --git a/keyring/file.go b/keyring/file.go index a8f7532274..00c968ba41 100644 --- a/keyring/file.go +++ b/keyring/file.go @@ -64,3 +64,27 @@ func (f *fileKeyring) Delete(user string) error { } return err } + +// List is a method that lists all keys in the file-based keyring directory. +func (f *fileKeyring) List() ([]string, error) { + return listKeys(f.dir) +} + +// listKeys is a helper function to list all keys in the given keyring directory. +func listKeys(keyringDir string) ([]string, error) { + // Read all the files in the keyring directory + files, err := os.ReadDir(keyringDir) + if err != nil { + return nil, err // Return an error if the directory cannot be read + } + + // Extract file names + var keyNames []string + for _, file := range files { + if !file.IsDir() { // Only include files, not subdirectories + keyNames = append(keyNames, file.Name()) + } + } + + return keyNames, nil +} diff --git a/keyring/keyring.go b/keyring/keyring.go index 603c25bb78..f7fa5961a1 100644 --- a/keyring/keyring.go +++ b/keyring/keyring.go @@ -24,4 +24,6 @@ type Keyring interface { // // If a key with that name does not exist `ErrNotFound` is returned. Delete(name string) error + // List returns a list of all keys in the keyring, used by the CLI 'keyring list' command + List() ([]string, error) } diff --git a/keyring/system.go b/keyring/system.go index 81575b5501..46ee03e1ca 100644 --- a/keyring/system.go +++ b/keyring/system.go @@ -53,3 +53,9 @@ func (s *systemKeyring) Get(name string) ([]byte, error) { func (s *systemKeyring) Delete(user string) error { return keyring.Delete(s.service, user) } + +func (s *systemKeyring) List() ([]string, error) { + // List does not need to be returned here + // This function is a stub + return nil, nil +}