0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

concourse用のbash completionを作成してflyコマンドを楽に使う

Posted at

concourse用のbash completionを作成してflyコマンドを楽に使う

背景

Cloud Foundryでも使われている、CI/CDツールのconcourse用のflyコマンドのサブコマンドを覚えるのが面倒で、日常的に色々なコマンドを使い分ける必要があったので、なんとか解決できないかと調べてみました。

参考にした流れ

  1. flyコマンドのcompletion的なキーワードで検索
    flyコマンドbash completion的なキーワードで検索してみると、公式のissueを発見
    それによると、既にflyコマンドはgo言語で書かれているが、使用しているライブラリで対応出来てるはずとのこと。
    https://github.com/concourse/fly/issues/111

  2. cfコマンドのbash completionの情報
    flyコマンドと同様にgo言語で書かれている、Cloud FoundryのCLIのcfコマンドも比較的最近bash completionに対応というのがReleaseノートにあった。
    https://github.com/cloudfoundry/cli/releases/tag/v6.25.0

作成

  1. completionのファイル格納場所を確認

    まずは今回はMacにbash completionを入れた環境だったので
    どのディレクトリにcompletion用ファイルをセットするのか確認した。

    morika-t@Takeshi-no-MacBook-Pro:~$ cat ~/.bash_profile
    
    [ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
    
    
  2. cfのcompletionファイルの確認
    https://github.com/cloudfoundry/cli/blob/master/ci/installers/completion/cf
    cfコマンド用のcompletion

    # bash completion for Cloud Foundry CLI
    
    _cf-cli() {
        # All arguments except the first one
        args=("${COMP_WORDS[@]:1:$COMP_CWORD}")
        # Only split on newlines
        local IFS=$'\n'
        # Call completion (note that the first element of COMP_WORDS is
        # the executable itself)
        COMPREPLY=($(GO_FLAGS_COMPLETION=1 ${COMP_WORDS[0]} "${args[@]}"))
        return 0
    }
    complete -F _cf-cli cf
    
  3. flyコマンド用に作成

    $ vi /usr/local/etc/bash_completion.d/fly
    # bash completion for Concourse CLI
    
    _fly-cli() {
        # All arguments except the first one
        args=("${COMP_WORDS[@]:1:$COMP_CWORD}")
        # Only split on newlines
        local IFS=$'\n'
        # Call completion (note that the first element of COMP_WORDS is
        # the executable itself)
        COMPREPLY=($(GO_FLAGS_COMPLETION=1 ${COMP_WORDS[0]} "${args[@]}"))
        return 0
    }
    complete -F _fly-cli fly
    

動作確認

ファイルを格納後ターミナルを開き直すなどでcompletionファイルを再読込するとflyコマンドのあとTABキーを押すとこのように動作

$ fly
abort-build        checklist          destroy-team       get-pipeline       hijack             pause-job          pipelines          set-pipeline       targets            unpause-job        validate-pipeline  workers
builds             containers         execute            help               login              pause-pipeline     prune-worker       set-team           teams              unpause-pipeline   volumes
check-resource     destroy-pipeline   expose-pipeline    hide-pipeline      logout             pause-resource     rename-pipeline    sync               trigger-job        unpause-resource   watch

これでflyコマンドをコンソール画面で使い倒せるようになります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?