From dbe4f9722bbd63f685b36606abbfd97cbf1dfb10 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 22 Jun 2024 16:35:47 +0700 Subject: all: implement command "export" The "export" command export all issuers to file or standard output, $ gotp export [FILE] List of known supported FORMAT is: uri. If FILE is not defined it will print to standard output. The list of exported issuers are printed in order of its label. --- cli.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'cli.go') diff --git a/cli.go b/cli.go index a28f40a..a2bcbf5 100644 --- a/cli.go +++ b/cli.go @@ -7,6 +7,7 @@ import ( _ "embed" "encoding/base32" "fmt" + "io" "os" "path/filepath" "sort" @@ -72,6 +73,48 @@ func (cli *Cli) Add(issuer *Issuer) (err error) { return nil } +// Export all the issuers and its secret to the file or standard output. +// List of supported format: "uri". +func (cli *Cli) Export(w io.Writer, formatName string) (err error) { + var logp = `Export` + + formatName = strings.ToLower(formatName) + switch formatName { + case formatNameURI: + default: + return fmt.Errorf(`%s: unknown format name %q`, logp, formatName) + } + + err = cli.cfg.loadPrivateKey() + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + + var ( + labels = cli.List() + issuers = make([]*Issuer, 0, len(labels)) + + label string + issuer *Issuer + ) + for _, label = range labels { + issuer, err = cli.cfg.get(label) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + issuers = append(issuers, issuer) + } + + if formatName == formatNameURI { + err = exportAsURI(w, issuers) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + } + + return nil +} + // Generate n number of OTP from given issuer name. func (cli *Cli) Generate(label string, n int) (listOtp []string, err error) { var ( -- cgit v1.3