11use clap:: arg;
2+ use environ:: Env ;
23use nix:: {
34 sys:: stat:: { umask, Mode } ,
4- unistd,
55 unistd:: daemon,
66} ;
7+ use std:: io:: Read ;
8+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
79use std:: {
810 env, fs,
911 fs:: OpenOptions ,
10- io:: prelude:: * ,
1112 os:: unix:: net:: UnixListener ,
1213 path:: Path ,
1314 process:: { exit, id, Command , Stdio } ,
14- time:: { SystemTime , UNIX_EPOCH } ,
1515} ;
1616use sysinfo:: { ProcessExt , System , SystemExt } ;
1717
18+ mod environ;
19+
1820fn main ( ) -> std:: io:: Result < ( ) > {
1921 let app = clap:: Command :: new ( "swhks" )
2022 . version ( env ! ( "CARGO_PKG_VERSION" ) )
@@ -38,7 +40,10 @@ fn main() -> std::io::Result<()> {
3840 log:: trace!( "Setting process umask." ) ;
3941 umask ( Mode :: S_IWGRP | Mode :: S_IWOTH ) ;
4042
41- let ( pid_file_path, sock_file_path) = get_file_paths ( ) ;
43+ // This is used to initialize the environment variables only once
44+ let environment = environ:: Env :: construct ( ) ;
45+
46+ let ( pid_file_path, sock_file_path) = get_file_paths ( & environment) ;
4247
4348 let log_file_name = if let Some ( val) = args. value_of ( "log" ) {
4449 val. to_string ( )
@@ -51,29 +56,7 @@ fn main() -> std::io::Result<()> {
5156 }
5257 } ;
5358
54- match env:: var ( "XDG_DATA_HOME" ) {
55- Ok ( val) => {
56- log:: info!(
57- "XDG_DATA_HOME Variable is present, using it's value for default file path."
58- ) ;
59- format ! ( "{}/swhks/swhks-{}.log" , val, time)
60- }
61- Err ( e) => {
62- log:: trace!(
63- "XDG_DATA_HOME Variable is not set, falling back on hardcoded path.\n Error: {:#?}" ,
64- e
65- ) ;
66- match env:: var ( "HOME" ) {
67- Ok ( val) => format ! ( "{}/.local/share/swhks/swhks-{}.log" , val, time) ,
68- Err ( _) => {
69- log:: error!(
70- "HOME Variable is not set, cannot fall back on hardcoded path for XDG_DATA_HOME."
71- ) ;
72- exit ( 1 ) ;
73- }
74- }
75- }
76- }
59+ format ! ( "{}/swhks/swhks-{}.log" , environment. data_home. to_string_lossy( ) , time)
7760 } ;
7861
7962 let log_path = Path :: new ( & log_file_name) ;
@@ -142,27 +125,11 @@ fn main() -> std::io::Result<()> {
142125 }
143126}
144127
145- fn get_file_paths ( ) -> ( String , String ) {
146- match env:: var ( "XDG_RUNTIME_DIR" ) {
147- Ok ( val) => {
148- log:: info!(
149- "XDG_RUNTIME_DIR Variable is present, using it's value as default file path."
150- ) ;
151-
152- let pid_file_path = format ! ( "{}/swhks.pid" , val) ;
153- let sock_file_path = format ! ( "{}/swhkd.sock" , val) ;
128+ fn get_file_paths ( env : & Env ) -> ( String , String ) {
129+ let pid_file_path = format ! ( "{}/swhks.pid" , env. runtime_dir. to_string_lossy( ) ) ;
130+ let sock_file_path = format ! ( "{}/swhkd.sock" , env. runtime_dir. to_string_lossy( ) ) ;
154131
155- ( pid_file_path, sock_file_path)
156- }
157- Err ( e) => {
158- log:: trace!( "XDG_RUNTIME_DIR Variable is not set, falling back on hardcoded path.\n Error: {:#?}" , e) ;
159-
160- let pid_file_path = format ! ( "/run/user/{}/swhks.pid" , unistd:: Uid :: current( ) ) ;
161- let sock_file_path = format ! ( "/run/user/{}/swhkd.sock" , unistd:: Uid :: current( ) ) ;
162-
163- ( pid_file_path, sock_file_path)
164- }
165- }
132+ ( pid_file_path, sock_file_path)
166133}
167134
168135fn run_system_command ( command : & str , log_path : & Path ) {
0 commit comments