Skip to content

Commit

Permalink
Docs
Browse files Browse the repository at this point in the history
codchi gc
codchi mod set
shortcuts
+much more
  • Loading branch information
htngr committed May 23, 2024
1 parent d018f35 commit dca3161
Show file tree
Hide file tree
Showing 40 changed files with 2,614 additions and 946 deletions.
314 changes: 288 additions & 26 deletions codchi/Cargo.lock

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion codchi/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ directories = "5.0.1"
fs4 = { version = "0.7", features = ["sync"] }
rustls-native-certs = "0.7.0"
base64 = "0.21.7"
petname = { version = "2.0.2", default-features = false, features = ["default-rng", "default-words"] }

# config = "0.13.4"
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -47,19 +48,25 @@ lazy-regex = "3.1.0"
inquire = "0.6.2"
indicatif = "0.17.8"
indicatif-log-bridge = "0.2.2"
indoc = "2.0.5"
freedesktop_entry_parser = "1.3.0"

[target.'cfg(unix)'.dependencies]
nix = { version = "0.28.0", features = ["user"] }
# signal-hook = "0.3.17"

# [target.'cfg(target_os = "linux")'.dependencies]
# lxd = "0.1.9"

[target.'cfg(windows)'.dependencies]
# windows = { version = "0.52", features = [ "Win32_System_Threading", "Win32_System_Console", "Win32_Foundation" ]}
windows = { version = "0.56", features = [ "Win32_System_Console", "Win32_UI_WindowsAndMessaging", "Win32_System_Diagnostics_Debug", "Win32_Storage_FileSystem", "Win32_Security" ]}
# win32console = "0.1.5"
wslapi = "0.1.3"
version-compare = "0.1"
# codepage-strings = "1.0.2"
known-folders = "1.1.0"
mslnk = "0.1.8"
sysinfo = "0.30.12"
# path-slash = "0.2.1"
# ctrlc = "3.4.4"
# wtf8 = "0.1"
Expand All @@ -76,3 +83,4 @@ url = "2.5.0"
git-url-parse = "0.4.4"
lazy-regex = "3.1.0"
log = "0.4"
strum = { version = "0.25.0", features = ["derive"] }
176 changes: 132 additions & 44 deletions codchi/cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use self::module::*;
use self::name::{ModuleName, CODCHI_DRIVER_MODULE};

use clap::builder::*;
use clap::*;
Expand Down Expand Up @@ -41,25 +42,37 @@ pub struct Cli {
// #[arg(long, short = 's')]
// pub stop_ctrl: bool,

/// Hide / show console window on windows (for GUI apps)
#[cfg(target_os = "windows")]
#[arg(long, hide = true, default_value = "true")]
pub terminal: Option<bool>,

#[command(subcommand)]
pub command: Option<Cmd>,
}

