This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
gootp/main.go

120 lines
2.4 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"github.com/Kichiyaki/gootp/internal"
"github.com/urfave/cli/v2"
"golang.org/x/term"
"log"
"os"
"syscall"
)
var Version = "development"
func main() {
app := newApp()
if err := app.Run(os.Args); err != nil {
log.Fatalln("app.Run:", err)
}
}
func newApp() *cli.App {
return &cli.App{
Name: "gootp",
Version: Version,
Action: func(c *cli.Context) error {
var err error
password := []byte(c.String("password"))
if len(password) == 0 {
password, err = readPasswordFromStdin()
}
if err != nil {
return err
}
b, err := os.ReadFile(c.String("path"))
if err != nil {
return fmt.Errorf("something went wrong while reading file: %w", err)
}
entries, err := internal.DecryptAsEntries(password, b)
if err != nil {
return fmt.Errorf("something went wrong while decrypting file: %w", err)
}
for _, entry := range entries {
otp, err := internal.GenerateOTP(entry)
if err != nil {
log.Printf("%s - %s: %s", entry.Issuer, entry.Label, err)
continue
}
log.Printf("%s - %s: %s", entry.Issuer, entry.Label, otp)
}
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
Usage: "path to encrypted andotp file",
Required: true,
},
&cli.StringFlag{
Name: "password",
Usage: "encryption password",
Required: false,
},
},
EnableBashCompletion: true,
Commands: []*cli.Command{
newDecryptCommand(),
},
}
}
func newDecryptCommand() *cli.Command {
return &cli.Command{
Name: "decrypt",
Usage: "decrypt backup file generated by andotp",
Action: func(c *cli.Context) error {
var err error
password := []byte(c.String("password"))
if len(password) == 0 {
password, err = readPasswordFromStdin()
}
if err != nil {
return err
}
b, err := os.ReadFile(c.String("path"))
if err != nil {
return fmt.Errorf("something went wrong while reading file: %w", err)
}
result, err := internal.Decrypt(password, b)
if err != nil {
return fmt.Errorf("something went wrong while decrypting file: %w", err)
}
fmt.Print(string(result))
return nil
},
}
}
func readPasswordFromStdin() ([]byte, error) {
fmt.Print("Password: ")
pass, err := term.ReadPassword(syscall.Stdin)
if err != nil {
return nil, fmt.Errorf("term.ReadPassword: %w", err)
}
fmt.Print("\n")
return pass, nil
}