Skip to content

Commit

Permalink
ZSH support (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbstp authored Apr 5, 2020
1 parent 64b0c1b commit ae17ec6
Show file tree
Hide file tree
Showing 16 changed files with 510 additions and 241 deletions.
18 changes: 5 additions & 13 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ serde_json = "1"
serde_yaml = "0.8"
signal-hook = "0.1"
structopt = "0.3"
tempfile = "3"
which = "3"
wildmatch = "1"

Expand Down
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,40 @@ You can customize kubie's behavior with the `~/.kube/kubie.yaml` file. The setti
available below.

```yaml
configs: # configure where to lookup the kubernetes configs
include: # include these globs
# Force kubie to use a particular shell, if unset detect shell currently in use.
# Possible values: bash, dash, fish, zsh
# Default: unset
shell: bash

# Configure where to look for kubernetes config files.
configs:

# Include these globs.
# Default: values listed below.
include:
- ~/.kube/config
- ~/.kube/*.yml
- ~/.kube/*.yaml
- ~/.kube/configs/*.yml
- ~/.kube/configs/*.yaml
- ~/.kube/kubie/*.yml
- ~/.kube/kubie/*.yaml
exclude: # exclude these globs

# Exclude these globs.
# Default: values listed below.
# Note: kubie's own config file is always excluded.
exclude:
- ~/.kube/kubie.yaml
prompt: # prompt settings
show_depth: true # show depth

# Prompt settings.
prompt:
# When using recursive contexts, show depth when larger than 1.
# Default: true
show_depth: true

# When using zsh, show context and namespace on the right side using RPS1.
# Default: false
zsh_use_rps1: false
```
## Future plans
Expand Down
8 changes: 5 additions & 3 deletions src/cmd/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use signal_hook::iterator::Signals;

use crate::kubeconfig::{self, KubeConfig};
use crate::settings::Settings;
use crate::tempfile::Tempfile;
use crate::vars;

fn run_in_context(kubeconfig: &KubeConfig, args: &[String]) -> anyhow::Result<i32> {
let temp_config_file = Tempfile::new("/tmp", "kubie-config", ".yaml")?;
kubeconfig.write_to(&*temp_config_file)?;
let temp_config_file = tempfile::Builder::new()
.prefix("kubie-config")
.suffix(".yaml")
.tempfile()?;
kubeconfig.write_to(&temp_config_file)?;

let depth = vars::get_depth();
let next_depth = depth + 1;
Expand Down
18 changes: 8 additions & 10 deletions src/cmd/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ use std::fs::Permissions;
use std::os::unix::prelude::*;
use std::path::Path;

use crate::tempfile::Tempfile;
use anyhow::{Context, Result};
use serde::Deserialize;

const VERSION: &str = env!("CARGO_PKG_VERSION");
const LATEST_RELEASE_URL: &str = "https://api.github.com/repos/sbstp/kubie/releases/latest";
const FILENAME: &str = "kubie";

#[derive(Debug, Deserialize)]
pub struct Release {
Expand Down Expand Up @@ -40,12 +38,10 @@ impl Release {

pub fn get_binary_url(&self) -> Option<&str> {
match os_info::get().os_type() {
os_info::Type::Macos => {
os_info::Type::Macos => {
return self.get_macos_binary_url();
}
os_info::Type::Windows => {
None
}
os_info::Type::Windows => None,
_ => {
return self.get_linux_binary_url();
}
Expand All @@ -72,10 +68,12 @@ pub fn update() -> Result<()> {
let download_url = latest_release.get_binary_url().context("Sorry, this release has no build for your OS, please create an issue : https://github.com/sbstp/kubie/issues")?;
let resp = attohttpc::get(download_url).send()?;
if resp.is_success() {
let tmp_file = Tempfile::new("/tmp", FILENAME, "")?;
resp.write_to(&*tmp_file)?;
let old_file = env::current_exe().expect("could not get own binary path");
replace_file(&old_file, tmp_file.path()).context("Update failed. Consider using sudo?")?;
let temp_file = tempfile::Builder::new().prefix("kubie").tempfile()?;
resp.write_to(&temp_file)?;

let old_file = env::current_exe().expect("Could not get own binary path");
replace_file(&old_file, temp_file.path()).context("Update failed. Consider using sudo?")?;

println!(
"Kubie has been updated successfully. Enjoy :) ({})",
Path::display(&old_file)
Expand Down
8 changes: 5 additions & 3 deletions src/kubectl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::str;
use anyhow::{anyhow, Context};

use crate::kubeconfig::KubeConfig;
use crate::tempfile::Tempfile;

pub fn get_namespaces<'a>(kubeconfig: impl Into<Option<&'a KubeConfig>>) -> anyhow::Result<Vec<String>> {
let mut cmd = Command::new("kubectl");
Expand All @@ -15,8 +14,11 @@ pub fn get_namespaces<'a>(kubeconfig: impl Into<Option<&'a KubeConfig>>) -> anyh
let temp_config_file;

if let Some(kubeconfig) = kubeconfig.into() {
temp_config_file = Tempfile::new("/tmp", "kubie-config", ".yaml")?;
kubeconfig.write_to(&*temp_config_file)?;
temp_config_file = tempfile::Builder::new()
.prefix("kubie-config")
.suffix(".yaml")
.tempfile()?;
kubeconfig.write_to(&temp_config_file)?;
cmd.env("KUBECONFIG", temp_config_file.path());
} else {
cmd.env(
Expand Down
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use anyhow::Result;
use structopt::StructOpt;

use cmd::meta::Kubie;
use settings::Settings;

mod cmd;
mod fzf;
mod kubeconfig;
mod kubectl;
mod session;
mod settings;
mod shell;
mod tempfile;
mod vars;

use anyhow::Result;
use structopt::StructOpt;

use cmd::meta::Kubie;
use settings::Settings;

fn main() -> Result<()> {
let settings = Settings::load()?;
let kubie = Kubie::from_args();
Expand Down
14 changes: 13 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub fn expanduser(path: &str) -> String {

#[derive(Debug, Deserialize)]
pub struct Settings {
#[serde(default)]
pub shell: Option<String>,
#[serde(default)]
pub configs: Configs,
#[serde(default)]
Expand Down Expand Up @@ -78,6 +80,7 @@ impl Settings {
impl Default for Settings {
fn default() -> Self {
Settings {
shell: Default::default(),
configs: Configs::default(),
prompt: Prompt::default(),
}
Expand Down Expand Up @@ -122,18 +125,27 @@ fn default_exclude_path() -> Vec<String> {
pub struct Prompt {
#[serde(default = "def_bool_true")]
pub show_depth: bool,
#[serde(default = "def_bool_false")]
pub zsh_use_rps1: bool,
}

impl Default for Prompt {
fn default() -> Self {
Prompt { show_depth: true }
Prompt {
show_depth: true,
zsh_use_rps1: false,
}
}
}

fn def_bool_true() -> bool {
true
}

fn def_bool_false() -> bool {
false
}

#[test]
fn test_expanduser() {
assert_eq!(
Expand Down
56 changes: 0 additions & 56 deletions src/shell.rs

This file was deleted.

46 changes: 46 additions & 0 deletions src/shell/bash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::io::Write;
use std::process::Command;

use anyhow::Result;

use super::ShellSpawnInfo;

pub fn spawn_shell(info: &ShellSpawnInfo) -> Result<()> {
let mut temp_rc_file = tempfile::Builder::new()
.prefix("kubie-bashrc")
.suffix(".bash")
.tempfile()?;

write!(
temp_rc_file,
r#"
if [ -f "$HOME/.bashrc" ] ; then
source "$HOME/.bashrc"
elif [ -f "/etc/skel/.bashrc" ] ; then
source /etc/skel/.bashrc
fi
function __kubie_cmd_pre_exec__() {{
export KUBECONFIG="$KUBIE_KUBECONFIG"
}}
trap '__kubie_cmd_pre_exec__' DEBUG
KUBIE_PROMPT='{}'
PS1="$KUBIE_PROMPT $PS1"
unset KUBIE_PROMPT
"#,
info.prompt,
)?;
temp_rc_file.flush()?;

let mut cmd = Command::new("bash");
cmd.arg("--rcfile");
cmd.arg(temp_rc_file.path());
info.env_vars.apply(&mut cmd);

let mut child = cmd.spawn()?;
child.wait()?;

Ok(())
}
Loading

0 comments on commit ae17ec6

Please sign in to comment.