Skip to content

Commit

Permalink
Add CSV Format (#35)
Browse files Browse the repository at this point in the history
Adds a csv format and fixes a bug in the strip-prefixes flag of `ls`.
  • Loading branch information
kingishb committed Jul 2, 2022
1 parent 64ae23e commit d638680
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/csv"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -28,9 +29,11 @@ func main() {
stripPrefix := false
// print tuple of parameter histories
showHistory := false
// serialize output to csv
toCSV := false

app := cli.NewApp()
app.Version = "1.4.3"
app.Version = "1.5.0"
app.Usage = "simple ssm param store interface"
app.Flags = []cli.Flag{
cli.StringFlag{
Expand All @@ -49,6 +52,11 @@ func main() {
Usage: "print out parameter values in plaintext",
Destination: &secrets,
},
cli.BoolFlag{
Name: "csv",
Usage: "serialize output to csv",
Destination: &toCSV,
},
cli.BoolFlag{
Name: "hide-ts",
Usage: "prints keys in alphabetical order without timestamps (good for diffs)",
Expand Down Expand Up @@ -83,6 +91,19 @@ func main() {
if err != nil {
return err
}

if toCSV {
w := csv.NewWriter(os.Stdout)
for _, k := range keys {
err = w.Write(k)
if err != nil {
return err
}
}
w.Flush()
return nil

}
for _, key := range keys {
fmt.Println(strings.Join(key, "\t"))
}
Expand Down Expand Up @@ -208,20 +229,24 @@ type entry struct {
history []string
}

func removePrefix(path string) string {
s := strings.Split(path, "/")
return s[len(s)-1]
}

// fmt returns a formatted string with optional timestamp and parameter prefix.
func (e *entry) fmt(ts, stripPrefix bool) []string {
var val string
var name string
if stripPrefix {
s := strings.Split(e.val, "/")
val = s[len(s)-1]
name = removePrefix(e.name)
} else {
val = e.val
name = e.name
}
h := strings.Join(e.history, ", ")
if ts {
return []string{e.t.Format("2006-01-02 15:04:05"), e.name, val, h}
return []string{e.t.Format("2006-01-02 15:04:05"), name, e.val, h}
}
return []string{e.name, val, h}
return []string{name, e.val, h}
}

// history returns the parameter history of a value.
Expand Down

0 comments on commit d638680

Please sign in to comment.