Falkor/falkorlib

View on GitHub
completion/_falkor

Summary

Maintainability
Test Coverage
#compdef falkor
########################################################################################
# -*- mode:sh; -*-
# Time-stamp: <Sat 2024-08-31 16:27 svarrette>
#
# ZSH completion for [FalkorLib](https://github.com/Falkor/falkorlib)
#
# Copyright (c) 2016 Sebastien Varrette <Sebastien.Varrette@uni.lu>
#
# This was made with the help of several zsh completion examples:
#  - the [kameleon gem](https://github.com/oar-team/kameleon),
#  - the _bundle, _vagrant, _packer and _yum completion file
# and these howto:
#  - https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org
#  - http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions
#  - https://zv.github.io/note/a-review-of-zsh-completion-utilities
########################################################################################
# You can inspired from the (unfortunately buggy) automatic generation of the ZSH
# completion file (see https://github.com/labocho/thor-zsh_completion) using:
#
#      bundle exec falkor zsh-completion > completion/_falkor.autogen
#
# Test modification in live in your shell:
#
#  unfunction _falkor && autoload -U _falkor

typeset -A opt_args

# local _falkor_common_opts
# _falkor_common_opts=(
#     '(- *)'{--version,-V}'[display version info]'
#     '(- *)'{-h,-help,--h,--help}'[display help information]'
#     '(- *)'{-n,--dry_run}'[Perform a trial run with (normally) no changes made]'
# )

# Main dispatcher
_falkor()
{
    local -a _falkor_cmds
    _falkor_cmds=(
        'commands:Lists all available commands'
        'config:Print the current configuration of FalkorLib'
        'gitcrypt:Initialize git-crypt for the current repository'
        'gitignore:Initialize .gitignore for the current repository'
        'help:Describe available commands or one specific command'
        'init:Bootstrap a Git Repository the falkor way'
        'link:Initialize a special symlink (.root, .makefile.d etc.)'
        'make:Initialize one of Falkor Makefile, typically bring as a symlink'
        'mkdocs:Initialize mkdocs for the current project'
        'motd:bootstrap a Message of the Day (motd) file'
        'new:Initialize the directory PATH with one of Falkors template(s)'
        'vagrant:Initialize vagrant in the current project'
        'version:Prints the FalkorLib version information'
    )

    local curcontext="$curcontext" state line ret=1
    typeset -A opt_args

    _arguments -C \
               '(- *)'{--version,-V}'[display version info]' \
               '(- *)'{-h,-help,--h,--help}'[display help information]' \
               '(- *)'{-n,--dry_run}'[Perform a trial run with (normally) no changes made]' \
               ': :->command' \
               '*:: :->options' && ret=0

    case $state in
        (command)
            _describe -t commands 'falkor' _falkor_cmds && ret=0
            ;;
        (options)
            curcontext=${curcontext%:*}-$line[1]:
            case $line[1] in
                (help)
                    _arguments \
                        ":Falkor commands:($(falkor commands))" && ret=0
                    ;;
                (config)
                    __falkor_config && ret=0 ;;
                (gitcrypt)
                    __falkor_gitcrypt && ret=0 ;;
                (gitignore)
                    __falkor_gitignore && ret=0 ;;
                (init)
                    __falkor_init   && ret=0   ;;
                (link)
                    __falkor_link   && ret=0 ;;
                (make)
                    __falkor_make   && ret=0 ;;
                (mkdocs)
                    __falkor_mkdocs && ret=0   ;;
                (motd)
                    __falkor_motd   && ret=0   ;;
                (new)
                    __falkor_new    && ret=0 ;;
                (vagrant)
                    __falkor_vagrant   && ret=0   ;;
                (version|*)
                    _nothing
                    ;;
            esac
            ;;
    esac
    return ret
}

