aboutsummaryrefslogtreecommitdiff
path: root/cli.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-06-22 16:35:47 +0700
committerShulhan <ms@kilabit.info>2024-06-22 17:21:56 +0700
commitdbe4f9722bbd63f685b36606abbfd97cbf1dfb10 (patch)
treeb1eb61d3f75aa60f65541ba51408d3d4a708b48d /cli.go
parent4eaf76a096a0ba8ba5e687ede23c1dafd9064358 (diff)
downloadgotp-dbe4f9722bbd63f685b36606abbfd97cbf1dfb10.tar.xz
all: implement command "export"
The "export" command export all issuers to file or standard output, $ gotp export <FORMAT> [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.
Diffstat (limited to 'cli.go')
-rw-r--r--cli.go43
1 files changed, 43 insertions, 0 deletions
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 (