@@ -23,9 +23,10 @@ use clap::{Parser, Subcommand};
23
23
use aw_client_rust:: blocking:: AwClient ;
24
24
25
25
mod accessmethod;
26
+ mod dirs;
26
27
mod sync;
27
-
28
- const DEFAULT_PORT : & str = "5600" ;
28
+ mod sync_wrapper ;
29
+ mod util ;
29
30
30
31
#[ derive( Parser ) ]
31
32
#[ clap( version = "0.1" , author = "Erik Bjäreholt" ) ]
@@ -38,8 +39,8 @@ struct Opts {
38
39
host : String ,
39
40
40
41
/// Port of instance to connect to.
41
- #[ clap( long, default_value = DEFAULT_PORT ) ]
42
- port : String ,
42
+ #[ clap( long) ]
43
+ port : Option < String > ,
43
44
44
45
/// Convenience option for using the default testing host and port.
45
46
#[ clap( long) ]
@@ -48,42 +49,53 @@ struct Opts {
48
49
/// Enable debug logging.
49
50
#[ clap( long) ]
50
51
verbose : bool ,
51
-
52
- /// Full path to sync directory.
53
- /// If not specified, exit.
54
- #[ clap( long) ]
55
- sync_dir : String ,
56
-
57
- /// Full path to sync db file
58
- /// Useful for syncing buckets from a specific db file in the sync directory.
59
- /// Must be a valid absolute path to a file in the sync directory.
60
- #[ clap( long) ]
61
- sync_db : Option < String > ,
62
52
}
63
53
64
54
#[ derive( Subcommand ) ]
65
55
enum Commands {
66
- /// Sync subcommand.
56
+ /// Sync subcommand (basic)
57
+ ///
58
+ /// Pulls remote buckets then pushes local buckets.
59
+ Sync {
60
+ /// Host(s) to pull from, comma separated. Will pull from all hosts if not specified.
61
+ #[ clap( long) ]
62
+ host : Option < String > ,
63
+ } ,
64
+
65
+ /// Sync subcommand (advanced)
67
66
///
68
67
/// Pulls remote buckets then pushes local buckets.
69
68
/// First pulls remote buckets in the sync directory to the local aw-server.
70
69
/// Then pushes local buckets from the aw-server to the local sync directory.
71
70
#[ clap( arg_required_else_help = true ) ]
72
- Sync {
71
+ SyncAdvanced {
73
72
/// Date to start syncing from.
74
73
/// If not specified, start from beginning.
75
74
/// NOTE: might be unstable, as count cannot be used to verify integrity of sync.
76
75
/// Format: YYYY-MM-DD
77
76
#[ clap( long) ]
78
77
start_date : Option < String > ,
78
+
79
79
/// Specify buckets to sync using a comma-separated list.
80
80
/// If not specified, all buckets will be synced.
81
81
#[ clap( long) ]
82
82
buckets : Option < String > ,
83
+
83
84
/// Mode to sync in. Can be "push", "pull", or "both".
84
85
/// Defaults to "both".
85
86
#[ clap( long, default_value = "both" ) ]
86
87
mode : String ,
88
+
89
+ /// Full path to sync directory.
90
+ /// If not specified, exit.
91
+ #[ clap( long) ]
92
+ sync_dir : String ,
93
+
94
+ /// Full path to sync db file
95
+ /// Useful for syncing buckets from a specific db file in the sync directory.
96
+ /// Must be a valid absolute path to a file in the sync directory.
97
+ #[ clap( long) ]
98
+ sync_db : Option < String > ,
87
99
} ,
88
100
/// List buckets and their sync status.
89
101
List { } ,
@@ -95,35 +107,59 @@ fn main() -> Result<(), Box<dyn Error>> {
95
107
96
108
info ! ( "Started aw-sync..." ) ;
97
109
98
- aw_server:: logging:: setup_logger ( true , verbose) . expect ( "Failed to setup logging" ) ;
99
-
100
- let sync_directory = if opts. sync_dir . is_empty ( ) {
101
- println ! ( "No sync directory specified, exiting..." ) ;
102
- std:: process:: exit ( 1 ) ;
103
- } else {
104
- Path :: new ( & opts. sync_dir )
105
- } ;
106
- info ! ( "Using sync dir: {}" , sync_directory. display( ) ) ;
107
-
108
- if let Some ( sync_db) = & opts. sync_db {
109
- info ! ( "Using sync db: {}" , sync_db) ;
110
- }
110
+ aw_server:: logging:: setup_logger ( "aw-sync" , opts. testing , verbose)
111
+ . expect ( "Failed to setup logging" ) ;
111
112
112
- let port = if opts. testing && opts. port == DEFAULT_PORT {
113
- "5666"
114
- } else {
115
- & opts. port
116
- } ;
113
+ let port = opts
114
+ . port
115
+ . or_else ( || Some ( crate :: util:: get_server_port ( opts. testing ) . ok ( ) ?. to_string ( ) ) )
116
+ . unwrap ( ) ;
117
117
118
- let client = AwClient :: new ( opts. host . as_str ( ) , port, "aw-sync" ) ;
118
+ let client = AwClient :: new ( opts. host . as_str ( ) , port. as_str ( ) , "aw-sync" ) ;
119
119
120
120
match & opts. command {
121
+ // Perform basic sync
122
+ Commands :: Sync { host } => {
123
+ // Pull
124
+ match host {
125
+ Some ( host) => {
126
+ let hosts: Vec < & str > = host. split ( ',' ) . collect ( ) ;
127
+ for host in hosts. iter ( ) {
128
+ info ! ( "Pulling from host: {}" , host) ;
129
+ sync_wrapper:: pull ( host, & client) ?;
130
+ }
131
+ }
132
+ None => {
133
+ info ! ( "Pulling from all hosts" ) ;
134
+ sync_wrapper:: pull_all ( & client) ?;
135
+ }
136
+ }
137
+
138
+ // Push
139
+ info ! ( "Pushing local data" ) ;
140
+ sync_wrapper:: push ( & client) ?;
141
+ Ok ( ( ) )
142
+ }
121
143
// Perform two-way sync
122
- Commands :: Sync {
144
+ Commands :: SyncAdvanced {
123
145
start_date,
124
146
buckets,
125
147
mode,
148
+ sync_dir,
149
+ sync_db,
126
150
} => {
151
+ let sync_directory = if sync_dir. is_empty ( ) {
152
+ error ! ( "No sync directory specified, exiting..." ) ;
153
+ std:: process:: exit ( 1 ) ;
154
+ } else {
155
+ Path :: new ( & sync_dir)
156
+ } ;
157
+ info ! ( "Using sync dir: {}" , sync_directory. display( ) ) ;
158
+
159
+ if let Some ( sync_db) = & sync_db {
160
+ info ! ( "Using sync db: {}" , sync_db) ;
161
+ }
162
+
127
163
let start: Option < DateTime < Utc > > = start_date. as_ref ( ) . map ( |date| {
128
164
println ! ( "{}" , date. clone( ) ) ;
129
165
chrono:: NaiveDate :: parse_from_str ( & date. clone ( ) , "%Y-%m-%d" )
@@ -140,7 +176,7 @@ fn main() -> Result<(), Box<dyn Error>> {
140
176
. as_ref ( )
141
177
. map ( |b| b. split ( ',' ) . map ( |s| s. to_string ( ) ) . collect ( ) ) ;
142
178
143
- let sync_db: Option < PathBuf > = opts . sync_db . as_ref ( ) . map ( |db| {
179
+ let sync_db: Option < PathBuf > = sync_db. as_ref ( ) . map ( |db| {
144
180
let db_path = Path :: new ( db) ;
145
181
if !db_path. is_absolute ( ) {
146
182
panic ! ( "Sync db path must be absolute" ) ;
@@ -165,11 +201,11 @@ fn main() -> Result<(), Box<dyn Error>> {
165
201
_ => panic ! ( "Invalid mode" ) ,
166
202
} ;
167
203
168
- sync:: sync_run ( client, & sync_spec, mode_enum)
204
+ sync:: sync_run ( & client, & sync_spec, mode_enum)
169
205
}
170
206
171
207
// List all buckets
172
- Commands :: List { } => sync:: list_buckets ( & client, sync_directory ) ,
208
+ Commands :: List { } => sync:: list_buckets ( & client) ,
173
209
} ?;
174
210
175
211
// Needed to give the datastores some time to commit before program is shut down.
0 commit comments