-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from PowerShell/export_scenario
ProcessList native resource
- Loading branch information
Showing
5 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "process" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
sysinfo = "*" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = { version = "1.0", features = ["preserve_order"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"manifestVersion": "1.0", | ||
"description": "Returns information about running processes.", | ||
"type": "Microsoft/ProcessList", | ||
"version": "0.1.0", | ||
"get": { | ||
"executable": "process", | ||
"args": [ | ||
"get" | ||
] | ||
}, | ||
"set": { | ||
"executable": "process", | ||
"args": [ | ||
"set" | ||
], | ||
"input": "stdin", | ||
"preTest": true, | ||
"return": "state" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
mod process_info; | ||
use std::env; | ||
use std::process::exit; | ||
use sysinfo::{ProcessExt, System, SystemExt, PidExt}; | ||
|
||
fn print_task_list() { | ||
|
||
let mut s = System::new(); | ||
s.refresh_processes(); | ||
for (pid, process) in s.processes() { | ||
let mut p = process_info::ProcessInfo::new(); | ||
p.pid = pid.as_u32(); | ||
p.name = String::from(process.name()); | ||
p.cmdline = format!("{:?}", process.cmd()); | ||
|
||
let json = serde_json::to_string(&p).unwrap(); | ||
println!("{json}"); | ||
} | ||
} | ||
|
||
fn help() { | ||
println!("usage: process list"); | ||
} | ||
|
||
fn main() { | ||
let args: Vec<String> = env::args().collect(); | ||
if args.len() == 2 { | ||
// one argument passed | ||
match args[1].as_str() { | ||
"list" => { | ||
print_task_list(); | ||
exit(0); | ||
}, | ||
"set" => { // used for testing only | ||
println!("{{\"result\":\"Ok\"}}"); | ||
exit(0); | ||
}, | ||
_ => { | ||
help(); | ||
exit(1); | ||
}, | ||
} | ||
} | ||
else { | ||
help(); | ||
exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use serde::Serialize; | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize)] | ||
pub struct ProcessInfo { | ||
pub pid: u32, | ||
pub name: String, | ||
pub cmdline: String | ||
} | ||
|
||
impl ProcessInfo { | ||
pub fn new() -> Self { | ||
|
||
Self { | ||
pid: 0, | ||
name: String::new(), | ||
cmdline: String::new(), | ||
} | ||
} | ||
} |