Skip to content

Commit

Permalink
feat: add list command
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Sep 30, 2022
1 parent dea5ba1 commit 68bfc56
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 25 deletions.
99 changes: 99 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cmd

import (
"fmt"
"os"
"sort"
"text/tabwriter"

"github.com/adikari/safebox/v2/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

// runCmd represents the exec command
var listCmd = &cobra.Command{
Use: "list",
Short: "Lists all the configs available",
RunE: list,
Example: `TODO: list command example`,
}

var (
sortByModified bool
sortByVersion bool
)

func init() {
listCmd.Flags().BoolVarP(&sortByModified, "modified", "m", false, "Sort by modified time")
listCmd.Flags().BoolVarP(&sortByVersion, "version", "v", false, "Sort by version")

rootCmd.AddCommand(listCmd)
}

func list(cmd *cobra.Command, args []string) error {
store, err := getStore()

if err != nil {
return errors.Wrap(err, "failed to instantiate store")
}

var keys []string

for _, value := range Config.Configs {
keys = append(keys, value.Key)
}

configs, err := store.GetMany(keys)

if err != nil {
return errors.Wrap(err, "failed to list params")
}

if sortByVersion {
sort.Sort(ByVersion(configs))
} else if sortByModified {
sort.Sort(ByModified(configs))
} else {
sort.Sort(ByName(configs))
}

w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0)

fmt.Fprint(w, "Name\tValue\tType\tVersion\tLastModified")
fmt.Fprintln(w, "")

for _, config := range configs {
fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%s",
*config.Name,
*config.Value,
config.Type,
config.Version,
config.Modified.Local().Format(TimeFormat),
)

fmt.Fprintln(w, "")
}

w.Flush()

return nil
}

type ByName []store.Config

func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool { return *a[i].Name < *a[j].Name }

type ByVersion []store.Config

func (a ByVersion) Len() int { return len(a) }
func (a ByVersion) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByVersion) Less(i, j int) bool { return a[i].Version < a[j].Version }

type ByModified []store.Config

func (a ByModified) Len() int { return len(a) }
func (a ByModified) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByModified) Less(i, j int) bool { return a[i].Modified.Before(a[j].Modified) }
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
stage string
pathToConfig string
Config *c.Config
TimeFormat = "2006-01-02 15:04:05"
)

var rootCmd = &cobra.Command{
Expand Down
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
var runCmd = &cobra.Command{
Use: "run",
Short: "Deploys all configurations specified in config file",
RunE: execute,
RunE: run,
Example: `TODO: run command example`,
}

func init() {
rootCmd.AddCommand(runCmd)
}

func execute(cmd *cobra.Command, args []string) error {
func run(cmd *cobra.Command, args []string) error {
store, err := getStore()

if err != nil {
Expand Down
47 changes: 33 additions & 14 deletions store/ssmstore.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package store

import (
"strconv"
"strings"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -94,7 +93,30 @@ func (s *SSMStore) Delete(key string) error {
}

func (s *SSMStore) GetMany(keys []string) ([]Config, error) {
return nil, nil
var names []*string

for _, key := range keys {
names = append(names, aws.String(key))
}

getParametersInput := &ssm.GetParametersInput{
Names: names,
WithDecryption: aws.Bool(true),
}

resp, err := s.svc.GetParameters(getParametersInput)

if err != nil {
return []Config{}, err
}

var params []Config

for _, param := range resp.Parameters {
params = append(params, parameterToConfig(param))
}

return params, nil
}

func (s *SSMStore) GetAll() ([]Config, error) {
Expand Down Expand Up @@ -151,8 +173,7 @@ func (s *SSMStore) Get(key string) (Config, error) {
}

return Config{
Value: param.Value,
Metadata: mapMetadata(parameter),
Value: param.Value,
}, nil
}

Expand All @@ -165,15 +186,13 @@ func basePath(key string) string {
return strings.Join(pathParts[0:end], "/")
}

func mapMetadata(p *ssm.ParameterMetadata) Metadata {
version := 0
if p.Description != nil {
version, _ = strconv.Atoi(*p.Description)
}
return Metadata{
Created: *p.LastModifiedDate,
CreatedBy: *p.LastModifiedUser,
Version: version,
Key: *p.Name,
func parameterToConfig(param *ssm.Parameter) Config {
return Config{
Name: param.Name,
Value: param.Value,
Modified: *param.LastModifiedDate,
Version: int(*param.Version),
Type: *param.Type,
DataType: *param.DataType,
}
}
15 changes: 6 additions & 9 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import (
"time"
)

type Metadata struct {
Created time.Time
CreatedBy string
Version int
Key string
}

type Config struct {
Value *string
Metadata
Name *string
Value *string
Modified time.Time
Version int
Type string
DataType string
}

type ConfigInput struct {
Expand Down

0 comments on commit 68bfc56

Please sign in to comment.