dentimoer_setup/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::process::Command;
use std::fs;
use std::io;
use std::path::Path;
use regex::Regex;
use std::time::Duration;

pub fn install_essential_equipments() {
    let path = "C:\\hello_world";
    let var_name = "OPENCV_INCLUDE_PATHS";
    
    let powershell_command = format!(
        "[Environment]::SetEnvironmentVariable('{}', '{}', 'Machine')",
        var_name,
        path
    );

    // 명시적으로 관리자 권한으로 새 PowerShell 프로세스를 실행
    let command = format!(
        "Start-Process powershell -Verb RunAs -ArgumentList \"-NoProfile -ExecutionPolicy Bypass -Command {}\" -Wait",
        powershell_command
    );

    match run_elevated_command(&command) {
        Ok(_) => {
            println!("환경 변수가 성공적으로 설정되었습니다.");
            
            // C:\ 에서 ls 명령어 실행
            match Command::new("powershell")
                .args(&["-Command", "Set-Location C:\\; ls"])
                .output() 
            {
                Ok(output) => {
                    println!("\nC:\\ 디렉토리의 ls 명령어 출력:");
                    println!("{}", String::from_utf8_lossy(&output.stdout));
                    
                    // C:\Users 에서 dir 명령어 실행
                    match Command::new("cmd")
                        .args(&["/C", "cd C:\\Users && dir"])
                        .output() 
                    {
                        Ok(dir_output) => {
                            println!("\nC:\\Users 디렉토리의 dir 명령어 출력:");
                            println!("{}", String::from_utf8_lossy(&dir_output.stdout));
                        }
                        Err(e) => eprintln!("dir 명령어 실행 실패: {}", e),
                    }
                }
                Err(e) => eprintln!("ls 명령어 실행 실패: {}", e),
            }
        }
        Err(e) => eprintln!("환경 변수 설정 중 오류 발생: {}", e),
    }
}

fn run_elevated_command(cmd: &str) -> io::Result<()> {
    let output = Command::new("powershell")
        .args(&[
            "-NoProfile",
            "-ExecutionPolicy",
            "Bypass",
            "-Command",
            cmd
        ])
        .output()?;

    if output.status.success() {
        Ok(())
    } else {
        let error_message = String::from_utf8_lossy(&output.stderr);
        Err(io::Error::new(
            io::ErrorKind::Other,
            format!("명령어 실행 실패: {}", error_message)
        ))
    }
}