This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Add plugin subcommand to generate shell completions #173
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
90e211b
Add tools subcommand to generate shell completions
chrisvittal 71cd825
Rename tools to plugin. Incorporate feedback.
chrisvittal 094e9b7
Merge branch 'master' into completions-2
chrisvittal 0876f19
Integrate plugin subcommand into beta feature cfg
chrisvittal 300f152
Make plugin subcommand arguments const variables
chrisvittal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* artifact: the requirements tracking tool made for developers | ||
* Copyright (C) 2017 Garrett Berg <@vitiral, vitiral@gmail.com> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Lesser GNU General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the Lesser GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* */ | ||
|
||
use clap::{App, Shell}; | ||
use std::fs; | ||
use dev_prefix::*; | ||
use types::*; | ||
use cmd::types::*; | ||
use cmd::matches::art_app; | ||
|
||
pub fn get_subcommand<'a, 'b>() -> App<'a, 'b> { | ||
SubCommand::with_name("plugin") | ||
.settings(&SUBCMD_SETTINGS) | ||
.about( | ||
"Command for integrating with external plugins, currently only \ | ||
supports generating shell completions", | ||
) | ||
.arg( | ||
Arg::with_name("name") | ||
.required(true) | ||
.possible_values(&[ | ||
"bash-completions", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: make these constants. I can tackle if you are busy.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want BASH_COMPLETIONS => Plugin::Comp(Shell::Bash),
FISH_COMPLETIONS => Plugin::Comp(Shell::Fish),
ZSH_COMPLETIONS => Plugin::Comp(Shell::Zsh),
PS_COMPLETIONS => Plugin::Comp(Shell::PowerShell),
... as the x if x == BASH_COMPLETIONS => Plugin::Comp(Shell::Bash),
x if x == FISH_COMPLETIONS => Plugin::Comp(Shell::Fish),
x if x == ZSH_COMPLETIONS => Plugin::Comp(Shell::Zsh),
x if x == PS_COMPLETIONS => Plugin::Comp(Shell::PowerShell),
... I'm not certain what this becomes, but it seems less elegant for serving the same purpose. |
||
"fish-completions", | ||
"zsh-completions", | ||
"ps-completions", | ||
]) | ||
.help("Plugin name"), | ||
) | ||
.arg( | ||
Arg::with_name("output") | ||
.long("output") | ||
.short("o") | ||
.value_name("PATH") | ||
.help( | ||
"Path of file to place the output of the plugin command \ | ||
(defaults to standard output)", | ||
), | ||
) | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Plugin { | ||
Comp(Shell), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Cmd<'a> { | ||
plugin: Plugin, | ||
output: Option<&'a Path>, | ||
} | ||
|
||
pub fn get_cmd<'a>(matches: &'a ArgMatches) -> Result<Cmd<'a>> { | ||
let plugin = match matches | ||
.value_of("name") | ||
.expect("clap error in argument parsing!") | ||
{ | ||
"bash-completions" => Plugin::Comp(Shell::Bash), | ||
"fish-completions" => Plugin::Comp(Shell::Fish), | ||
"zsh-completions" => Plugin::Comp(Shell::Zsh), | ||
"ps-completions" => Plugin::Comp(Shell::PowerShell), | ||
_ => panic!("clap error in argument parsing!"), | ||
}; | ||
let out = matches.value_of("output").map(|p| Path::new(p).as_ref()); | ||
Ok(Cmd { | ||
plugin: plugin, | ||
output: out, | ||
}) | ||
} | ||
|
||
pub fn run_cmd<W: io::Write>(cmd: &Cmd, w: W) -> Result<u8> { | ||
match cmd.plugin { | ||
Plugin::Comp(_) => if let Some(path) = cmd.output { | ||
let md = fs::metadata(path); | ||
if md.ok().map_or(false, |md| md.is_dir()) { | ||
run_cmd_completions(cmd, w, true) | ||
} else { | ||
// we can't see it as a directory, try to create it as | ||
// a file. | ||
let file = fs::OpenOptions::new() | ||
.write(true) | ||
.truncate(true) | ||
.create(true) | ||
.open(path)?; | ||
run_cmd_completions(cmd, file, false) | ||
} | ||
} else { | ||
run_cmd_completions(cmd, w, false) | ||
}, | ||
} | ||
} | ||
|
||
fn run_cmd_completions<W: io::Write>(cmd: &Cmd, mut w: W, is_dir: bool) -> Result<u8> { | ||
// If use_gen_to is true then we use gen_completions_to as our output | ||
// function, otherwise we use gen_completions. This is only relevant | ||
// for completion generating options. | ||
match cmd.plugin { | ||
Plugin::Comp(shell) => { | ||
let (sh, name) = match shell { | ||
Shell::Bash => ("bash", "art.bash-completion"), | ||
Shell::Fish => ("fish", "art.fish"), | ||
Shell::Zsh => ("zsh", "_art"), | ||
Shell::PowerShell => ("PowerShell", "_art.ps1"), | ||
}; | ||
if !is_dir { | ||
info!( | ||
"Generating {} completions to {}", | ||
sh, | ||
cmd.output.unwrap_or("standard output".as_ref()).display(), | ||
); | ||
art_app().gen_completions_to("art", shell, &mut w) | ||
} else { | ||
// unwrap is safe as cmd.output had to be Some for this branch to | ||
// be taken | ||
info!( | ||
"Generating {} completions to {}", | ||
sh, | ||
cmd.output.unwrap().join(name).display(), | ||
); | ||
art_app().gen_completions("art", shell, cmd.output.unwrap()) | ||
} | ||
Ok(0) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol,
Project
does have to be an option... doesn't it!