mtgadmin/
mtgadmin.rs

1// Import required libraries.
2extern crate daemonize;
3
4use daemonize::Daemonize;
5use fs::File;
6use std::fs;
7    
8// function() -> Get directory with MTGA game logs.
9pub fn get_log_dir(mtg_arena_tool_log_dir: &str) {
10
11    // Import required libraries.
12    use std::process::Command;
13    
14    // Check if MTG Arena Tool log dir exists.
15    let mut binding = Command::new("test");
16    let logdir = binding.arg("-d").arg(mtg_arena_tool_log_dir);
17        
18    // Uncomment to debug.
19    //println!("Debug info: {:?}", logdir.output());
20
21    // Tell user what MTG Arena Tool log directory we are monitoring.
22    println!(
23        "Monitoring MTG Arena Tool log directory {}",
24        mtg_arena_tool_log_dir
25    );
26
27}
28
29/////  BEGIN: fn daemonize()  /////
30pub fn daemonize() {
31
32    let stdout = File::create("/tmp/mtgadmin.out").unwrap();
33    let stderr = File::create("/tmp/mtgadmin.err").unwrap();
34
35    let daemonize = Daemonize::new()
36        .pid_file("/tmp/mtgadmin.pid") // Every method except `new` and `start`
37        .chown_pid_file(false) // is optional, see `Daemonize` documentation
38        .working_directory("/tmp") // for default behaviour.
39        .user("nobody")
40        .group("daemon") // Group name
41        .group(1) // or group id.
42        .umask(0o027) // Set umask, `0o027` by default.
43        .stdout(stdout) // Redirect stdout to `/tmp/mtgadmin.out`.
44        .stderr(stderr) // Redirect stderr to `/tmp/mtgadmin.err`.
45        .privileged_action(|| "Executed before drop privileges");
46
47    match daemonize.start() {
48        Ok(_) => println!("Successfully daemonized mtgadmin...!"),
49        Err(e) => eprintln!("Unknown error! mtgadmin failed to daemonize...!, {}", e),
50    }
51
52    /////  END: fn daemonize()  /////
53}
54
55/////  BEGIN: fn get_api_endpoint()  /////
56pub fn get_api_endpoint(api_endpoint: &str) {
57
58    //use std::process::Command;
59
60    // Tell user what API endpoint we're about to upload to
61    println!("API endpoint to upload game data to: {}", api_endpoint);
62
63    // Did user supply a supported API endpoint?
64    // Possible values: 17lands
65    // Uncomment below to debug
66    //println!("{}", api_endpoint.eq("17lands"));
67    
68    if api_endpoint.eq("17lands") {
69        // Tell user '17lands' is supported.
70        println!("17lands is a supported API endpoint!");
71    }
72
73    /////  END: fn get_api_endpoint()
74}
75
76/////  upload_game_data_to_17lands()  /////
77pub fn upload_game_data_to_17lands(mtg_arena_tool_log_dir: &str, api_endpoint: &str) {
78
79    // Import required libraries
80    use std::process::Command;
81    extern crate execute;
82    
83    // Tell user we are uploading MTGA game data to '17lands' API endpoint
84    println!("Uploading MTGA game data to API endpoint {}", api_endpoint);
85
86    // Upload MTGA game data to API endpoint '17lands'
87    for entry in fs::read_dir(mtg_arena_tool_log_dir).unwrap() {
88        let entry = entry.unwrap();
89        let path = entry.path();
90
91        if path.is_dir() {
92            // Found a directory not an MTGA game log file.
93            println!("{:?} is a dir", path);
94        } else {
95            // Found a file. Hopefully it's an MTGA game log file.
96            println!("{:?} appears to be an MTGA game log file", path);
97            let mut binding = Command::new("seventeenlands");
98            let landscmd = binding
99
100                .arg("-l")
101                .arg(mtg_arena_tool_log_dir.to_owned() + "{:?}");
102        
103            // Uncomment to debug.
104            println!("Debug info: {:?}", landscmd.output());
105            println!("Uploading MTGA game data to 17lands...");
106            
107        }
108    
109    }
110    ///// END: upload_game_data_to_17lands() /////
111}