-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.go
63 lines (54 loc) · 1.27 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
var cmdExport = &Command{
UsageLine: "export file",
Short: "export passman store",
Long: `
JSON-formatted, defaults to stdout.
`,
}
func init() {
cmdExport.Run = runExport
// cmdExport.Flag.StringVar(&exportOutput, "o", "", "")
// cmdExport.Flag.StringVar(&exportOutput, "output", "", "")
}
// Writes a JSON-formatted output of the password store to stdout or file.
func runExport(cmd *Command, args []string) {
var err error
var out *os.File = os.Stdout
s := openStore()
if len(args) > 0 {
filename := args[0]
if _, err := os.Stat(filename); err == nil {
fatalf("passman init: '%s' already exists", filename)
}
out, err = os.OpenFile(filename, storeFileCreateFlag, storeFilePerm)
if err != nil {
fatalf("passman export: %s", err)
}
defer out.Close()
}
// Serialize store and output it
content, err := json.MarshalIndent(s, "", " ")
if err != nil {
fatalf("%s", err)
}
if _, err = out.Write(content); err != nil {
fatalf("%s", err)
}
if _, err = fmt.Fprintln(out); err != nil {
fatalf("%s", err)
}
if out != os.Stdout {
if path, err := filepath.Abs(out.Name()); err != nil {
panic(err)
} else {
fmt.Printf("Created export file at '%s'\n", path)
}
}
}