Skip to content

Commit

Permalink
Merge pull request #1700 from zowe/awharn-attempt-daemonizing
Browse files Browse the repository at this point in the history
Make changes to fix several daemon related issues
  • Loading branch information
awharn authored May 1, 2023
2 parents a07f88f + edf355a commit c445fa7
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 25 deletions.
7 changes: 7 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to the Zowe CLI package will be documented in this file.

## Recent Changes:

- Enhancement: Re-enabled color in the daemon client [#1379](https://github.com/zowe/zowe-cli/issues/1379)
- BugFix: Enabled ANSI in Windows based terminals [#1701](https://github.com/zowe/zowe-cli/issues/1701)
- BugFix: Changed daemon to spawn as its own process [#1241](https://github.com/zowe/zowe-cli/issues/1241) [#1277](https://github.com/zowe/zowe-cli/issues/1277) [#1309](https://github.com/zowe/zowe-cli/issues/1309)
- BugFix: Updated Imperative to allow for special handling of chalk and coloring in daemon client

## `7.13.0`

- Enhancement: Updated Imperative to add `--prune` option to `zowe config secure` command. [Imperative #547](https://github.com/zowe/imperative/issues/547)
Expand Down
26 changes: 25 additions & 1 deletion zowex/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion zowex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zowe"
version = "1.0.4"
version = "1.0.5"
authors = ["Zowe Project"]
edition = "2018"
license = "EPL-2.0"
Expand All @@ -17,8 +17,10 @@ rpassword = "5.0.1"
serde = { version = "1.0.156", features = ["derive"]}
serde_json = "1.0.94"
simple-error = "0.2.3"
supports-color = "2.0.0"
sysinfo = "0.28.2"
whoami = "1.4.0"
yansi = "0.5.1"

[target.'cfg(windows)'.dependencies]
fslock = "0.2.1"
Expand Down
30 changes: 16 additions & 14 deletions zowex/src/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

// Functions related to the manipulation of processes.

use std::env;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use std::process::{Command, Stdio};

#[cfg(target_family = "windows")]
use std::os::windows::process::CommandExt;

extern crate sysinfo;
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};

Expand Down Expand Up @@ -269,23 +271,23 @@ fn read_pid_for_user() -> Option<sysinfo::Pid> {
* On Linux:
* If you use Stdio::inherit(), you get double output. If use use Stdio::null(), you get no color.
*/

pub fn proc_start_daemon(njs_zowe_path: &str) -> String {
println!("Starting a background process to increase performance ...");

if env::consts::OS == "windows" {
/* Windows CMD and Powershell terminal windows show escape characters
* instead of colors in daemon-mode. A more elegant solution may exist,
* but for now we just turn off color in daemon mode on Windows.
*/
env::set_var("FORCE_COLOR", "0");
}

let daemon_arg = LAUNCH_DAEMON_OPTION;
match Command::new(njs_zowe_path)
.arg(daemon_arg)
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
let mut cmd = Command::new(njs_zowe_path);

// Uses creation flags from https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags
// Flags are CREATE_NO_WINDOW, CREATE_NEW_PROCESS_GROUP, and CREATE_UNICODE_ENVIRONMENT
#[cfg(target_family = "windows")]
cmd.creation_flags(0x08000600);

cmd.arg(daemon_arg)
.stdout(Stdio::null())
.stderr(Stdio::null());

match cmd.spawn()
{
Ok(_unused) => { /* nothing to do */ }
Err(error) => {
Expand Down
36 changes: 29 additions & 7 deletions zowex/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ use std::time::Duration;

#[cfg(target_family = "unix")]
use home::home_dir;
#[cfg(target_family = "windows")]
use whoami::username;

// Zowe daemon executable modules
use crate::defs::*;
Expand Down Expand Up @@ -114,14 +112,38 @@ fn unit_test_util_get_socket_string() {

#[test]
fn unit_test_get_zowe_env() {
let env = util_get_zowe_env();
assert_eq!(env.get("ZOWE_EDITOR"), None);
let environment = util_get_zowe_env();
assert_eq!(environment.get("ZOWE_EDITOR"), None);

env::set_var("ZOWE_EDITOR", "nano");
let env = util_get_zowe_env();
assert_eq!(env.get("ZOWE_EDITOR"), Some(&"nano".to_owned()));

let environment = util_get_zowe_env();
assert_eq!(environment.get("ZOWE_EDITOR"), Some(&"nano".to_owned()));
env::remove_var("ZOWE_EDITOR");

env::remove_var("FORCE_COLOR");
let environment = util_get_zowe_env();
let color = util_terminal_supports_color();
assert_eq!(environment.get("FORCE_COLOR"), Some(&color.to_string()));

env::set_var("FORCE_COLOR", "0");
let environment = util_get_zowe_env();
assert_eq!(environment.get("FORCE_COLOR"), Some(&"0".to_owned()));
env::remove_var("FORCE_COLOR");

env::set_var("FORCE_COLOR", "1");
let environment = util_get_zowe_env();
assert_eq!(environment.get("FORCE_COLOR"), Some(&"1".to_owned()));
env::remove_var("FORCE_COLOR");

env::set_var("FORCE_COLOR", "2");
let environment = util_get_zowe_env();
assert_eq!(environment.get("FORCE_COLOR"), Some(&"2".to_owned()));
env::remove_var("FORCE_COLOR");

env::set_var("FORCE_COLOR", "3");
let env = util_get_zowe_env();
assert_eq!(env.get("FORCE_COLOR"), Some(&"3".to_owned()));
env::remove_var("FORCE_COLOR");
}

#[test]
Expand Down
36 changes: 34 additions & 2 deletions zowex/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ use home::home_dir;
extern crate pathsearch;
use pathsearch::PathSearcher;

extern crate supports_color;
use supports_color::Stream;

extern crate yansi;
use yansi::Paint;

extern crate whoami;
use whoami::username;

Expand Down Expand Up @@ -134,9 +140,22 @@ pub fn util_get_socket_string() -> Result<String, i32> {
}

pub fn util_get_zowe_env() -> HashMap<String, String> {
env::vars()
let mut environment: HashMap<String, String> = env::vars()
.filter(|(k, _)| k.starts_with("ZOWE_"))
.collect()
.collect();

match env::var("FORCE_COLOR") {
Ok(val) => {environment.insert(String::from("FORCE_COLOR"), val);},
Err(_val) => {environment.insert(String::from("FORCE_COLOR"), util_terminal_supports_color().to_string());}
}

// Make sure ansi is enabled for the response
if !Paint::enable_windows_ascii() {
#[cfg(not(test))] // Because this is a problem during GitHub Actions CI builds
environment.insert(String::from("FORCE_COLOR"), String::from("0"));
}

environment
}

#[cfg(target_family = "windows")]
Expand All @@ -148,3 +167,16 @@ pub fn util_get_username() -> String {
pub fn util_get_username() -> String {
username()
}

pub fn util_terminal_supports_color() -> i32 {
if let Some(support) = supports_color::on(Stream::Stdout) {
if support.has_16m {
return 3;
} else if support.has_256 {
return 2;
} else if support.has_basic {
return 1;
}
}
return 0;
}

0 comments on commit c445fa7

Please sign in to comment.