-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
163 lines (150 loc) · 3.94 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"go.uber.org/zap"
"github.com/ristomcgehee/lyncser/filestore"
"github.com/ristomcgehee/lyncser/sync"
"github.com/ristomcgehee/lyncser/utils"
)
const appVersion = "v0.1.20"
var rootCmd = &cobra.Command{
Use: "lyncser",
}
//nolint:gochecknoinits
func init() {
syncCmd := &cobra.Command{
Use: "sync",
Short: "Syncs the files that are configured to be synced.",
Run: syncCmd,
}
addCommonFlags(syncCmd)
syncCmd.Flags().BoolP("force-download", "f", false, "Forces download of all files")
syncCmd.Flags().BoolP("dont-encrypt", "d", false, "Don't encrypt files. By default, files are encrypted.")
rootCmd.AddCommand(syncCmd)
deleteFilesCmd := &cobra.Command{
Use: "deleteAllRemoteFiles",
Short: "Deletes all files in the remote file store.",
Run: deleteRemoteFiles,
}
addCommonFlags(deleteFilesCmd)
deleteFilesCmd.Flags().BoolP("yes", "y", false, "Confirm deletion of all remote files")
rootCmd.AddCommand(deleteFilesCmd)
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the version number of lyncser",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(appVersion)
},
}
rootCmd.AddCommand(versionCmd)
}
func addCommonFlags(cmd *cobra.Command) {
cmd.Flags().StringP("log-level", "l", "info", "The log level to use. One of: debug, info, warn, error, fatal")
}
func getLogger(cmd *cobra.Command) (*zap.SugaredLogger, error) {
cfg := zap.NewProductionConfig()
logLevel, err := cmd.Flags().GetString("log-level")
if err != nil {
return nil, err
}
if err := cfg.Level.UnmarshalText([]byte(logLevel)); err != nil {
return nil, err
}
cfg.Encoding = "console"
logger, err := cfg.Build()
if err != nil {
return nil, err
}
//nolint:errcheck
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
return sugar, nil
}
func syncCmd(cmd *cobra.Command, args []string) {
logger, err := getLogger(cmd)
if err != nil {
panic(err)
}
forceDownload, err := cmd.Flags().GetBool("force-download")
if err != nil {
logger.Warn("error getting force-download flag", zap.Error(err))
}
remoteFileStore := getRemoteFileStore(logger)
if err != nil {
logger.Panic(err)
}
dontEncrypt, err := cmd.Flags().GetBool("dont-encrypt")
if err != nil {
logger.Warn("error getting dont-encrypt flag", zap.Error(err))
}
var encryptor utils.ReaderEncryptor
if dontEncrypt {
encryptor = &utils.NopEncryptor{}
} else {
encryptionKey, err := sync.GetEncryptionKey()
if err != nil {
logger.Panic(err)
}
encryptor = &utils.AESGCMEncryptor{
Key: encryptionKey,
}
}
syncer := sync.Syncer{
RemoteFileStore: remoteFileStore,
LocalFileStore: &filestore.LocalFileStore{},
Logger: logger,
Encryptor: encryptor,
ForceDownload: forceDownload,
}
if err = syncer.PerformSync(); err != nil {
logger.Panic(err)
}
}
func deleteRemoteFiles(cmd *cobra.Command, args []string) {
logger, err := getLogger(cmd)
if err != nil {
panic(err)
}
remoteFileStore := getRemoteFileStore(logger)
if err != nil {
logger.Panic(err)
}
files, err := remoteFileStore.GetFiles()
if err != nil {
logger.Panic(err)
}
yes, err := cmd.Flags().GetBool("yes")
if err != nil {
logger.Warn("error getting yes flag", zap.Error(err))
}
if !yes {
fmt.Printf("This will delete all %d files in the remote file store. Are you sure you want to continue? (y/n): ",
len(files))
var input string
fmt.Scanln(&input)
if input != "y" {
return
}
}
if err = remoteFileStore.DeleteAllFiles(); err != nil {
logger.Panic(err)
}
logger.Infof("Deleted %d files", len(files))
}
func getRemoteFileStore(logger utils.Logger) filestore.FileStore {
return &filestore.DriveFileStore{
Logger: logger,
}
}
func main() {
// Check for version flag before executing the root command
if len(os.Args) == 2 && (os.Args[1] == "-v" || os.Args[1] == "--version") {
fmt.Println(appVersion)
return
}
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}