Skip to content

Commit

Permalink
refactor(headless-client): deprecate subcommands for now (#4953)
Browse files Browse the repository at this point in the history
Closes #4907

They're still accepted, but the binary entirely determines the behavior.
This makes the code for CLI parsing and token handling simpler with
fewer branches, so it's easier to be sure it's correct.

Replaces #4942 which isn't doing what I intended anymore.
  • Loading branch information
ReactorScram committed May 13, 2024
1 parent ca0de26 commit b444dee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ services:
- type=registry,ref=us-east1-docker.pkg.dev/firezone-staging/cache/client:main
args:
PACKAGE: firezone-headless-client
# Add "standalone" to the command here once PR $4604 merges
image: ${CLIENT_IMAGE:-us-east1-docker.pkg.dev/firezone-staging/firezone/dev/client}:${CLIENT_TAG:-main}
cap_add:
- NET_ADMIN
Expand Down
47 changes: 12 additions & 35 deletions rust/headless-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ const TOKEN_ENV_KEY: &str = "FIREZONE_TOKEN";
#[derive(clap::Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
// Needed to preserve CLI arg compatibility
// TODO: Remove
#[command(subcommand)]
command: Option<Cmd>,
_command: Option<Cmd>,

#[arg(
short = 'u',
Expand Down Expand Up @@ -104,22 +106,10 @@ struct Cli {
max_partition_time: Option<humantime::Duration>,
}

impl Cli {
fn command(&self) -> Cmd {
// Needed for backwards compatibility with old Docker images
self.command.unwrap_or(Cmd::Auto)
}
}

#[derive(clap::Subcommand, Clone, Copy)]
enum Cmd {
/// If there is a token on disk, run in standalone mode. Otherwise, run as an IPC service. This will be removed in a future version.
#[command(hide = true)]
Auto,
/// Listen for IPC connections and act as a privileged tunnel process for a GUI client
#[command(hide = true)]
IpcService,
/// Act as a CLI-only Client
Standalone,
}

Expand All @@ -139,7 +129,7 @@ pub enum IpcServerMsg {
TunnelReady,
}

pub fn run() -> Result<()> {
pub fn run_only_headless_client() -> Result<()> {
let mut cli = Cli::parse();

// Modifying the environment of a running process is unsafe. If any other
Expand Down Expand Up @@ -168,27 +158,14 @@ pub fn run() -> Result<()> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let (_shutdown_tx, shutdown_rx) = mpsc::channel(1);

match cli.command() {
Cmd::Auto => {
if let Some(token) = get_token(token_env_var, &cli)? {
run_standalone(cli, rt, &token)
} else {
imp::run_ipc_service(cli, rt, shutdown_rx)
}
}
Cmd::IpcService => imp::run_ipc_service(cli, rt, shutdown_rx),
Cmd::Standalone => {
let token = get_token(token_env_var, &cli)?.with_context(|| {
format!(
"Can't find the Firezone token in ${TOKEN_ENV_KEY} or in `{}`",
cli.token_path
)
})?;
run_standalone(cli, rt, &token)
}
}

let token = get_token(token_env_var, &cli)?.with_context(|| {
format!(
"Can't find the Firezone token in ${TOKEN_ENV_KEY} or in `{}`",
cli.token_path
)
})?;
run_standalone(cli, rt, &token)
}

// Allow dead code because Windows doesn't have an obvious SIGHUP equivalent
Expand Down
2 changes: 1 addition & 1 deletion rust/headless-client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() -> anyhow::Result<()> {
firezone_headless_client::run()
firezone_headless_client::run_only_headless_client()
}

0 comments on commit b444dee

Please sign in to comment.