Merge pull request #28 from creachadair/formatting
Clean up output formatting code.
This commit is contained in:
commit
c217b384f0
21
gauth.go
21
gauth.go
@ -9,10 +9,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"text/tabwriter"
|
||||||
|
|
||||||
"github.com/pcarrier/gauth/gauth"
|
"github.com/pcarrier/gauth/gauth"
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
@ -74,31 +76,20 @@ func main() {
|
|||||||
prevTS := currentTS - 1
|
prevTS := currentTS - 1
|
||||||
nextTS := currentTS + 1
|
nextTS := currentTS + 1
|
||||||
|
|
||||||
wordSize := 0
|
tw := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
|
||||||
for _, record := range cfg {
|
fmt.Fprintln(tw, "\tprev\tcurr\tnext")
|
||||||
actualSize := len([]rune(record[0]))
|
|
||||||
if actualSize > wordSize {
|
|
||||||
wordSize = actualSize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var header = "prev curr next"
|
|
||||||
fmt.Println(leftPad(header, " ", wordSize+1))
|
|
||||||
for _, record := range cfg {
|
for _, record := range cfg {
|
||||||
name := record[0]
|
name := record[0]
|
||||||
secret := normalizeSecret(record[1])
|
secret := normalizeSecret(record[1])
|
||||||
prevToken := authCodeOrDie(secret, prevTS)
|
prevToken := authCodeOrDie(secret, prevTS)
|
||||||
currentToken := authCodeOrDie(secret, currentTS)
|
currentToken := authCodeOrDie(secret, currentTS)
|
||||||
nextToken := authCodeOrDie(secret, nextTS)
|
nextToken := authCodeOrDie(secret, nextTS)
|
||||||
fmt.Printf("%-*s %s %s %s\n", wordSize, name, prevToken, currentToken, nextToken)
|
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", name, prevToken, currentToken, nextToken)
|
||||||
}
|
}
|
||||||
|
tw.Flush()
|
||||||
fmt.Printf("[%-29s]\n", strings.Repeat("=", progress))
|
fmt.Printf("[%-29s]\n", strings.Repeat("=", progress))
|
||||||
}
|
}
|
||||||
|
|
||||||
func leftPad(s string, padStr string, pLen int) string {
|
|
||||||
return strings.Repeat(padStr, pLen) + s
|
|
||||||
}
|
|
||||||
|
|
||||||
// normalizeSecret cleans up whitespace and adds any missing padding to sec to
|
// normalizeSecret cleans up whitespace and adds any missing padding to sec to
|
||||||
// use it as an OTP seed.
|
// use it as an OTP seed.
|
||||||
func normalizeSecret(sec string) string {
|
func normalizeSecret(sec string) string {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user