cli/
constants.rs

1/*---------------------------------------------------------------------------------------------
2 *  Copyright (c) Microsoft Corporation. All rights reserved.
3 *  Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
5
6use serde::Deserialize;
7use std::{collections::HashMap, io::IsTerminal};
8
9use const_format::concatcp;
10use lazy_static::lazy_static;
11
12use crate::options::Quality;
13
14pub const CONTROL_PORT: u16 = 31545;
15
16/// Protocol version sent to clients. This can be used to indiciate new or
17/// changed capabilities that clients may wish to leverage.
18///  1 - Initial protocol version
19///  2 - Addition of `serve.compressed` property to control whether servermsg's
20///      are compressed bidirectionally.
21///  3 - The server's connection token is set to a SHA256 hash of the tunnel ID
22///  4 - The server's msgpack messages are no longer length-prefixed
23pub const PROTOCOL_VERSION: u32 = 4;
24
25/// Prefix for the tunnel tag that includes the version.
26pub const PROTOCOL_VERSION_TAG_PREFIX: &str = "protocolv";
27/// Tag for the current protocol version, which is included in dev tunnels.
28pub const PROTOCOL_VERSION_TAG: &str = concatcp!("protocolv", PROTOCOL_VERSION);
29
30pub const VSCODE_CLI_VERSION: Option<&'static str> = option_env!("VSCODE_CLI_VERSION");
31pub const VSCODE_CLI_AI_KEY: Option<&'static str> = option_env!("VSCODE_CLI_AI_KEY");
32pub const VSCODE_CLI_AI_ENDPOINT: Option<&'static str> = option_env!("VSCODE_CLI_AI_ENDPOINT");
33pub const VSCODE_CLI_QUALITY: Option<&'static str> = option_env!("VSCODE_CLI_QUALITY");
34pub const DOCUMENTATION_URL: Option<&'static str> = option_env!("VSCODE_CLI_DOCUMENTATION_URL");
35pub const VSCODE_CLI_COMMIT: Option<&'static str> = option_env!("VSCODE_CLI_COMMIT");
36pub const VSCODE_CLI_UPDATE_ENDPOINT: Option<&'static str> = option_env!("VSCODE_CLI_UPDATE_URL");
37
38/// Windows lock name for the running tunnel service. Used by the setup script
39/// to detect a tunnel process. See #179265.
40pub const TUNNEL_SERVICE_LOCK_NAME: Option<&'static str> =
41	option_env!("VSCODE_CLI_WIN32_TUNNEL_SERVICE_MUTEX");
42
43/// Windows lock name for the running tunnel without a service. Used by the setup
44/// script to detect a tunnel process. See #179265.
45pub const TUNNEL_CLI_LOCK_NAME: Option<&'static str> = option_env!("VSCODE_CLI_WIN32_TUNNEL_MUTEX");
46
47pub const TUNNEL_SERVICE_USER_AGENT_ENV_VAR: &str = "TUNNEL_SERVICE_USER_AGENT";
48
49/// Application name as it appears on the CLI.
50pub const APPLICATION_NAME: &str = match option_env!("VSCODE_CLI_APPLICATION_NAME") {
51	Some(n) => n,
52	None => "code",
53};
54
55/// Full name of the product with its version.
56pub const PRODUCT_NAME_LONG: &str = match option_env!("VSCODE_CLI_NAME_LONG") {
57	Some(n) => n,
58	None => "Code - OSS",
59};
60
61/// Name of the application without quality information.
62pub const QUALITYLESS_PRODUCT_NAME: &str = match option_env!("VSCODE_CLI_QUALITYLESS_PRODUCT_NAME")
63{
64	Some(n) => n,
65	None => "Code",
66};
67
68/// Name of the application without quality information.
69pub const QUALITYLESS_SERVER_NAME: &str = concatcp!(QUALITYLESS_PRODUCT_NAME, " Server");
70
71pub const QUALITY: &str = match VSCODE_CLI_QUALITY {
72	Some(q) => q,
73	_ => "oss",
74};
75
76/// Web URL the editor is hosted at. For VS Code, this is vscode.dev.
77pub const EDITOR_WEB_URL: Option<&'static str> = option_env!("VSCODE_CLI_TUNNEL_EDITOR_WEB_URL");
78
79/// Name shown in places where we need to tell a user what a process is, e.g. in sleep inhibition.
80pub const TUNNEL_ACTIVITY_NAME: &str = concatcp!(PRODUCT_NAME_LONG, " Tunnel");
81
82/// Download URL of the desktop product.
83pub const PRODUCT_DOWNLOAD_URL: Option<&'static str> = option_env!("VSCODE_CLI_DOWNLOAD_URL");
84
85const NONINTERACTIVE_VAR: &str = "VSCODE_CLI_NONINTERACTIVE";
86
87/// Default data CLI data directory.
88pub const DEFAULT_DATA_PARENT_DIR: &str = match option_env!("VSCODE_CLI_DATA_FOLDER_NAME") {
89	Some(n) => n,
90	None => ".vscode-oss",
91};
92
93pub fn get_default_user_agent() -> String {
94	format!(
95		"vscode-server-launcher/{}",
96		VSCODE_CLI_VERSION.unwrap_or("dev")
97	)
98}
99
100const NO_COLOR_ENV: &str = "NO_COLOR";
101
102#[derive(Deserialize, Debug)]
103#[serde(rename_all = "camelCase")]
104pub struct ServerQualityInfo {
105	pub server_application_name: String,
106}
107
108lazy_static! {
109	pub static ref TUNNEL_SERVICE_USER_AGENT: String =
110		match std::env::var(TUNNEL_SERVICE_USER_AGENT_ENV_VAR) {
111			Ok(ua) if !ua.is_empty() => format!("{} {}", ua, get_default_user_agent()),
112			_ => get_default_user_agent(),
113		};
114
115	/// Map of qualities to the server name
116	pub static ref SERVER_NAME_MAP: Option<HashMap<Quality, ServerQualityInfo>> =
117		option_env!("VSCODE_CLI_TUNNEL_SERVER_QUALITIES").and_then(|s| serde_json::from_str(s).unwrap());
118
119	/// Whether i/o interactions are allowed in the current CLI.
120	pub static ref IS_A_TTY: bool = std::io::stdin().is_terminal();
121
122	/// Whether i/o interactions are allowed in the current CLI.
123	pub static ref COLORS_ENABLED: bool = *IS_A_TTY && std::env::var(NO_COLOR_ENV).is_err();
124
125	/// Whether i/o interactions are allowed in the current CLI.
126	pub static ref IS_INTERACTIVE_CLI: bool = *IS_A_TTY && std::env::var(NONINTERACTIVE_VAR).is_err();
127
128	/// Map of quality names to arrays of app IDs used for them, for example, `{"stable":["ABC123"]}`
129	pub static ref WIN32_APP_IDS: Option<Vec<String>> =
130		option_env!("VSCODE_CLI_WIN32_APP_IDS").map(|s| s.split(',').map(|s| s.to_string()).collect());
131}