diff --git a/CHANGELOG.md b/CHANGELOG.md index d7a1711..f1310b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/) - Use go module to get this software - Generate random password - Print the expiration time of TOTP code +- Export a wallet in json ## Changed diff --git a/gpm/cli.go b/gpm/cli.go index c229b2a..ad2236d 100644 --- a/gpm/cli.go +++ b/gpm/cli.go @@ -254,3 +254,14 @@ func (c *Cli) copyEntry() { } } } + +func (c *Cli) ExportWallet() { + c.loadWallet() + + data, err := c.Wallet.Export() + if err != nil { + c.error(fmt.Sprintf("%s", err)) + } + + fmt.Println(data) +} diff --git a/gpm/main.go b/gpm/main.go index b9b88c5..094ab0f 100644 --- a/gpm/main.go +++ b/gpm/main.go @@ -37,6 +37,7 @@ var( DIGIT = flag.Bool("digit", false, "use digit to generate a random password") LETTER = flag.Bool("letter", false, "use letter to generate a random password") SPECIAL = flag.Bool("special", false, "use special chars to generate a random password") + EXPORT = flag.Bool("export", false, "export a wallet in json format") HELP = flag.Bool("help", false, "print this help message") ) @@ -61,5 +62,7 @@ func Run() { cli.updateEntry() } else if *DELETE { cli.deleteEntry() + } else if *EXPORT { + cli.ExportWallet() } } diff --git a/gpm/wallet.go b/gpm/wallet.go index 13a7137..a8dd555 100644 --- a/gpm/wallet.go +++ b/gpm/wallet.go @@ -189,3 +189,13 @@ func (w *Wallet) UpdateEntry(entry Entry) error { return fmt.Errorf("unknown error during the update") } + +// Export a wallet to json +func (w *Wallet) Export() (string, error) { + data, err := json.Marshal(&w.Entries) + if err != nil { + return "", err + } + + return string(data), nil +}