Skip to content

Commit

Permalink
Break output functions into their own files; colorize yaml output
Browse files Browse the repository at this point in the history
  • Loading branch information
gdanko committed Aug 20, 2024
1 parent c438ecd commit b5a9ec0
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 88 deletions.
36 changes: 36 additions & 0 deletions pkg/output/default-output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package output

import (
"fmt"

"github.com/fatih/color"
"github.com/gdanko/enpass/pkg/enpass"
)

func doDefaultOutput(cards []enpass.Card, cmdType string) {
for i, cardItem := range cards {
if cmdType == "list" {
c := color.New(color.FgCyan)
title := c.Sprintf("[%05d] >", i+1)
fmt.Printf(
"%s title: %s, login: %s, category: %s\n",
title,
cardItem.Title,
cardItem.Subtitle,
cardItem.Category,
)
} else if cmdType == "show" {
c := color.New(color.FgRed)
title := c.Sprintf("[%05d] >", i+1)
fmt.Printf(
"%s title: %s, login: %s, category: %s, %s: %s\n",
title,
cardItem.Title,
cardItem.Subtitle,
cardItem.Category,
cardItem.Type,
cardItem.DecryptedValue,
)
}
}
}
26 changes: 26 additions & 0 deletions pkg/output/json-output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package output

import (
"fmt"

"github.com/gdanko/enpass/pkg/enpass"
"github.com/hokaccha/go-prettyjson"
"github.com/sirupsen/logrus"
)

func doJsonOutput(logger *logrus.Logger, cards []enpass.Card, nocolorFlag bool) {
disabledColor := false
if nocolorFlag {
disabledColor = true
}
formatter := prettyjson.NewFormatter()
formatter.DisabledColor = disabledColor
formatter.Indent = 4

jsonBytes, err := formatter.Marshal(cards)
if err != nil {
logger.Errorf("failed to parse the output to JSON, %s", err)
logger.Exit(2)
}
fmt.Println(string(jsonBytes))
}
26 changes: 26 additions & 0 deletions pkg/output/list-output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package output

import (
"fmt"

"github.com/gdanko/enpass/pkg/enpass"
)

