How to conditionally add flag in Fishshell - fish

I want to set a flag when $DEBUG[1] is set:
In bash this would be the way ${DEBUG:+-v}, how to do it in Fishshell?
For example:
yarn config (not set -q DEBUG[1] && echo "-s") delete proxy

If you're in a code block (function/begin-end) you can use a variable with Brace Expansion:
set -q DEBUG[1] && set -l silent "-s"
yarn config $silent delete proxy
edit: faho added that it works also without brace expansion

Related

Makefile target to add k8s cluster config

I want, in one command with args to config kubeconfig, that is able to connect to k8s cluster.
I tried the following which does not work.
cfg:
mkdir ~/.kube
kube: cfg
touch config $(ARGS)
In the args the user should pass the config file content of the cluster (kubeconfig).
If there is a shorter way please let me know.
update
I've used the following which (from the answer) is partially solve the issue.
kube: cfg
case "$(ARGS)" in \
("") printf "Please provide ARGS=/some/path"; exit 1;; \
(*) cp "$(ARGS)" /some/where/else;; \
esac
The problem is because of the cfg which is creating the dir in case the user not providing the args and in the second run when providing the path the dir is already exist and you get an error, is there a way to avoid it ? something like if the arg is not provided dont run the cfg
I assume the user input is the pathname of a file. The make utility can take variable assignments as arguments, in the form of make NAME=VALUE. You refer to these in your Makefile as usual, with $(NAME). So something like
kube: cfg
case "$(ARGS)" in \
("") printf "Please provide ARGS=/some/path"; exit 1;; \
(*) cp "$(ARGS)" /some/where/else;; \
esac
called with
make ARGS=/some/path/file kube
would then execute cp /some/path/file /some/where/else. If that is not what you were asking, please rephrase the question, providing exact details of what you want to do.

How do you override/remove default completions in fish shell?

I am adding completions for a command's subcommand, however fish is retaining the built in completions for the base command, but those no longer apply for the subcommand. I want to disable those base command completions when using the subcommand.
So, to give a specific example, I am adding complete completions for the python3 -m venv command. As I stated, all the builtin python3 completions still show even though they no longer apply. So, when I type python3 -m venv -<TAB>, I get the completions I've added (good!), but also all the default completions too (bad).
So I have this code:
function __fish_python_using_command
# make sure that the command (minus the first item) matches argv
set cmd (commandline -opc)
if [ (count $cmd) -le (count $argv) ]
return 1
end
set idx (math (count $argv)+1)
if [ "$argv" = "$cmd[2..$idx]" ]
return 0
end
return 1
end
complete -f -c python3 -n '__fish_python_using_command -m venv' -s h -l help -d 'Display help creating virtual Python environments'
After running this, when I type when I type python3 -m venv -<TAB> I get:
The new auto complete I defined for --help (correct)
The base defined auto complete for -h (wrong)
All the other python3 base auto complete switches like -V from complete --command python3 --short-option 'V' --description 'Display version and exit' (I want to disable these)
I have considered using the -e flag to remove the defaults when you are in python3 -m venv mode, but that seems like the wrong way to go about it. I'm stumped. How would one disable all existing completions once a subcommand mode is entered? Or would this require a fundamental change to the way the python3 fish builtin completions are structured?
Fish loads completions from files in $fish_complete_path. This is a list of directories, like $PATH. Put your completions into a file named after the command with a ".fish" suffix in an earlier directory and it will take precedence.
E.g. ~/.config/fish/completions/python3.fish.

How to enable kubernetes commands autocomplete

I'm tired of writing all kubectl and kubeadm commands by hand. Is there any way of enabling autocomplete on these commands?
Bash Solution
# Execute these commands
$ echo "source <(kubectl completion bash)" >> ~/.bashrc
$ echo "source <(kubeadm completion bash)" >> ~/.bashrc
# Reload bash without logging out
$ source ~/.bashrc

AutoLS for fish shell

I have an auto-ls script in my conf.d directory
function __autols_hook --description "Auto ls" --on-event fish_prompt
if test "$__autols_last" != (pwd)
if test "$HOME" = (pwd)
else
clear; ls;
# Show git information, and if it's not a git repo, throw error
# into /dev/null. Simples
git status 2>/dev/null
end
end
set -g __autols_last (pwd)
end
This works very well. However, I'd also like this to trigger when I hit enter, in the same pwd, but with no command.
I can't find a way to check if the enter key was pressed but no command
Change the binding for the [enter] key:
bind \cm 'set -l cmd (commandline); test -z "$cmd"; and set -g _empty_command yes; or set -g _empty_command no; commandline -f execute'
Now you can test $_empty_command in your fish_prompt event function. Note that [ctrl-j] also invokes the execute command so you should probably also bind \cj to the same code. But that's optional unless you have an unusual terminal config.

Passing variable from container start to file

I have the following lines in a Dockerfile where I want to set a value in a config file to a default value before the application starts up at the end and provide optionally setting it using the -e option when starting the container.
I am trying to do this using Docker's ENV commando
ENV CONFIG_VALUE default_value
RUN sed -i 's/CONFIG_VALUE/'"$CONFIG_VALUE"'/g' CONFIG_FILE
CMD command_to_start_app
I have the string CONFIG_VALUE explicitly in the file CONFIG_FILE and the default value from the Dockerfile gets correctly substituted. However, when I run the container with the added -e CONFIG_VALUE=100 the substitution is not carried out, the default value set in the Dockerfile is kept.
When I do
docker exec -i -t container_name bash
and echo $CONFIG_VALUE inside the container the environment variable does contain the desired value 100.
Instructions in the Dockerfile are evaluated line-by-line when you do docker build and are not re-evaluated at run-time.
You can still do this however by using an entrypoint script, which will be evaluated at run-time after any environment variables have been set.
For example, you can define the following entrypoint.sh script:
#!/bin/bash
sed -i 's/CONFIG_VALUE/'"$CONFIG_VALUE"'/g' CONFIG_FILE
exec "$#"
The exec "$#" will execute any CMD or command that is set.
Add it to the Dockerfile e.g:
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
Note that if you have an existing entrypoint, you will need to merge it with this one - you can only have one entrypoint.
Now you should find that the environment variable is respected i.e:
docker run -e CONFIG_VALUE=100 container_name cat CONFIG_FILE
Should work as expected.
That shouldn't be possible in a Dockerfile: those instructions are static, for making an image.
If you need runtime instruction when launching a container, you should code them in a script called by the CMD directive.
In other words, the sed would take place in a script that the CMD called. When doing the docker run, that script would have access to the environment variable set just before said docker run.