#[derive(Debug, Subcommand, Clone)]
#[allow(clippy::large_enum_variant)] // can't use Box with flattened Option
pub enum Cmd {
// #[command(subcommand)]
// #[clap(aliases = &["ctrl"])]
// Controller(ControllerCmd),
///
/// List machines with their status
Status {},

/// Create a new code machine. <https://codchi.dev/docs/start/usage.html#creating-a-machine>
Init {
/// Initialize this code machine without any modules
#[arg(long, short)]
empty: bool,
/// Name of the code machine
machine_name: String,

/// http(s) url to the codchi module
url: Option<GitUrl>,

#[command(flatten)]
options: AddModuleOptions,
// #[group(requires = "url")]
options: Box<ModuleOptions>,
},

/// Execute (interactive) command inside a machine.
Expand All @@ -74,9 +87,13 @@ pub enum Cmd {
cmd: Vec<String>,
},

/// Apply changes to a code machine.
/// Apply changes to a code machine. By default this fetches updates for each module.
/// <https://codchi.dev/docs/start/usage.html#applying-changes>
Rebuild {
/// Don't fetch module updates
#[arg(long, short = 'n')]
no_update: bool,

/// Name of the code machine
name: String,
},
Expand All @@ -92,7 +109,7 @@ pub enum Cmd {
Delete {
/// Don't prompt for confirmation and delete immediately
#[arg(long)]
im_really_sure: bool,
i_am_really_sure: bool,

/// Name of the code machine
name: String,
Expand All @@ -103,6 +120,25 @@ pub enum Cmd {
#[command(subcommand)]
#[clap(aliases = &["mod"])]
Module(ModuleCmd),

/// Perform garbage collection of old nix store paths.
/// <https://codchi.dev/docs/start/usage.html#garbage-collection>
#[clap(
group(ArgGroup::new("exclusive").args(&["all", "machines"])),
)]
GC {
/// Delete old machine generations. AGE is the minimum age in days to be deleted [default: 0]
#[arg(long, short = 'd', value_name = "AGE")]
older_than: Option<Option<u16>>,

/// Process all machines (only works with `--older-than`)
#[arg(long, short = 'a', requires = "older_than")]
all: bool,

/// Machines to be processed (only works with `--older-than`)
#[arg(requires = "older_than")]
machines: Vec<String>,
},
}

// mod ctrl {
Expand Down Expand Up @@ -161,28 +197,55 @@ pub enum Cmd {
// }

mod module {
use self::name::ModuleName;

pub use super::*;

#[derive(Debug, Subcommand, Clone)]
pub enum ModuleCmd {
/// <https://codchi.dev/docs/start/usage.html#modules>
/// Lists modules of a machine <https://codchi.dev/docs/start/usage.html#modules>
#[clap(aliases = &["ls"])]
List {
/// Name of the code machine
name: String,
},

/// <https://codchi.dev/docs/start/usage.html#adding-modules>
Add(AddModuleOptions),
/// Adds a module to a machine <https://codchi.dev/docs/start/usage.html#adding-modules>
Add {
/// Name of the code machine
machine_name: String,

/// HTTP(S) URL or file path to the codchi module
url: GitUrl,

#[command(flatten)]
options: Box<ModuleOptions>,
},

/// Modifies a module of a machine <https://codchi.dev/docs/start/usage.html#modifying-a-module>
Set {
/// Name of the code machine
machine_name: String,

/// Name of the module to modify
module_name: ModuleName,

/// <https://codchi.dev/docs/start/usage.html#delete-a-module>
/// HTTP(S) URL or file path to the codchi module
url: Option<GitUrl>,

#[command(flatten)]
options: Box<ModuleOptions>,
},

/// Deletes a module of a machine <https://codchi.dev/docs/start/usage.html#delete-a-module>
/// Note: This will NOT delete user data.
#[clap(aliases = &["rm", "remove"])]
Delete {
/// Name of the code machine
name: String,

/// Id of the module (You can list them with `codchi module ls NAME`)
id: usize,
/// Name of the module (You can list them with `codchi module ls NAME`)
module_name: ModuleName,
},
// /// Fetch module updates
// Update {
Expand All @@ -191,14 +254,19 @@ mod module {
// },
}

#[derive(clap::Args, Debug, Clone)]
#[derive(clap::Args, Debug, Clone, Default, Eq, PartialEq)]
// branch an tag are mutually exclusive
#[clap(group(ArgGroup::new("ref").args(&["branch", "tag"])))]
pub struct AddModuleOptions {
#[clap(
group(ArgGroup::new("exclusive").args(&["branch", "tag"])),
)]
pub struct ModuleOptions {
/// Don't prompt for confirmation and accept all of codchi's defaults
#[arg(long, short = 'y')]
pub dont_prompt: bool,

#[arg(long, short = 'p')]
pub use_nixpkgs: Option<NixpkgsLocation>,

/// Authorisation token for private repositories.
#[arg(long, short)]
pub token: Option<String>,
Expand All @@ -215,41 +283,16 @@ mod module {
#[arg(long, short)]
pub commit: Option<String>,

/// Name of the code machine
pub name: String,

/// http(s) url to the codchi module
pub url: GitUrl,
/// Name of the module
#[arg(long, short = 'n')]
pub name: Option<ModuleName>,

/// Path of the NixOS module you whish to add.
/// Currently supported: 'codchiModules.<module>' or 'nixosModules.<module>'
pub module_path: Option<ModuleAttrPath>,
}

// this is only neccessary when we have named modules. Currently they are refered to by their
// index
// #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
// pub struct CodchiName(pub String);

// impl Display for CodchiName {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// f.write_str(&self.0)
// }
// }

// impl FromStr for CodchiName {
// type Err = String;

// fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
// if s != "__codchi" {
// Ok(CodchiName(s.to_string()))
// } else {
// Err("The name '__codchi' is a reserved keyword.".to_string())
// }
// }
// }

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ModuleAttrPath {
/// codchiModules or nixosModules
pub base: String,
Expand Down Expand Up @@ -282,3 +325,48 @@ mod module {
}
}
}

pub mod name {

use super::*;
use core::fmt;

pub static CODCHI_DRIVER_MODULE: &str = "codchi_driver";

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub struct ModuleName(pub String);

impl Display for ModuleName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}

impl FromStr for ModuleName {
type Err = String;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
if s != CODCHI_DRIVER_MODULE {
Ok(ModuleName(s.to_string()))
} else {
Err("The name 'codchi_driver' is reserved.".to_string())
}
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
pub enum NixpkgsLocation {
Local,
Remote,
}
#[allow(dead_code)]
impl NixpkgsLocation {
pub fn named(loc: &Option<NixpkgsLocation>, module_name: &ModuleName) -> Option<String> {
match loc {
Some(Self::Remote) => Some(module_name.to_string()),
Some(Self::Local) => Some(CODCHI_DRIVER_MODULE.to_owned()),
None => None,
}
}
}
Loading

0 comments on commit dca3161

Please sign in to comment.