func doListOutput(cards []enpass.Card, cmdType string) {
length := 10
for i, cardItem := range cards {
fmt.Printf("%*s = %s\n", length, "uuid", cardItem.UUID)
fmt.Printf("%*s = %s\n", length, "title", cardItem.Title)
fmt.Printf("%*s = %s\n", length, "login", cardItem.Subtitle)
fmt.Printf("%*s = %s\n", length, "category", cardItem.Category)
fmt.Printf("%*s = %s\n", length, "note", cardItem.Note)
fmt.Printf("%*s = %v\n", length, "sensitive", cardItem.Sensitive)
fmt.Printf("%*s = %v\n", length, "raw", cardItem.RawValue)
if cmdType == "show" {
fmt.Printf("%*s = %s: %s\n", length, "type", cardItem.Type, cardItem.DecryptedValue)
}
if i < len(cards)-1 {
fmt.Println()
}
}
}
99 changes: 11 additions & 88 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ package output
import (
"fmt"
"os"
"strings"

"github.com/fatih/color"
"github.com/gdanko/enpass/pkg/enpass"
"github.com/hokaccha/go-prettyjson"
"github.com/markkurossi/tabulate"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

func GenerateOutput(logger *logrus.Logger, cmdType string, jsonFlag, listFlag, tableFlag, trashedFlag, yamlFlag, nocolorFlag bool, cards *[]enpass.Card) {
var (
// err error
// jsonBytes []byte
yamlString string
)
const escape = "\x1b"

func format(attr color.Attribute) string {
return fmt.Sprintf("%s[%dm", escape, attr)
}

func GenerateOutput(logger *logrus.Logger, cmdType string, jsonFlag, listFlag, tableFlag, trashedFlag, yamlFlag, nocolorFlag bool, cards *[]enpass.Card) {
if len(*cards) <= 0 {
fmt.Println("No cards found matching the specified criteria")
os.Exit(0)
Expand Down Expand Up @@ -51,87 +47,14 @@ func GenerateOutput(logger *logrus.Logger, cmdType string, jsonFlag, listFlag, t
cards = &cardsPruned

if jsonFlag {
disabledColor := false
if nocolorFlag {
disabledColor = true
}
formatter := prettyjson.NewFormatter()
formatter.DisabledColor = disabledColor
formatter.Indent = 4

jsonBytes, err := formatter.Marshal(cards)
if err != nil {
logger.Errorf("failed to parse the output to JSON, %s", err)
logger.Exit(2)
}
fmt.Println(string(jsonBytes))

doJsonOutput(logger, *cards, nocolorFlag)
} else if yamlFlag {
yamlBytes, _ := yaml.Marshal(cards)
yamlString = string(yamlBytes)
yamlString = strings.TrimSpace(yamlString)
fmt.Println(yamlString)
doYamlOutput(logger, *cards, nocolorFlag)
} else if listFlag {
length := 10
for i, cardItem := range *cards {
fmt.Printf("%*s = %s\n", length, "uuid", cardItem.UUID)
fmt.Printf("%*s = %s\n", length, "title", cardItem.Title)
fmt.Printf("%*s = %s\n", length, "login", cardItem.Subtitle)
fmt.Printf("%*s = %s\n", length, "category", cardItem.Category)
fmt.Printf("%*s = %s\n", length, "note", cardItem.Note)
fmt.Printf("%*s = %v\n", length, "sensitive", cardItem.Sensitive)
fmt.Printf("%*s = %v\n", length, "raw", cardItem.RawValue)
if cmdType == "show" {
fmt.Printf("%*s = %s: %s\n", length, "type", cardItem.Type, cardItem.DecryptedValue)
}
if i < len(*cards)-1 {
fmt.Println()
}
}
doListOutput(*cards, cmdType)
} else if tableFlag {
tab := tabulate.New(tabulate.Simple)
tab.Header("title").SetAlign(tabulate.ML)
tab.Header("login").SetAlign(tabulate.ML)
tab.Header("category").SetAlign(tabulate.ML)
if cmdType == "show" {
tab.Header("decrypted").SetAlign(tabulate.ML)
}
for _, cardItem := range *cards {
row := tab.Row()
row.Column(cardItem.Title)
row.Column(cardItem.Subtitle)
row.Column(cardItem.Category)
if cmdType == "show" {
password := fmt.Sprintf("%s: %s", cardItem.Type, cardItem.DecryptedValue)
row.Column(password)
}
}
tab.Print(os.Stdout)
doTableOutput(*cards, cmdType)
} else {
for i, cardItem := range *cards {
if cmdType == "list" {
c := color.New(color.FgCyan)
title := c.Sprintf("[%05d] >", i+1)
fmt.Printf(
"%s title: %s, login: %s, category: %s\n",
title,
cardItem.Title,
cardItem.Subtitle,
cardItem.Category,
)
} else if cmdType == "show" {
c := color.New(color.FgRed)
title := c.Sprintf("[%05d] >", i+1)
fmt.Printf(
"%s title: %s, login: %s, category: %s, %s: %s\n",
title,
cardItem.Title,
cardItem.Subtitle,
cardItem.Category,
cardItem.Type,
cardItem.DecryptedValue,
)
}
}
doDefaultOutput(*cards, cmdType)
}
}
30 changes: 30 additions & 0 deletions pkg/output/table-output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package output

import (
"fmt"
"os"

"github.com/gdanko/enpass/pkg/enpass"
"github.com/markkurossi/tabulate"
)

func doTableOutput(cards []enpass.Card, cmdType string) {
tab := tabulate.New(tabulate.Simple)
tab.Header("title").SetAlign(tabulate.ML)
tab.Header("login").SetAlign(tabulate.ML)
tab.Header("category").SetAlign(tabulate.ML)
if cmdType == "show" {
tab.Header("decrypted").SetAlign(tabulate.ML)
}
for _, cardItem := range cards {
row := tab.Row()
row.Column(cardItem.Title)
row.Column(cardItem.Subtitle)
row.Column(cardItem.Category)
if cmdType == "show" {
password := fmt.Sprintf("%s: %s", cardItem.Type, cardItem.DecryptedValue)
row.Column(password)
}
}
tab.Print(os.Stdout)
}
80 changes: 80 additions & 0 deletions pkg/output/yaml-output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package output

import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/gdanko/enpass/pkg/enpass"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/lexer"
"github.com/goccy/go-yaml/printer"
"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
)

var (
err error
yamlBytes []byte
yamlString string
)

func doYamlOutput(logger *logrus.Logger, cards []enpass.Card, nocolorFlag bool) {
yamlBytes, err = yaml.Marshal(cards)
if err != nil {
logger.Errorf("failed to parse the output to YAML, %s", err)
logger.Exit(2)
}

if nocolorFlag {
yamlString = string(yamlBytes)
yamlString = strings.TrimSpace(yamlString)
fmt.Println(yamlString)
} else {
tokens := lexer.Tokenize(string(yamlBytes))
var p printer.Printer
p.LineNumber = false
p.LineNumberFormat = func(num int) string {
fn := color.New(color.Bold, color.FgHiWhite).SprintFunc()
return fn(fmt.Sprintf("%2d | ", num))
}
p.Bool = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiMagenta),
Suffix: format(color.Reset),
}
}
p.Number = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiMagenta),
Suffix: format(color.Reset),
}
}
p.MapKey = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiCyan),
Suffix: format(color.Reset),
}
}
p.Anchor = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiYellow),
Suffix: format(color.Reset),
}
}
p.Alias = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiYellow),
Suffix: format(color.Reset),
}
}
p.String = func() *printer.Property {
return &printer.Property{
Prefix: format(color.FgHiGreen),
Suffix: format(color.Reset),
}
}
writer := colorable.NewColorableStdout()
writer.Write([]byte(p.PrintTokens(tokens) + "\n"))
}
}

0 comments on commit b5a9ec0

Please sign in to comment.