1
1
#[ macro_use]
2
2
extern crate log;
3
- extern crate getopts;
4
-
5
- use getopts:: Options ;
6
- use rocket:: config:: Environment ;
7
3
8
4
use std:: env;
9
5
6
+ use clap:: { AppSettings , Clap } ;
7
+ use rocket:: config:: Environment ;
8
+
10
9
use aw_server:: * ;
11
10
12
11
#[ cfg( all( target_os = "linux" , target_arch = "x86" ) ) ]
@@ -15,36 +14,37 @@ extern crate jemallocator;
15
14
#[ global_allocator]
16
15
static ALLOC : jemallocator:: Jemalloc = jemallocator:: Jemalloc ;
17
16
18
- fn print_usage ( program : & str , opts : Options ) {
19
- let brief = format ! ( "Usage: {} FILE [options]" , program) ;
20
- print ! ( "{}" , opts. usage( & brief) ) ;
17
+ /// Rust server for ActivityWatch
18
+ #[ derive( Clap ) ]
19
+ #[ clap( version = "0.10" , author = "Johan Bjäreholt, Erik Bjäreholt" ) ]
20
+ #[ clap( setting = AppSettings :: ColoredHelp ) ]
21
+ struct Opts {
22
+ /// Run in testing mode
23
+ #[ clap( long) ]
24
+ testing : bool ,
25
+ /// Address to listen to
26
+ #[ clap( long) ]
27
+ host : Option < String > ,
28
+ /// Port to listen on
29
+ #[ clap( long) ]
30
+ port : Option < String > ,
31
+ /// Path to database override
32
+ #[ clap( long) ]
33
+ dbpath : Option < String > ,
34
+ /// Device ID override
35
+ #[ clap( long) ]
36
+ device_id : Option < String > ,
37
+ /// Don't import from aw-server-python if no aw-server-rust db found
38
+ #[ clap( long) ]
39
+ no_legacy_import : bool ,
21
40
}
22
41
23
42
fn main ( ) {
24
- use std:: sync:: Mutex ;
25
-
26
- let args: Vec < String > = env:: args ( ) . collect ( ) ;
27
- let program = args[ 0 ] . clone ( ) ;
28
-
29
- let mut opts = Options :: new ( ) ;
30
- opts. optflag ( "" , "testing" , "run in testing mode" ) ;
31
- opts. optflag ( "h" , "help" , "print this help menu" ) ;
32
- opts. optopt ( "" , "port" , "port to listent to" , "PORT" ) ;
33
- opts. optopt ( "" , "dbpath" , "path to database" , "PATH" ) ;
34
- opts. optopt ( "" , "device-id" , "device ID override" , "ID" ) ;
43
+ let opts: Opts = Opts :: parse ( ) ;
35
44
36
- opts. optflag ( "" , "no-legacy-import" , "don't import from aw-server-python" ) ;
37
-
38
- let matches = match opts. parse ( & args[ 1 ..] ) {
39
- Ok ( m) => m,
40
- Err ( f) => panic ! ( "{}" , f. to_string( ) ) ,
41
- } ;
42
- if matches. opt_present ( "h" ) {
43
- print_usage ( & program, opts) ;
44
- return ;
45
- }
45
+ use std:: sync:: Mutex ;
46
46
47
- let mut testing = matches . opt_present ( " testing" ) ;
47
+ let mut testing = opts . testing ;
48
48
// Always override environment if --testing is specified
49
49
if !testing {
50
50
let env = Environment :: active ( ) . expect ( "Failed to get current environment" ) ;
@@ -59,13 +59,18 @@ fn main() {
59
59
60
60
let mut config = config:: create_config ( testing) ;
61
61
62
- // Set port if overridden
63
- if let Ok ( Some ( port) ) = matches. opt_get ( "port" ) {
64
- config. port = port;
62
+ // set host if overridden
63
+ if let Some ( host) = opts. host {
64
+ config. address = host;
65
+ }
66
+
67
+ // set port if overridden
68
+ if let Some ( port) = opts. port {
69
+ config. port = port. parse ( ) . unwrap ( ) ;
65
70
}
66
71
67
72
// Set db path if overridden
68
- let db_path: String = if let Ok ( Some ( dbpath) ) = matches . opt_get ( " dbpath" ) {
73
+ let db_path: String = if let Some ( dbpath) = opts . dbpath {
69
74
dbpath
70
75
} else {
71
76
dirs:: db_path ( testing)
@@ -79,9 +84,9 @@ fn main() {
79
84
let asset_path = get_asset_path ( ) ;
80
85
info ! ( "Using aw-webui assets at path {:?}" , asset_path) ;
81
86
82
- let legacy_import = !matches . opt_present ( "no-legacy-import" ) ;
87
+ let legacy_import = !opts . no_legacy_import ;
83
88
84
- let device_id: String = if let Ok ( Some ( id) ) = matches . opt_get ( "device-id" ) {
89
+ let device_id: String = if let Some ( id) = opts . device_id {
85
90
id
86
91
} else {
87
92
device_id:: get_device_id ( )
0 commit comments