Knife bash completion - improved


#1

OK. Now it works fast, used cache files for command completions and
while i was at it i added roles and nodes completion.

Cache files reside in $CHEF_HOME/.completion_cache and you have to
create this directory if you want cached completion.

And yes, now it will read chef repository locations from either
CHEF_REPOS or $CHEF_HOME/knife.rb

enjoy.

Avishai


#2
This sounded helpful enough that I gave it a shot.  I'm using a Mac, so I ran into a few issues.  Unfortunately I still don't have it working past the first knife parameter (i.e. if I type "knife cook" and hit <tab> I end up with "knife cookbook", but when I type "knife cookbook upl" and hit tab I get an error.  I'm going to give up for now, but if you're interested here's what I had to do (This is mostly from memory so YMMV):

Use Macports to install bash-completion:
sudo port install bash-completion

Update ~/.bash_profile with:

MacPorts Bash shell command completion

if [ -f /opt/local/etc/bash_completion ]; then
. /opt/local/etc/bash_completion
fi

The version of sed used in this script uses “-r” to get extended regular expressions, but for the version of sed on my Mac I had to change that to “-E”. If someone works out the rest, I’d be interested.

Thanks,
Jake.

On Jan 20, 2011, at 4:06 AM, Avishai Ish-Shalom wrote:

OK. Now it works fast, used cache files for command completions and
while i was at it i added roles and nodes completion.

Cache files reside in $CHEF_HOME/.completion_cache and you have to
create this directory if you want cached completion.

And yes, now it will read chef repository locations from either
CHEF_REPOS or $CHEF_HOME/knife.rb

enjoy.

Avishai

<knife.txt>


#3

Well, my wife has a mac. I’ll try to arm wrestle her for some quality
time with it later today.

Regards,
Avishai

On 01/20/2011 04:36 PM, Jacob Vanderdray wrote:

This sounded helpful enough that I gave it a shot. I’m using a Mac, so I ran into a few issues. Unfortunately I still don’t have it working past the first knife parameter (i.e. if I type “knife cook” and hit I end up with “knife cookbook”, but when I type “knife cookbook upl” and hit tab I get an error. I’m going to give up for now, but if you’re interested here’s what I had to do (This is mostly from memory so YMMV):

Use Macports to install bash-completion:
sudo port install bash-completion

Update ~/.bash_profile with:

MacPorts Bash shell command completion

if [ -f /opt/local/etc/bash_completion ]; then
. /opt/local/etc/bash_completion
fi

The version of sed used in this script uses “-r” to get extended regular expressions, but for the version of sed on my Mac I had to change that to “-E”. If someone works out the rest, I’d be interested.

Thanks,
Jake.

On Jan 20, 2011, at 4:06 AM, Avishai Ish-Shalom wrote:

OK. Now it works fast, used cache files for command completions and
while i was at it i added roles and nodes completion.

Cache files reside in $CHEF_HOME/.completion_cache and you have to
create this directory if you want cached completion.

And yes, now it will read chef repository locations from either
CHEF_REPOS or $CHEF_HOME/knife.rb

enjoy.

Avishai

<knife.txt>


#4

Hello,

Thanks for your script, it’s great, specially for beginers on the chef CLI.

I tried your script in OS X too and the problems are related to differences
between GNU sed and BSD sed. I just quick fixed them installing gsed with
macports and changing sed references to gsed.

Also it’s possible to have a knife config file per chef repository instead
of ~/.chef/knife.rb. This way you can work over several chef servers. I am
not sure how to fix that in the script but probably there’s some clues on
knife code. I found the script crashing here too.

Greetings.

Jacobo García López de Araujo
blog: http://robotplaysguitar.com
http://workingwithrails.com/person/13395-jacobo-garc-a

On Thu, Jan 20, 2011 at 3:40 PM, Avishai Ish-Shalom avishai@fewbytes.comwrote:

Well, my wife has a mac. I’ll try to arm wrestle her for some quality
time with it later today.

Regards,
Avishai

On 01/20/2011 04:36 PM, Jacob Vanderdray wrote:

  This sounded helpful enough that I gave it a shot.  I'm using a

Mac, so I ran into a few issues. Unfortunately I still don’t have it
working past the first knife parameter (i.e. if I type “knife cook” and hit
I end up with “knife cookbook”, but when I type "knife cookbook upl"
and hit tab I get an error. I’m going to give up for now, but if you’re
interested here’s what I had to do (This is mostly from memory so YMMV):

Use Macports to install bash-completion:
sudo port install bash-completion

Update ~/.bash_profile with:

MacPorts Bash shell command completion

if [ -f /opt/local/etc/bash_completion ]; then
. /opt/local/etc/bash_completion
fi

The version of sed used in this script uses “-r” to get extended regular
expressions, but for the version of sed on my Mac I had to change that to
"-E". If someone works out the rest, I’d be interested.

Thanks,
Jake.

On Jan 20, 2011, at 4:06 AM, Avishai Ish-Shalom wrote:

OK. Now it works fast, used cache files for command completions and
while i was at it i added roles and nodes completion.

Cache files reside in $CHEF_HOME/.completion_cache and you have to
create this directory if you want cached completion.

And yes, now it will read chef repository locations from either
CHEF_REPOS or $CHEF_HOME/knife.rb

enjoy.

Avishai

<knife.txt>


#5

Great to hear you got it working on OSX.

You may be interested to know that I too am working with several chef
servers. The problem is that bash_completion can not take into account
the knife -c switch because it appears late on the command line. There
are several work-arounds for this, the easiest would be

knife () {

/absolute/path/to/knife $@ -c $CHEF_HOME/.chef/knife.rb

}

then set CHEF_HOME to a different directory when working with the other
server. If, like me, you are constantly switching between chef servers
it’s convenient to set multiple knife functions, e.g.: server1_knife.
This is what I do and it did require modifications to the script -
revised version attached.

The revised version should work for regular knife as well, and feature
data bag completion for bags and items.

Regards,
Avishai

On 01/20/2011 05:46 PM, Jacobo García wrote:

Hello,

Thanks for your script, it’s great, specially for beginers on the chef
CLI.

I tried your script in OS X too and the problems are related to
differences between GNU sed and BSD sed. I just quick fixed them
installing gsed with macports and changing sed references to gsed.

Also it’s possible to have a knife config file per chef
repository instead of ~/.chef/knife.rb. This way you can work over
several chef servers. I am not sure how to fix that in the script but
probably there’s some clues on knife code. I found the script crashing
here too.

Greetings.

Jacobo García López de Araujo
blog: http://robotplaysguitar.com
http://workingwithrails.com/person/13395-jacobo-garc-a

On Thu, Jan 20, 2011 at 3:40 PM, Avishai Ish-Shalom
<avishai@fewbytes.com mailto:avishai@fewbytes.com> wrote:

Well, my wife has a mac. I'll try to arm wrestle her for some quality
time with it later today.

Regards,
Avishai


On 01/20/2011 04:36 PM, Jacob Vanderdray wrote:
>       This sounded helpful enough that I gave it a shot.  I'm
using a Mac, so I ran into a few issues.  Unfortunately I still
don't have it working past the first knife parameter (i.e. if I
type "knife cook" and hit <tab> I end up with "knife cookbook",
but when I type "knife cookbook upl" and hit tab I get an error.
 I'm going to give up for now, but if you're interested here's
what I had to do (This is mostly from memory so YMMV):
>
> Use Macports to install bash-completion:
>  sudo port install bash-completion
>
> Update ~/.bash_profile with:
>
> # MacPorts Bash shell command completion
> if [ -f /opt/local/etc/bash_completion ]; then
>    . /opt/local/etc/bash_completion
> fi
>
> The version of sed used in this script uses "-r" to get extended
regular expressions, but for the version of sed on my Mac I had to
change that to "-E".  If someone works out the rest, I'd be
interested.
>
> Thanks,
> Jake.
>
> On Jan 20, 2011, at 4:06 AM, Avishai Ish-Shalom wrote:
>
>> OK. Now it works fast, used cache files for command completions and
>> while i was at it i added roles and nodes completion.
>>
>> Cache files reside in $CHEF_HOME/.completion_cache and you have to
>> create this directory if you want cached completion.
>>
>>
>> And yes, now it will read chef repository locations from either
>> CHEF_REPOS or $CHEF_HOME/knife.rb
>>
>>
>> enjoy.
>>
>> Avishai
>>
>> <knife.txt>

#6

You don’t need to use the -c option: knife looks for .chef/kinfe.rb in all directories up from the current one and stops when it finds one.

-ash

On 20 Jan 2011, at 16:10, Avishai Ish-Shalom wrote:

Great to hear you got it working on OSX.
You may be interested to know that I too am working with several chef servers. The problem is that bash_completion can not take into account the knife -c switch because it appears late on the command line. There are several work-arounds for this, the easiest would be
knife () {
/absolute/path/to/knife $@ -c $CHEF_HOME/.chef/knife.rb
}
then set CHEF_HOME to a different directory when working with the other server. If, like me, you are constantly switching between chef servers it’s convenient to set multiple knife functions, e.g.: server1_knife. This is what I do and it did require modifications to the script - revised version attached.
The revised version should work for regular knife as well, and feature data bag completion for bags and items.
Regards,
Avishai

On 01/20/2011 05:46 PM, Jacobo García wrote:

Hello,

Thanks for your script, it’s great, specially for beginers on the chef CLI.

I tried your script in OS X too and the problems are related to differences between GNU sed and BSD sed. I just quick fixed them installing gsed with macports and changing sed references to gsed.

Also it’s possible to have a knife config file per chef repository instead of ~/.chef/knife.rb. This way you can work over several chef servers. I am not sure how to fix that in the script but probably there’s some clues on knife code. I found the script crashing here too.

Greetings.

Jacobo García López de Araujo
blog: http://robotplaysguitar.com
http://workingwithrails.com/person/13395-jacobo-garc-a