Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement: settings clone CLI command #367

Merged
merged 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use git_version::git_version;
use printnanny_nats::message_v2::{NatsReply, NatsRequest};
use printnanny_nats::cloud_worker::DEFAULT_NATS_CLOUD_APP_NAME;
use printnanny_nats::subscriber::{ NatsSubscriber, DEFAULT_NATS_EDGE_APP_NAME};
use printnanny_settings::SettingsFormat;
use printnanny_settings::{SettingsFormat};
use printnanny_services::janus::{ JanusAdminEndpoint, janus_admin_api_call };
use printnanny_cli::settings::{SettingsCommand};
use printnanny_cli::cloud_data::CloudDataCommand;
Expand Down Expand Up @@ -110,6 +110,18 @@ async fn main() -> Result<()> {
.version(GIT_VERSION)
.arg_required_else_help(true)
.about("Interact with PrintNanny device and user settings")
.subcommand(Command::new("clone")
.author(crate_authors!())
.about(crate_description!())
.version(GIT_VERSION)
.about("Git clone PrintNanny Settings repo (default settings files for PrintNanny, OctoPrint, Moonraker, Klipper)")
.arg(Arg::new("dir")
.short('d')
.long("dir")
.takes_value(true)
.help("Directory to clone repo to")
)
)
.subcommand(Command::new("get")
.author(crate_authors!())
.about(crate_description!())
Expand Down
6 changes: 6 additions & 0 deletions cli/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::{self, Write};
use std::path::PathBuf;

use printnanny_services::error::ServiceError;
use printnanny_settings::printnanny::PrintNannySettings;
Expand All @@ -10,6 +11,11 @@ impl SettingsCommand {
pub async fn handle(sub_m: &clap::ArgMatches) -> Result<(), ServiceError> {
let config: PrintNannySettings = PrintNannySettings::new()?;
match sub_m.subcommand() {
Some(("clone", args)) => {
let dir = args.value_of("dir").map(PathBuf::from);
let settings = PrintNannySettings::new()?;
settings.init_local_git_repo(dir).await?;
}
Some(("get", args)) => {
let key = args.value_of("key");
let f: SettingsFormat = args.value_of_t("format").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion nats/src/message_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ mod tests {
let settings = PrintNannySettings::new().unwrap();
Runtime::new()
.unwrap()
.block_on(settings.init_local_git_repo())
.block_on(settings.init_local_git_repo(None))
.unwrap();
}

Expand Down
8 changes: 6 additions & 2 deletions settings/src/printnanny.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ impl PrintNannySettings {

Ok(result)
}
pub async fn init_local_git_repo(&self) -> Result<(), PrintNannySettingsError> {
let repo = git2::Repository::clone(&self.git.remote, &self.paths.settings_dir)?;
pub async fn init_local_git_repo(
&self,
dir: Option<PathBuf>,
) -> Result<(), PrintNannySettingsError> {
let target_dir = dir.unwrap_or(self.paths.settings_dir.clone());
let repo = git2::Repository::clone(&self.git.remote, &target_dir)?;
let config = repo.config()?;
let mut localconfig = config.open_level(git2::ConfigLevel::Local)?;
localconfig.set_str("user.email", &self.git.email)?;
Expand Down