Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CSV output, error handling #42

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions gauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package main
import (
"bytes"
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"os/user"
"path"
"strconv"
"strings"
"syscall"
"text/tabwriter"
Expand All @@ -16,14 +18,20 @@ import (
"golang.org/x/crypto/ssh/terminal"
)

var (
useCSV = flag.Bool("csv", false, "Output CSV for easy machine processing")
)

func main() {
flag.Parse()

cfgPath := os.Getenv("GAUTH_CONFIG")
if cfgPath == "" {
user, err := user.Current()
u, err := user.Current()
if err != nil {
log.Fatal(err)
}
cfgPath = path.Join(user.HomeDir, ".config/gauth.csv")
cfgPath = path.Join(u.HomeDir, ".config/gauth.csv")
}

cfgContent, err := gauth.LoadConfigFile(cfgPath, getPassword)
Expand All @@ -42,22 +50,49 @@ func main() {

currentTS, progress := gauth.IndexNow()

tw := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
fmt.Fprintln(tw, "\tprev\tcurr\tnext")
for _, record := range cfg {
name, secret := record[0], record[1]
prev, curr, next, err := gauth.Codes(secret, currentTS)
if *useCSV {
cw := csv.NewWriter(os.Stdout)
for _, record := range cfg {
name, secret := record[0], record[1]
prev, curr, next, err := gauth.Codes(secret, currentTS)
if err != nil {
log.Fatalf("Generating codes: %v", err)
}
if err := cw.Write([]string{name, curr, next, prev, strconv.Itoa(30 - progress)}); err != nil {
log.Fatalf("Printing CSV: %v", err)
creachadair marked this conversation as resolved.
Show resolved Hide resolved
}
}
cw.Flush()
err := cw.Error()
if err != nil {
log.Fatalf("Generating codes: %v", err)
log.Fatalf("Flushing: %v", err)
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", name, prev, curr, next)
} else {
tw := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
if _, err := fmt.Fprintln(tw, "\tprev\tcurr\tnext"); err != nil {
log.Fatalf("Printing header: %v", err)
}
for _, record := range cfg {
name, secret := record[0], record[1]
prev, curr, next, err := gauth.Codes(secret, currentTS)
if err != nil {
log.Fatalf("Generating codes: %v", err)
}
if _, err := fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", name, prev, curr, next); err != nil {
log.Fatalf("Printing %v: %v", name, err)
}
}
if err := tw.Flush(); err != nil {
log.Fatalf("Flushing: %v", err)
}
fmt.Printf("[%-29s]\n", strings.Repeat("=", progress))
}
tw.Flush()
fmt.Printf("[%-29s]\n", strings.Repeat("=", progress))
}

func getPassword() ([]byte, error) {
fmt.Printf("Encryption password: ")
if _, err := fmt.Printf("Encryption password: "); err != nil {
log.Fatalf("Printing encryption password prompt")
}
defer fmt.Println()
return terminal.ReadPassword(int(syscall.Stdin))
return terminal.ReadPassword(syscall.Stdin)
}
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a h1:e3IU37lwO4aq3uoRKINC7JikojFmE5gO7xhfxs8VC34=
Expand Down