This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement "init" command Implements an `init` command that writes a default shell.nix and .envrc to $CWD if those files don't exist already. * Use `ok_msg` * Remove overly redundant message * Print custom message per envrc/shell.nix
- Loading branch information
1 parent
ee66dea
commit 1586fac
Showing
4 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
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,44 @@ | ||
//! Bootstrap a new lorri project | ||
|
||
use crate::ops::{ok, ok_msg, ExitError, OpResult}; | ||
use std::fs::File; | ||
use std::io; | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
fn create_if_missing(path: &Path, contents: &str, msg: &str) -> Result<(), io::Error> { | ||
if path.exists() { | ||
println!("- {} {}", msg, path.display()); | ||
Ok(()) | ||
} else { | ||
let mut f = File::create(path)?; | ||
f.write_all(contents.as_bytes())?; | ||
println!("- Writing {}", path.display()); | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn to_op(e: Result<(), io::Error>) -> OpResult { | ||
match e { | ||
Ok(_) => ok(), | ||
Err(e) => ExitError::errmsg(format!("{}", e)), | ||
} | ||
} | ||
|
||
/// See the documentation for lorri::cli::Command::Init for | ||
/// more details | ||
pub fn main(default_shell: &str, default_envrc: &str) -> OpResult { | ||
to_op(create_if_missing( | ||
Path::new("./shell.nix"), | ||
default_shell, | ||
"shell.nix exists, skipping. Make sure it is of a form that works with nix-shell.", | ||
))?; | ||
|
||
to_op(create_if_missing( | ||
Path::new("./.envrc"), | ||
default_envrc, | ||
".envrc exists, skipping. Please add 'eval \"$(lorri direnv)\" to it to set up lorri support.", | ||
))?; | ||
|
||
ok_msg(String::from("\nSetup done.")) | ||
} |
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