colab-cli 0.1.4

Google Colab from the terminal — manage CPU/GPU/TPU runtimes, open interactive shells, and stream files, straight from your shell.
Documentation
use std::env;
use std::fs;
use std::path::PathBuf;

const CLIENT_ID_VAR: &str = "COLAB_EXTENSION_CLIENT_ID";
const CLIENT_SECRET_VAR: &str = "COLAB_EXTENSION_CLIENT_NOT_SO_SECRET";

fn main() {
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-env-changed={CLIENT_ID_VAR}");
    println!("cargo:rerun-if-env-changed={CLIENT_SECRET_VAR}");

    let client_id = env::var(CLIENT_ID_VAR).unwrap_or_default();
    let client_secret = env::var(CLIENT_SECRET_VAR).unwrap_or_default();

    if client_id.is_empty() ^ client_secret.is_empty() {
        println!(
            "cargo:warning=colab-cli: only one of {CLIENT_ID_VAR} / {CLIENT_SECRET_VAR} is set at build time; both or neither is expected"
        );
    }

    let manifest_dir = PathBuf::from(
        env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set by cargo"),
    );
    let dest = manifest_dir.join("src").join("embedded.rs");

    let have_env = !client_id.is_empty() && !client_secret.is_empty();

    if !have_env && dest.exists() {
        return;
    }

    let client_id_lit = format!("{client_id:?}");
    let client_secret_lit = format!("{client_secret:?}");

    let generated = format!(
        r##"// @generated by build.rs — do not edit, do not commit.
#[inline]
pub fn embedded_client_id() -> String {{
    obfstr::obfstr!({client_id_lit}).to_string()
}}

#[inline]
pub fn embedded_client_secret() -> String {{
    obfstr::obfstr!({client_secret_lit}).to_string()
}}
"##
    );

    fs::write(&dest, generated).expect("failed to write src/embedded.rs");
}