###############
__falkor_new()
{
    local -a _falkor_new_cmds
    _falkor_new_cmds=(
        'article:Bootstrap a LaTeX Article'
        'letter:LaTeX-based letter'
        'license:Generate an Open-Source License for your project'
        'make:Initiate one of Falkor Makefile'
        'pyenv:Initialize pyenv/direnv'
        'readme:Initiate a README file in the PATH directory (''./'' by default)'
        'repo:Bootstrap a Git Repository with my favorite git-flow layout'
        'rvm:Initialize RVM'
        'slides:Bootstrap LaTeX Beamer slides'
        'trash:Add a Trash directory for your LaTeX project'
        'versionfile:initiate a VERSION file'
        'help:Describe subcommands or one specific subcommand'
    )

    local curcontext="$curcontext" state line
    typeset -A opt_args
    _arguments -C \
               ':command:->command' \
               '*::options:->options'

    case $state in
        (command)
            _describe -t commands "falkor new subcommand" _falkor_new_cmds
            return
            ;;
        (options)
            case $line[1] in
                (help)
                    _arguments \
                        ":Falkor new commands:($(falkor new commands))" && ret=0
                    ;;
                (article)
                    _arguments -C \
                               '(-n --name)'{-n,--name}'[Name of the LaTeX project]' \
                               '(-d --dir)'{-d,--dir}':Project directory (relative to the git root directory):_directories' \
                               '--ieee[Use the IEEEtran style for IEEE conference]' \
                               '--llncs[Use the Springer LNCS style]' \
                               '--acm[Use the ACM style]' && ret=0
                    ;;
                (letter)
                    _arguments -C \
                               '(-n --name)'{-n,--name}'[Name of the LaTeX project]' \
                               '(-d --dir)'{-d,--dir}':Project directory (relative to the git root directory):_directories' && ret=0
                    ;;
                (license)
                    _arguments -C \
                               '(-l --license)'{-l,--licence}'[Open Source License to use within your project]:lic:(none Apache-2.0 BSD CC-by-nc-sa GPL-2.0 GPL-3.0 LGPL-2.1 LGPL-3.0 MIT)' \
                               '(-f --licensefile)'{-f,--licensefile}'[License Filename]:filename:(LICENSE)' && ret=0
                    ;;
                (make)
                    _arguments -C \
                               {--latex,-l}'[Makefile to compile LaTeX documents]' \
                               {--gnuplot,--plot,-p}'[Makefile to compile GnuPlot scripts]' \
                               {--generic,-g}'[Generic Makefile for sub directory]' \
                               {--images,-i,--img}'[Makefile to optimize images]' \
                               {--src,-s}'[Path to Falkor''s Makefile for latex_src]' && ret=0
                    ;;
                (pyenv)
                    _arguments -C \
                               '--python[Python version to configure]:version' \
                               '--virtualenv[Virtualenv to configure for this directory]:env' \
                               '*: :->path' && ret=0
                    case "$state" in
                        (path)
                            _directories ;;
                    esac
                    ;;
                (readme)
                    _arguments -C \
                               '--make[Use a Makefile to pilot the repository actions]' \
                               '--rake[Use a Rakefile (and FalkorLib) to pilot the repository actions]' \
                               {--latex,-l}'[Describe a LaTeX project]' \
                               '--gem[Describe a Ruby gem project]' \
                               '--rvm[Describe a RVM-based Ruby project]' \
                               '-pyenv[Describe a pyenv-based Python project]' && ret=0
                    ;;
                (repo)
                    _arguments -C \
                               '(--no-git-flow)--git-flow[Bootstrap the repository with Git-flow]' \
                               '--make[Use a Makefile to pilot the repository actions]' \
                               '--rake[Use a Rakefile (and FalkorLib) to pilot the repository actions]' \
                               '--mkdocs[Initiate MkDocs within your project]' \
                               '--license[Open Source License to use within your project]:lic:(none Apache-2.0 BSD CC-by-nc-sa GPL-2.0 GPL-3.0 LGPL-2.1 LGPL-3.0 MIT)' \
                               '--licensefile[License Filename]:filename:(LICENSE)' \
                               '(-i --interactive)'{-i,--interactive}'[Interactive mode, in particular to confirm Gitflow branch names]' \
                               '(-r --remote-sync)'{-r,--remote-sync}'[Operate a git remote synchronization with remote. By default, all commits stay local]' \
                               '--master[Master Branch name for production releases]' \
                               '(-b --branch --devel --develop)'{-b,--branch,--devel,--develop}'[Branch name for development commits]' \
                               '(-l --latex)'{-l,--latex}'[Initiate a LaTeX project]' \
                               '--rvm[Initiate a RVM-based Ruby project]' \
                               '--ruby[Ruby version to configure for RVM]' \
                               '*: :->path' && ret=0
                    case "$state" in
                        (path)
                            _directories ;;
                    esac
                    ;;
                (rvm)
                    _arguments -C \
                               '--ruby[Ruby version to configure]:version' \
                               '--gemset[RVM gemset to configure for this directory]:gemset' \
                               '*: :->path' && ret=0
                    case "$state" in
                        (path)
                            _directories ;;
                    esac
                    ;;
                (slides)
                    _arguments -C \
                               {--name,-n}'[Name of the LaTeX project]:name' && ret=0
                    ;;
                (trash)
                    _arguments -C \
                               '*: :->path' && ret=0
                    case "$state" in
                        (path)
                            _directories ;;
                    esac
                    ;;
            esac
            ;;
    esac
    return ret
}



