Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ereslibre/ls-rm
Implement list, pull force and rm
- Loading branch information
Showing
8 changed files
with
240 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use anyhow::Result; | ||
use pathdiff::diff_paths; | ||
use std::{fs, path::Path}; | ||
use term_table::{row::Row, Table, TableStyle}; | ||
|
||
use crate::store::{ALL_MODULES_STORE_ROOT, STORE_ROOT}; | ||
|
||
pub(crate) fn ls() { | ||
let mut table = Table::new(); | ||
table.style = TableStyle::simple(); | ||
table.add_row(Row::new(vec!["Name", "Location"])); | ||
for module in fs::read_dir(ALL_MODULES_STORE_ROOT.as_path()) | ||
.expect("could not read store root") | ||
.flatten() | ||
{ | ||
if let Some(module_name) = module.file_name().to_str() { | ||
table.add_row(Row::new(vec![ | ||
module_name, | ||
&module_store_location(&module.path()).expect("invalid filename"), | ||
])); | ||
} | ||
} | ||
println!("{}", table.render()); | ||
} | ||
|
||
// Given a module location in the directory where symlinks to all | ||
// modules are located, give back the URI resembling where this module | ||
// was pulled from, or the path to the local filesystem where this | ||
// module is located if it wasn't pulled from a remote location | ||
fn module_store_location(module_path: &Path) -> Result<String> { | ||
let module_path = std::fs::read_link(module_path)?; | ||
// If this module was added from somehwere in the filesystem | ||
// (outside of the store), just return it as it is | ||
if !module_path.starts_with(STORE_ROOT.as_path()) { | ||
return Ok(format!( | ||
"{} (not in the store)", | ||
module_path.to_str().expect("invalid path") | ||
)); | ||
} | ||
let path = diff_paths(module_path, STORE_ROOT.as_path()).expect("failed to diff paths"); | ||
let mut component_iterator = path.components(); | ||
let scheme = component_iterator.next().expect("invalid path"); | ||
Ok(component_iterator.fold( | ||
format!("{}:/", scheme.as_os_str().to_str().expect("invalid path")), | ||
|acc, element| { | ||
format!( | ||
"{}/{}", | ||
acc, | ||
element.as_os_str().to_str().expect("invalid path") | ||
) | ||
}, | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use crate::store::STORE_ROOT; | ||
|
||
use std::path::PathBuf; | ||
|
||
// This removes the module from the store, and then removes both | ||
// links, the `all` toplevel link of the module itself, and the | ||
// kubectl-plugin link to `krew-wasm`. When the module is removed from | ||
// the store, it also cleans up the structure up to the root of the | ||
// store, so no empty folders are kept around in the store | ||
pub(crate) fn rm(module: &str) { | ||
let (module_paths, module_store_path) = | ||
crate::store::all_module_paths(module).expect("failed to get module paths for module"); | ||
|
||
// Unlink files that can be directly removed without any extra | ||
// cleanup: the toplevel "all" module and the symlink for the | ||
// kubectl-plugin | ||
for path in module_paths { | ||
#[allow(unused_must_use)] | ||
{ | ||
std::fs::remove_file(path); | ||
} | ||
} | ||
|
||
if !module_store_path.starts_with(STORE_ROOT.as_path()) { | ||
// Nothing to clean in the store itself, given this module | ||
// comes from another part of the filesystem. Just return. | ||
return; | ||
} | ||
|
||
#[allow(unused_must_use)] | ||
{ | ||
std::fs::remove_file(&module_store_path); | ||
} | ||
|
||
// Clean up parent directories in the store up to its root | ||
{ | ||
let mut prefix = STORE_ROOT.clone(); | ||
let module_leading_store_components = module_store_path | ||
.iter() | ||
.map(|component| { | ||
prefix = prefix.join(component); | ||
prefix.clone() | ||
}) | ||
.collect::<Vec<PathBuf>>(); | ||
|
||
module_leading_store_components | ||
.iter() | ||
.rev() | ||
.skip(1) // module file -- already unlinked | ||
.take(module_store_path.components().count() - STORE_ROOT.components().count() - 1 /* krew-wasm-store */) | ||
.for_each(|component| { | ||
#[allow(unused_must_use)] | ||
{ | ||
// try to clean up empty dirs. Ignore errors. | ||
std::fs::remove_dir(component); | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use anyhow::Result; | ||
use directories::{ProjectDirs, UserDirs}; | ||
use lazy_static::lazy_static; | ||
use std::path::PathBuf; | ||
|
||
lazy_static! { | ||
pub(crate) static ref BIN_ROOT: PathBuf = UserDirs::new() | ||
.expect("cannot find home directory for user") | ||
.home_dir() | ||
.join(".krew-wasm") | ||
.join("bin"); | ||
pub(crate) static ref STORE_ROOT: PathBuf = ProjectDirs::from("io.krew-wasm", "", "krew-wasm") | ||
.expect("cannot find project dirs") | ||
.cache_dir() | ||
.join("krew-wasm-store"); | ||
pub(crate) static ref ALL_MODULES_STORE_ROOT: PathBuf = STORE_ROOT.join("all"); | ||
} | ||
|
||
// Given a module name, return a tuple with elements that can be | ||
// unlinked directly in the first component of the tuple, and a second | ||
// argument with the full path to the location in the store. In order | ||
// to leave nothing behind in the store, we need to clean up every | ||
// directory until the root of the store after unlinking the module | ||
// from the store. | ||
pub(crate) fn all_module_paths(module_name: &str) -> Result<(Vec<PathBuf>, PathBuf)> { | ||
let module_bin = BIN_ROOT.join(format!("kubectl-{}", module_name)); | ||
let module_root = ALL_MODULES_STORE_ROOT.join(module_name); | ||
let module_path = std::fs::read_link(&module_root)?; | ||
Ok((vec![module_bin, module_root], module_path)) | ||
} | ||
|
||
pub(crate) fn ensure() { | ||
// Try to create the kubectl plugin bin path. | ||
std::fs::create_dir_all(BIN_ROOT.as_path()).unwrap_or_else(|err| { | ||
panic!( | ||
"could not create alias binary root at {}: {}", | ||
BIN_ROOT.display(), | ||
err | ||
) | ||
}); | ||
// Try to create the "all modules" root on the store. Used | ||
// to look for modules given a name. | ||
std::fs::create_dir_all(ALL_MODULES_STORE_ROOT.as_path()) | ||
.expect("could not create top level store path for all modules"); | ||
} |