-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
132 lines (109 loc) · 3.02 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"errors"
"fmt"
"github.com/revengel/enpass2gopass/store"
"github.com/revengel/enpass2gopass/store/enpass"
"github.com/revengel/enpass2gopass/store/gopass"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
EnpassJsonSourceType = "enpassJsonSource"
GopassDestinationType = "gopassDestination"
KeepassDestinationType = "keepassDestination"
)
type app struct {
ctx context.Context
logger *logrus.Logger
source store.StoreSource
destination store.StoreDestination
}
func (a *app) Close() error {
var err error
if a.destination != nil {
err = a.destination.Close()
if err != nil {
return err
}
}
return nil
}
func (a *app) SetLogLevel(cmd *cobra.Command, args []string) error {
debug, _ := cmd.Flags().GetBool("debug")
if debug {
a.logger.SetLevel(logrus.DebugLevel)
return nil
}
logLevelStr, _ := cmd.Flags().GetString("log-level")
if logLevelStr == "" {
return nil
}
lvl, err := logrus.ParseLevel(logLevelStr)
if err != nil {
a.logger.Warnf("cannot parse log level '%s': %s", logLevelStr, err.Error())
return nil
}
a.logger.SetLevel(lvl)
return nil
}
func (a *app) Before(cmd *cobra.Command, args []string) error {
var err error
err = a.SetLogLevel(cmd, args)
if err != nil {
return err
}
prefix, _ := cmd.Flags().GetString("prefix")
dryRun, _ := cmd.Flags().GetBool("dry-run")
sourceProvider, _ := cmd.Flags().GetString("source-provider")
destProvider, _ := cmd.Flags().GetString("destination-provider")
switch sourceProvider {
case EnpassJsonSourceType:
enpassJsonPath, _ := cmd.Flags().GetString("source-enpass-json-path")
if enpassJsonPath == "" {
return errors.New("source enpass json file is not set")
}
a.source, err = enpass.NewEnpassJsonSource(enpassJsonPath)
default:
return fmt.Errorf("invalid source provider: %s", sourceProvider)
}
if err != nil {
return fmt.Errorf("failed to connect source: %s", err)
}
switch destProvider {
case GopassDestinationType:
a.destination, err = gopass.NewStore(a.ctx, prefix, dryRun, a.logger)
default:
return fmt.Errorf("invalid destination provider: %s", destProvider)
}
if err != nil {
return fmt.Errorf("failed to connect destination: %s", err)
}
return nil
}
func (a *app) Import(cmd *cobra.Command, args []string) error {
items, err := a.source.LoadData()
if err != nil {
return fmt.Errorf("Cannot load data from source: %s", err.Error())
}
for _, item := range items {
secretPath, err := item.GetSecretPath()
if err != nil {
return fmt.Errorf("cannot get seceret path: %s", err.Error())
}
fields, err := item.GetFields()
if err != nil {
return fmt.Errorf("cannot get item fields; secret key - '%s': %s", secretPath, err.Error())
}
_, err = a.destination.Save(fields, secretPath)
if err != nil {
return fmt.Errorf("cannot save secret; secret key - '%s': %s", secretPath, err.Error())
}
}
_, err = a.destination.Cleanup()
if err != nil {
return fmt.Errorf("cannot cleanup passwords storage: %s", err.Error())
}
return nil
}