###############
__falkor_link()
{
    local -a _falkor_link_cmds
    _falkor_link_cmds=(
        'rootdir:Create a symlink ''.root'' which targets the root of the repository'
        'make:Create a symlink to one of Falkor''s Makefile, set as Git submodule'
        'help:Describe subcommands or one specific subcommand'
    )

    local curcontext="$curcontext" state line
    typeset -A opt_args
    _arguments -C \
               ':command:->command' \
               '*::options:->options'

    case $state in
        (command)
            _describe -t commands "falkor link subcommand" _falkor_link_cmds
            return
            ;;
        (options)
            case $line[1] in
                (help)
                    _arguments \
                        ":Falkor link commands:($(falkor link commands))" && ret=0
                    ;;
                (make)
                    _arguments -C \
                               {--latex,-l}'[Makefile to compile LaTeX documents]' \
                               {--gnuplot,--plot,-p}'[Makefile to compile GnuPlot scripts]' \
                               {--generic,-g}'[Generic Makefile for sub directory]' \
                               '--server:Makefile for fetching key server files' \
                               {--markdown,-m}'[Makefile to convert Markdown files to HTML]' \
                               {--images,-i,--img}'[Makefile to optimize images]' \
                               {--refdir,-d}'[Path to Falkor''s Makefile repository (Relative to Git root dir)]' \
                               {--src,-s}'[Path to Falkor''s Makefile for latex_src]' && ret=0
                    ;;
                (rootdir|root)
                    _arguments -C \
                               {--name,--target,-t,-n}'[Name of the symlink]' && ret=0
            esac
            ;;
    esac
    return ret
}

###############
__falkor_make()
{
    local -a _falkor_make_cmds
    _falkor_make_cmds=(
        'generic:Symlink to Generic Makefile for sub directory'
        'gnuplot:Symlink to a Makefile to compile GnuPlot scripts'
        'help:Describe subcommands or one specific subcommand'
        'latex:Symlink to a Makefile to compile LaTeX documents'
        'repo:Create a root Makefile piloting repository operations'
    )

    local curcontext="$curcontext" state line
    typeset -A opt_args
    _arguments -C \
               ':command:->command' \
               '*::options:->options'

    case $state in
        (command)
            _describe -t commands "falkor make subcommand" _falkor_make_cmds
            return
            ;;
        (options)
            case $line[1] in
                (help)
                    _arguments \
                        ":Falkor make commands:($(falkor make commands))" && ret=0
                    ;;
            esac
            ;;
    esac
    return ret
}

##################
__falkor_config() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
               '(- *)'{--version,-V}'[display version info]' \
               '(- *)'{-h,-help,--h,--help}'[display help information]' \
               '(- *)'{-n,--dry_run}'[Perform a trial run with (normally) no changes made]' \
               '(- *)'{-g,--global}'Operate on the global configuration' \
               '(- *)'{-l,--local}'Operate on the local configuration of the repository'
}


##################
__falkor_gitcrypt() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
               {--owner,-o}'[Email or GPG ID of the owner of the git-crypt root vault]:owner:($(git config user.signingKey) $(git config user.email))' \
               {--keys,-k}'[(space separated) List of GPG IDs allowed to unlock the repository]:keys' \
               {-u,--ulhpc}'[Bootstrap git-crypt for the UL HPC team]' \
               '*:directory:_files -/'
}

##################
__falkor_gitignore() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
               {--all,-a}'[Ignore all files (except .gitignore)]' \
               {--latex,-l}'[Setup .gitignore for LaTeX sources]' \
               {--python,-p}'[Setup .gitignore for Python project]' \
               '*:directory:_files -/'
}

##################
__falkor_init() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
               {--git-flow,--no-git-flow}'[Bootstrap the repository with (or without) Git-flow]' \
               {-i,--interactive}'[Interactive mode, in particular to confirm Gitflow branch names]' \
               {-r,--remote-sync}'[Operate a git remote synchronization with remote. By default, all commits stay local]' \
               '--master[Master Branch name for production releases]' \
               {-b,--branch,--devel,--develop}'[Branch name for development commits]' \
               '--make[Use a Makefile to pilot the repository actions (default)]' \
               '--rake[Use a Rakefile (and FalkorLib) to pilot the repository actions]' \
               '*:directory:_directories'
}


##################
__falkor_mkdocs() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
               {--force,-f}'[Force generation]' \
               '*:directory:_directories'
}


##################
__falkor_motd() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
               {--file,-f}'[File storing the message of the day]:filename:_files' \
               {--width,-w}'[Width for the text]:[0-9]#' \
               {--title,-t}'[Title to be placed in the motd (using asciify or figlet)]:title' \
               '--subtitle[Eventual subtitle to place below the title]:subtitle' \
               '--hostname[Hostname]:hostname' \
               {--support,-s}"[Support/Contact mail]:email:($(git config user.email) hpc-sysadmins@uni.lu)" \
               {--desc,-d}'[Short Description of the host]:desc' \
               {--nodemodel,-n}'[Node Model]:model'  \
               '*:directory:_files -/'
}

##################
__falkor_vagrant() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
               {--force,-f}'[Force generation]' \
               '*:directory:_directories'
}



_falkor "$@"