-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break output functions into their own files; colorize yaml output
- Loading branch information
Showing
6 changed files
with
209 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
} |