Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Added ability to specify username and password for https, and did a v…
Browse files Browse the repository at this point in the history
…ersion bump.
  • Loading branch information
jmwright committed Mar 11, 2019
1 parent 2950727 commit a3fb459
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 9 deletions.
96 changes: 91 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "sliderule-cli"
version = "0.1.0"
version = "0.3.0"
authors = ["Jeremy Wright <wrightjmf@gmail.com>"]

[dependencies]
# sliderule = { path = "../sliderule-rs" }
sliderule = { git = "https://github.com/7bindustries/sliderule-rs" }

argparse = "0.2.2"
os_info = "1.0.3"
rpassword = "2.1.0"

[dev-dependencies]
git2 = "0.8"
Expand Down
85 changes: 83 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
extern crate argparse;
extern crate rpassword;
extern crate sliderule;

use argparse::{ArgumentParser, List, Store, StoreTrue};
use sliderule::SROutput;
use std::env;
use std::io;
use std::io::prelude::*;
use std::path::{Path, PathBuf};

fn main() {
Expand All @@ -20,7 +22,7 @@ fn main() {

// Some items for the command line help interface
let app_description = "Tool to manage Sliderule projects.";
let cmd_description = "Sliderule command to run: [create | download | upload | add | remove | refactor | licenses ]";
let cmd_description = "Sliderule command to run: [create | download | upload | add | remove | refactor | licenses | login]";
let args_description = "Arguments to Sliderule commands:
create [name],
download [all | dependencies | component_url],
Expand Down Expand Up @@ -200,6 +202,8 @@ fn main() {
message = message.trim().to_string();
}

let mut userinfo = (String::new(), String::new());

// Make sure this project has already been initialized as a repository
if !Path::new(".git").exists() && url.is_empty() {
println!("This project has not been initialized with a repository yet. Enter a URL of an existing repository to upload this component to:");
Expand All @@ -209,9 +213,21 @@ fn main() {
.expect("ERROR: Failed to read name or URL from user.");

url = url.trim().to_string();

// Check to see if there needs to be a username and password set for this
if url.contains("https") {
userinfo = get_https_user_info();
}
}

let mut user = None;
let mut pass = None;
if !userinfo.0.is_empty() && !userinfo.1.is_empty() {
user = Some(userinfo.0.trim().to_string());
pass = Some(userinfo.1.trim().to_string());
}

let output = sliderule::upload_component(&get_cwd(), message, &url);
let output = sliderule::upload_component(&get_cwd(), message, url, user, pass);

// Show extra output only when the user requests it
if verbose {
Expand Down Expand Up @@ -327,6 +343,49 @@ fn main() {
eprintln!("licenses subcommand not understood: {}", subcommand);
std::process::exit(1);
}
} else if command == "login" {
// Make sure this project has already been initialized as a repository
if url.is_empty() {
println!(
"Enter a URL of an existing repository to that this component will be uploaded to:"
);

io::stdin()
.read_line(&mut url)
.expect("ERROR: Failed to read name or URL from user.");

url = url.trim().to_string();
}

let mut userinfo = (String::new(), String::new());

// Check to see if there needs to be a username and password set for this
if url.contains("https") {
userinfo = get_https_user_info();
}

let mut user = None;
let mut pass = None;
if !userinfo.0.is_empty() && !userinfo.1.is_empty() {
user = Some(userinfo.0.trim().to_string());
pass = Some(userinfo.1.trim().to_string());
}

// If a URL is not present, it will mess up the git config
if url.is_empty() {
println!("URL cannot be empty when logging into a repository.");
std::process::exit(2);
}

// Change/add the login information for the user
let output = sliderule::remote_login(&get_cwd(), Some(url), user, pass);

// Show extra output only when the user requests it
if verbose {
print_stdout(&output);
} else {
println!("Finished setting username and password for remote repository.");
}
}

// The user has to supply a command, and it needs to be recognized
Expand All @@ -336,6 +395,28 @@ fn main() {
}
}

/*
* Prompts the user for an https username and a password, with a warning that doing so is a security concern.
*/
fn get_https_user_info() -> (String, String) {
let mut username = String::new();
let password: String;

println!(
"You need to enter a username and password for using an https URL. Please do that now."
);
println!("WARNING: Using https with sliderule-cli can lead to passwords being stored in plain text. It is better to use ssh.");
print!("User: ");
io::stdout().flush().ok().expect("Could not flush stdout");
io::stdin()
.read_line(&mut username)
.expect("ERROR: Failed to read username from user");

password = rpassword::prompt_password_stdout("Password: ").unwrap();

(username, password)
}

/*
* Prints all the lines of standard output to standard output.
*/
Expand Down

0 comments on commit a3fb459

Please sign in to comment.