Skip to content

Commit

Permalink
Merge pull request #32 from ericmcbride/tokio-integration
Browse files Browse the repository at this point in the history
Updated to use struct ops
  • Loading branch information
ericmcbride authored Jan 14, 2020
2 parents 3900ec1 + 537fe8a commit 628ab11
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 77 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ colored = "1.7"
rand = "0.6.5"
tokio-threadpool = "0.1.17"
futures = "0.1.19"
structopt = "0.3"
36 changes: 17 additions & 19 deletions src/kube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
use std::str;

use structopt::StructOpt;
use tokio_threadpool::{blocking, ThreadPool};

use futures::future::{lazy, poll_fn};
Expand All @@ -36,14 +37,23 @@ static COLOR_VEC: &'static [&str] = &[
"bright cyan",
];

/// Config built from cli-args
#[derive(Debug)]
#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
pub struct LogRecorderConfig {
#[structopt(short, long, default_value = "kube-system")]
namespace: String,

#[structopt(short, long = "kubeconfig", default_value = "")]
kube_config: String,

#[structopt(short, long, default_value = "/tmp/wufei/")]
outfile: String,

#[structopt(short, long)]
file: bool,

#[structopt(long)]
color: bool,
outfile: String,
}

/// Pod infromation
Expand All @@ -54,22 +64,10 @@ pub struct PodInfo {
parent: String,
}

impl LogRecorderConfig {
pub fn new(
namespace: String,
kube_config: String,
file: bool,
color: bool,
outfile: String,
) -> LogRecorderConfig {
LogRecorderConfig {
namespace: namespace,
kube_config: kube_config,
file: file,
color: color,
outfile: outfile,
}
}
/// Cli options for wufei
pub fn run() -> Result<(LogRecorderConfig), Box<dyn::std::error::Error>> {
let opt = LogRecorderConfig::from_args();
Ok(opt)
}

/// Entrypoint for the tailing of the logs
Expand Down
26 changes: 2 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#[macro_use]
extern crate clap;
extern crate structopt;

mod kube;
mod utils;

/// Main Entrypoint into the code
fn main() {
match run() {
match kube::run() {
Ok(log_config) => match kube::run_logs(&log_config) {
Ok(_) => {
println!("Log files are found at /tmp/<podname>");
Expand All @@ -20,24 +19,3 @@ fn main() {
}
}
}

/// Cli options for wufei
fn run() -> Result<(kube::LogRecorderConfig), Box<dyn ::std::error::Error>> {
let args = clap_app!(wufei =>
(version: "1.0")
(author: "Eric McBride <[email protected]>")
(about: "View All Logs from Kubernetes Namespace")
(@arg NAMESPACE: -n --namespace +required +takes_value "Namespace for logs")
(@arg KUBECONFIG: -k --kubeconfig +takes_value "Kube config file if not using context")
(@arg OUTFILE: -o --outfile +takes_value requires[FILE] "Outfile for --file flag")
(@arg FILE: -f --file "Write logs to files based on pod name /tmp/podname")
(@arg COLOR: --color "Show colored output")
)
.get_matches();

let log_recorder = utils::set_args(&args);
match log_recorder {
Ok(log_recorder) => Ok(log_recorder),
Err(log_recorder) => Err(log_recorder),
}
}
35 changes: 1 addition & 34 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,7 @@
use crate::kube;
use clap::ArgMatches;
use std::io::{Error, ErrorKind};

/// Parse the cli args, and return the kube::LogRecorderConfig
pub fn set_args(
args: &ArgMatches,
) -> Result<kube::LogRecorderConfig, Box<dyn ::std::error::Error>> {
let namespace = args.value_of("NAMESPACE").unwrap().to_string();
let kube_config = if let Some(kube_config) = args.value_of("KUBECONFIG") {
kube_config
} else {
""
};

let mut outfile = "";
let file = args.is_present("FILE");
if file {
outfile = if let Some(o) = args.value_of("OUTFILE") {
o
} else {
"/tmp/wufei/"
}
}

let color = args.is_present("COLOR");
Ok(kube::LogRecorderConfig::new(
namespace.to_string(),
kube_config.to_string(),
file,
color,
outfile.to_string(),
))
}

/// Since Command returns stdout or stderr attrs instead of actual errors, we need a helper
/// function to generate custom errors when dealing with Command.
pub fn generate_err(err_msg: String) -> Result<(), Box<dyn ::std::error::Error>> {
pub fn generate_err(err_msg: String) -> Result<(), Box<dyn::std::error::Error>> {
Err(Box::new(Error::new(ErrorKind::Other, err_msg)))
}

0 comments on commit 628ab11

Please sign in to comment.