Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Implement "init" command (#83)
Browse files Browse the repository at this point in the history
* 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
gilligan authored and Profpatsch committed May 17, 2019
1 parent ee66dea commit 1586fac
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/cli.rs
Expand Up @@ -45,6 +45,10 @@ pub enum Command {
/// Upgrade Lorri
#[structopt(name = "self-upgrade", alias = "self-update")]
Upgrade(UpgradeTo),

/// Bootstrap files for a new setup
#[structopt(name = "init")]
Init,
}

/// A stub struct to represent how what we want to upgrade to.
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Expand Up @@ -4,12 +4,13 @@ extern crate structopt;
extern crate log;

use lorri::cli::{Arguments, Command};
use lorri::ops::{build, direnv, info, shell, upgrade, watch, ExitError, OpResult};
use lorri::ops::{build, direnv, info, init, shell, upgrade, watch, ExitError, OpResult};
use lorri::project::{Project, ProjectLoadError};
use std::env;
use structopt::StructOpt;

const TRIVIAL_SHELL_SRC: &str = include_str!("./trivial-shell.nix");
const DEFAULT_ENVRC: &str = "eval \"$(lorri direnv)\"";

fn main() {
let opts = Arguments::from_args();
Expand All @@ -32,6 +33,8 @@ fn main() {

(Command::Upgrade(args), _) => upgrade::main(args),

(Command::Init, _) => init::main(TRIVIAL_SHELL_SRC, DEFAULT_ENVRC),

(_, Err(ProjectLoadError::ConfigNotFound)) => {
let current_dir_msg = match env::current_dir() {
Err(_) => String::from(""),
Expand Down
44 changes: 44 additions & 0 deletions src/ops/init/mod.rs
@@ -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."))
}
1 change: 1 addition & 0 deletions src/ops/mod.rs
Expand Up @@ -3,6 +3,7 @@
pub mod build;
pub mod direnv;
pub mod info;
pub mod init;
pub mod shell;
pub mod upgrade;
pub mod watch;
Expand Down

0 comments on commit 1586fac

Please sign in to comment.