From 2eacb8247b36f415ef57258ffcd95862e1dca657 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 16 Mar 2018 19:11:08 +0100 Subject: [PATCH] Fix config CLI option Closes #254 --- CHANGELOG.md | 5 + Cargo.lock | 2 +- Cargo.toml | 4 +- completions/_gutenberg | 4 +- completions/_gutenberg.ps1 | 141 +++++++++--------- completions/gutenberg.bash | 34 +++-- components/config/src/lib.rs | 4 +- .../getting-started/cli-usage.md | 16 +- src/cli.rs | 2 + src/main.rs | 2 +- 10 files changed, 125 insertions(+), 89 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4109564a..7cd97140 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.3.3 (unreleased) + +- Fixed config flag in CLI + + ## 0.3.2 (2018-03-05) - Fix `serve` command trying to read all files as markdown diff --git a/Cargo.lock b/Cargo.lock index b2959dc7..c3389d39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -383,7 +383,7 @@ dependencies = [ [[package]] name = "gutenberg" -version = "0.3.2" +version = "0.3.3" dependencies = [ "chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.31.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 8cf708c2..4448eb26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "gutenberg" -version = "0.3.2" +version = "0.3.3" authors = ["Vincent Prouillet "] license = "MIT" readme = "README.md" -description = "A static site generator with everything built-in" +description = "A fast static site generator with everything built-in" homepage = "https://github.com/Keats/gutenberg" repository = "https://github.com/Keats/gutenberg" keywords = ["static", "site", "generator", "blog"] diff --git a/completions/_gutenberg b/completions/_gutenberg index 465719fd..39ead5c7 100644 --- a/completions/_gutenberg +++ b/completions/_gutenberg @@ -15,8 +15,8 @@ _gutenberg() { local context curcontext="$curcontext" state line _arguments "${_arguments_options[@]}" \ -'-c[Path to a config file other than config.toml]' \ -'--config[Path to a config file other than config.toml]' \ +'-c+[Path to a config file other than config.toml]' \ +'--config=[Path to a config file other than config.toml]' \ '-h[Prints help information]' \ '--help[Prints help information]' \ '-V[Prints version information]' \ diff --git a/completions/_gutenberg.ps1 b/completions/_gutenberg.ps1 index 8448a770..da561007 100644 --- a/completions/_gutenberg.ps1 +++ b/completions/_gutenberg.ps1 @@ -1,74 +1,79 @@ -@('gutenberg', './gutenberg') | %{ - Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock { - param($wordToComplete, $commandAst, $cursorPosition) +using namespace System.Management.Automation +using namespace System.Management.Automation.Language - $command = '_gutenberg' - $commandAst.CommandElements | - Select-Object -Skip 1 | - %{ - switch ($_.ToString()) { - - 'gutenberg' { - $command += '_gutenberg' - break - } - - 'init' { - $command += '_init' - break - } - - 'build' { - $command += '_build' - break - } - - 'serve' { - $command += '_serve' - break - } - - 'help' { - $command += '_help' - break - } - - default { - break - } - } - } - - $completions = @() - - switch ($command) { - - '_gutenberg' { - $completions = @('init', 'build', 'serve', 'help', '-c', '-h', '-V', '--config', '--help', '--version') - } - - '_gutenberg_init' { - $completions = @('-h', '-V', '--help', '--version') - } - - '_gutenberg_build' { - $completions = @('-h', '-V', '-u', '-o', '--help', '--version', '--base-url', '--output-dir') - } - - '_gutenberg_serve' { - $completions = @('-h', '-V', '-i', '-p', '-o', '-u', '--help', '--version', '--interface', '--port', '--output-dir', '--base-url') - } - - '_gutenberg_help' { - $completions = @('-h', '-V', '--help', '--version') - } +Register-ArgumentCompleter -Native -CommandName 'gutenberg' -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + $commandElements = $commandAst.CommandElements + $command = @( + 'gutenberg' + for ($i = 1; $i -lt $commandElements.Count; $i++) { + $element = $commandElements[$i] + if ($element -isnot [StringConstantExpressionAst] -or + $element.StringConstantType -ne [StringConstantType]::BareWord -or + $element.Value.StartsWith('-')) { + break } + $element.Value + }) -join ';' - $completions | - ?{ $_ -like "$wordToComplete*" } | - Sort-Object | - %{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ } - } + $completions = @(switch ($command) { + 'gutenberg' { + [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'Path to a config file other than config.toml') + [CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'Path to a config file other than config.toml') + [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information') + [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information') + [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information') + [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information') + [CompletionResult]::new('init', 'init', [CompletionResultType]::ParameterValue, 'Create a new Gutenberg project') + [CompletionResult]::new('build', 'build', [CompletionResultType]::ParameterValue, 'Builds the site') + [CompletionResult]::new('serve', 'serve', [CompletionResultType]::ParameterValue, 'Serve the site. Rebuild and reload on change automatically') + [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Prints this message or the help of the given subcommand(s)') + break + } + 'gutenberg;init' { + [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information') + [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information') + [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information') + [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information') + break + } + 'gutenberg;build' { + [CompletionResult]::new('-u', 'u', [CompletionResultType]::ParameterName, 'Force the base URL to be that value (default to the one in config.toml)') + [CompletionResult]::new('--base-url', 'base-url', [CompletionResultType]::ParameterName, 'Force the base URL to be that value (default to the one in config.toml)') + [CompletionResult]::new('-o', 'o', [CompletionResultType]::ParameterName, 'Outputs the generated site in the given path') + [CompletionResult]::new('--output-dir', 'output-dir', [CompletionResultType]::ParameterName, 'Outputs the generated site in the given path') + [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information') + [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information') + [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information') + [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information') + break + } + 'gutenberg;serve' { + [CompletionResult]::new('-i', 'i', [CompletionResultType]::ParameterName, 'Interface to bind on') + [CompletionResult]::new('--interface', 'interface', [CompletionResultType]::ParameterName, 'Interface to bind on') + [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'Which port to use') + [CompletionResult]::new('--port', 'port', [CompletionResultType]::ParameterName, 'Which port to use') + [CompletionResult]::new('-o', 'o', [CompletionResultType]::ParameterName, 'Outputs the generated site in the given path') + [CompletionResult]::new('--output-dir', 'output-dir', [CompletionResultType]::ParameterName, 'Outputs the generated site in the given path') + [CompletionResult]::new('-u', 'u', [CompletionResultType]::ParameterName, 'Changes the base_url') + [CompletionResult]::new('--base-url', 'base-url', [CompletionResultType]::ParameterName, 'Changes the base_url') + [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information') + [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information') + [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information') + [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information') + break + } + 'gutenberg;help' { + [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information') + [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information') + [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information') + [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information') + break + } + }) + + $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | + Sort-Object -Property ListItemText } diff --git a/completions/gutenberg.bash b/completions/gutenberg.bash index 23159a5c..a9da2449 100644 --- a/completions/gutenberg.bash +++ b/completions/gutenberg.bash @@ -32,13 +32,21 @@ _gutenberg() { case "${cmd}" in gutenberg) - opts=" -c -h -V --config --help --version init build serve help" + opts=" -h -V -c --help --version --config init build serve help" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 fi case "${prev}" in + --config) + COMPREPLY=($(compgen -f ${cur})) + return 0 + ;; + -c) + COMPREPLY=($(compgen -f ${cur})) + return 0 + ;; *) COMPREPLY=() ;; @@ -56,19 +64,19 @@ _gutenberg() { case "${prev}" in --base-url) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; -u) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; --output-dir) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; -o) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; *) @@ -117,35 +125,35 @@ _gutenberg() { case "${prev}" in --interface) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; -i) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; --port) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; -p) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; --output-dir) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; -o) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; --base-url) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; -u) - COMPREPLY=("") + COMPREPLY=($(compgen -f ${cur})) return 0 ;; *) diff --git a/components/config/src/lib.rs b/components/config/src/lib.rs index 00dc1bc0..6c6951e2 100644 --- a/components/config/src/lib.rs +++ b/components/config/src/lib.rs @@ -123,8 +123,10 @@ impl Config { /// Parses a config file from the given path pub fn from_file>(path: P) -> Result { let mut content = String::new(); + let path = path.as_ref(); + let file_name = path.file_name().unwrap(); File::open(path) - .chain_err(|| "No `config.toml` file found. Are you in the right directory?")? + .chain_err(|| format!("No `{:?}` file found. Are you in the right directory?", file_name))? .read_to_string(&mut content)?; Config::parse(&content) diff --git a/docs/content/documentation/getting-started/cli-usage.md b/docs/content/documentation/getting-started/cli-usage.md index 0671041d..9aa1af44 100644 --- a/docs/content/documentation/getting-started/cli-usage.md +++ b/docs/content/documentation/getting-started/cli-usage.md @@ -13,7 +13,7 @@ the command help by running `gutenberg --help`. Creates the directory structure used by Gutenberg at the given directory. ```bash -$ gutenberg init +$ gutenberg init my_site ``` will create a new folder named `my_site` and the files/folders needed by @@ -37,10 +37,17 @@ This is useful for example when you want to deploy previews of a site to a dynam deploy previews. +You can override the default output directory 'public' by passing a other value to the `output-dir` flag. + ```bash $ gutenberg build --output-dir $DOCUMENT_ROOT ``` +You can also point to another config file than `config.toml` like so - the position of the `config` option is important: + +```bash +$ gutenberg --config config.staging.toml build +``` + ## serve This will build and serve the site using a local server. You can also specify @@ -63,3 +70,10 @@ hard refresh if possible. Gutenberg does a best-effort to live reload but some changes cannot be handled automatically. If you fail to see your change, you will need to restart `gutenberg serve`. + + +You can also point to another config file than `config.toml` like so - the position of the `config` option is important: + +```bash +$ gutenberg --config config.staging.toml serve +``` diff --git a/src/cli.rs b/src/cli.rs index 70fcfd6e..f4fc5225 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -10,6 +10,8 @@ pub fn build_cli() -> App<'static, 'static> { Arg::with_name("config") .short("c") .long("config") + .default_value("config.toml") + .takes_value(true) .help("Path to a config file other than config.toml") ) .subcommands(vec![ diff --git a/src/main.rs b/src/main.rs index 5d0fa359..2c2ea23f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ mod prompt; fn main() { let matches = cli::build_cli().get_matches(); - let config_file = matches.value_of("config").unwrap_or("config.toml"); + let config_file = matches.value_of("config").unwrap(); match matches.subcommand() { ("init", Some(matches)) => {