@@ -56,56 +56,42 @@ pub fn user_config_setup(config_path: &str, home_dir: &str, cfg_name: &str) -> i
56
56
}
57
57
}
58
58
59
- /// sets up root config in /root directory
60
- pub fn setup_root_config ( home_dir : & str ) -> i8 {
61
- let oh_my_zsh_src = format ! ( "{}{}" , home_dir, "/.oh-my-zsh" ) ;
62
- let oh_my_zsh_dest = String :: from ( "/root/.oh-my-zsh" ) ;
63
- let zshrc_src = format ! ( "{}{}" , home_dir, "/.zshrc" ) ;
64
- let zshrc_dest = String :: from ( "/root/.zshrc" ) ;
65
- let vimrc_src = format ! ( "{}{}" , home_dir, "/.vimrc" ) ;
66
- let vimrc_dest = String :: from ( "/root/.vimrc" ) ;
59
+ /// Helper function to copy a file or directory as root.
60
+ /// Takes source path, destination path, and a description for messages.
61
+ /// Uses 'cp -r' via run_sudo_command.
62
+ /// Returns 0 on success, 1 on failure.
63
+ fn copy_item_as_root ( src : & str , dest : & str , description : & str ) -> i8 {
64
+ let args = & [ "-r" , src, dest] ;
67
65
68
- // Create symbolic link for .oh-my-zsh
69
- match run_sudo_command (
70
- "cp" ,
71
- & [ "-r" , oh_my_zsh_src. as_str ( ) , oh_my_zsh_dest. as_str ( ) ] ,
72
- ) {
73
- Ok ( _) => println ! ( "/root/.oh-my-zsh {}" , "created configuration" . green( ) ) ,
66
+ match run_sudo_command ( "cp" , args) {
67
+ Ok ( _) => {
68
+ println ! ( "{} {}" , description, "created configuration" . green( ) ) ;
69
+ return 0 ;
70
+ }
74
71
Err ( e) => {
75
- eprintln ! (
76
- "{}{}" ,
77
- "error creating configuration /root/.oh-my-zsh:" . red( ) ,
78
- e. red( )
79
- ) ;
72
+ eprintln ! ( "{} failed to copy '{}' to '{}': {}" ,
73
+ "error:" . red( ) , src, dest, e. red( ) ) ;
80
74
return 1 ;
81
75
}
82
76
}
77
+ }
83
78
84
- // Create symbolic link for .zshrc
85
- match run_sudo_command ( "cp" , & [ "-r" , zshrc_src. as_str ( ) , zshrc_dest. as_str ( ) ] ) {
86
- Ok ( _) => println ! ( "/root/.zshrc {}" , "created configuration" . green( ) ) ,
87
- Err ( e) => {
88
- eprintln ! (
89
- "{}{}" ,
90
- "error creating configuration /root/.zshrc:" . red( ) ,
91
- e. red( )
92
- ) ;
93
- return 2 ;
94
- }
95
- }
96
79
97
- // Create symbolic link for .vimrc
98
- match run_sudo_command ( "cp" , & [ "-r" , vimrc_src. as_str ( ) , vimrc_dest. as_str ( ) ] ) {
99
- Ok ( _) => println ! ( "/root/.vimrc {}" , "created configuration" . green( ) ) ,
100
- Err ( e) => {
101
- eprintln ! (
102
- "{}{}" ,
103
- "error creating configuration /root/.vimrc:" . red( ) ,
104
- e. red( )
105
- ) ;
106
- return 3 ;
80
+ /// sets up root config in /root directory by copying files/directories from user's home
81
+ /// Note: Copies .oh-my-zsh, .zshrc, and .vimrc using 'cp -r' via sudo.
82
+ pub fn setup_root_config ( home_dir : & str ) -> i8 {
83
+ let items_to_copy = [
84
+ ( format ! ( "{}/.oh-my-zsh" , home_dir) , "/root/.oh-my-zsh" . to_string ( ) , "/root/.oh-my-zsh" ) ,
85
+ ( format ! ( "{}/.zshrc" , home_dir) , "/root/.zshrc" . to_string ( ) , "/root/.zshrc" ) ,
86
+ ( format ! ( "{}/.vimrc" , home_dir) , "/root/.vimrc" . to_string ( ) , "/root/.vimrc" ) ,
87
+ ] ;
88
+
89
+ for ( src, dest, desc) in & items_to_copy {
90
+ let status = copy_item_as_root ( src. as_str ( ) , dest. as_str ( ) , desc) ;
91
+ if status != 0 {
92
+ return 1 ;
107
93
}
108
94
}
109
95
110
96
return 0 ;
111
- }
97
+ }
0 commit comments