2019-05-28 01:16:50 +00:00
|
|
|
package commands
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-06-01 20:32:11 +00:00
|
|
|
"time"
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2019-05-30 06:32:56 +00:00
|
|
|
"gopkg.in/AlecAivazis/survey.v1"
|
2019-06-01 20:32:11 +00:00
|
|
|
|
2019-07-11 02:33:22 +00:00
|
|
|
"github.com/mitchell/selfpass/services/credentials/types"
|
2019-05-22 15:22:40 +00:00
|
|
|
)
|
|
|
|
|
2019-08-12 18:51:39 +00:00
|
|
|
func makeList(initClient credentialsClientInit) *cobra.Command {
|
2019-08-31 21:19:25 +00:00
|
|
|
flags := credentialFlagSet{}.withCredFlags()
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
listCmd := &cobra.Command{
|
|
|
|
Use: "list",
|
|
|
|
Short: "List the metadata for all credentials",
|
|
|
|
Long: `List the metadata for all credentials, with the option to filter by source host. Metadata
|
|
|
|
includes almost all the information but the most sensitive.`,
|
|
|
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-08-31 21:19:25 +00:00
|
|
|
defer flags.resetValues()
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
2019-06-01 20:32:11 +00:00
|
|
|
defer cancel()
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
client := initClient(ctx)
|
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
mdch, errch := client.GetAllMetadata(ctx, flags.sourceHost)
|
|
|
|
var mds []types.Metadata
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-05-30 06:32:56 +00:00
|
|
|
fmt.Println()
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
receive:
|
2019-06-16 11:02:49 +00:00
|
|
|
for {
|
2019-05-22 15:22:40 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-06-12 23:51:29 +00:00
|
|
|
check(ctx.Err())
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
case err := <-errch:
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
case md, ok := <-mdch:
|
|
|
|
if !ok {
|
|
|
|
break receive
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
mds = append(mds, md)
|
2019-06-01 20:32:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-30 06:32:56 +00:00
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
var sources []string
|
|
|
|
mdmap := map[string][]types.Metadata{}
|
|
|
|
for _, md := range mds {
|
|
|
|
tmds := mdmap[md.SourceHost]
|
2019-05-30 06:32:56 +00:00
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
if tmds == nil {
|
|
|
|
mdmap[md.SourceHost] = []types.Metadata{md}
|
|
|
|
sources = append(sources, md.SourceHost)
|
|
|
|
continue
|
|
|
|
}
|
2019-06-03 09:28:43 +00:00
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
mdmap[md.SourceHost] = append(mdmap[md.SourceHost], md)
|
2019-06-01 20:32:11 +00:00
|
|
|
}
|
2019-05-30 06:32:56 +00:00
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
if flags.sourceHost == "" {
|
|
|
|
prompt := &survey.Select{
|
|
|
|
Message: "Source host:",
|
|
|
|
Options: sources,
|
|
|
|
PageSize: 20,
|
|
|
|
VimMode: true,
|
|
|
|
}
|
2019-06-01 20:32:11 +00:00
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
check(survey.AskOne(prompt, &flags.sourceHost, nil))
|
|
|
|
}
|
2019-07-23 00:24:00 +00:00
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
if len(mdmap[flags.sourceHost]) == 0 {
|
|
|
|
check(errSourceNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, md := range mdmap[flags.sourceHost] {
|
2019-06-01 20:32:11 +00:00
|
|
|
fmt.Println(md)
|
2019-05-22 15:22:40 +00:00
|
|
|
}
|
2019-05-28 01:16:50 +00:00
|
|
|
|
|
|
|
fmt.Println("Done listing.")
|
2019-05-22 15:22:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
flags.register(listCmd)
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
return listCmd
|
|
|
|
}
|