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

csv output option #34

Open
jesseorr opened this issue Aug 6, 2020 · 1 comment
Open

csv output option #34

jesseorr opened this issue Aug 6, 2020 · 1 comment
Assignees

Comments

@jesseorr
Copy link

jesseorr commented Aug 6, 2020

It would be nice to have a flag to do CSV output as well as the normal output, so the data can be read in more easily by scripts.

@jesseorr
Copy link
Author

jesseorr commented Aug 6, 2020

index 59df65f..526016b 100644
--- a/gauth.go
+++ b/gauth.go
@@ -6,6 +6,7 @@ import (
        "crypto/cipher"
        "crypto/sha256"
        "encoding/csv"
+       "flag"
        "fmt"
        "io/ioutil"
        "log"
@@ -21,6 +22,9 @@ import (
 )

 func main() {
+       parseable := flag.Bool("csv",false,"Use to enable machine parseable output: Name, Current, Next, TTL")
+       flag.Parse()
+
        user, e := user.Current()
        if e != nil {
                log.Fatal(e)
@@ -75,15 +79,24 @@ 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 err != nil {
                        log.Fatalf("Code: %v", err)
                }
-               fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", name, prev, curr, next)
+               if *parseable {
+                       fmt.Fprintf(tw,"%s,%s,%s,%d\n",name, curr, next, 30 - progress)
+               } else {
+                       fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", name, prev, curr, next)
+               }
+       }
+
+       if ! *parseable {
+               fmt.Fprintln(tw, "\tprev\tcurr\tnext")
+               tw.Flush()
+               fmt.Printf("[%-29s]\n", strings.Repeat("=", progress))
+       } else {
+               tw.Flush()
        }
-       tw.Flush()
-       fmt.Printf("[%-29s]\n", strings.Repeat("=", progress))
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants