1

I don't have bat on one of my servers, but I do have docker so rather than trying to extract the files I was thinking of creating a simple docker image like

FROM alpine
RUN apk add --no-cache bat
ENTRYPOINT [ "/usr/bin/bat" ]

Then run it

docker run --rm -it  -w /w -v ${PWD}:/w trajano/bat .bash_profile

But what if I want to do something like

docker run --rm -it  -w /w -v ${PWD}:/w trajano/bat /etc/passwd

Is that possible?

I was thinking of something like changing the image to

FROM alpine
RUN apk add --no-cache bat
COPY bat.sh /bat
RUN chmod 700 /bat
ENTRYPOINT [ "/bat" ]

bat.sh

#!/bin/sh
/usr/bin/bat (some magic here)

Then run as

docker run --rm -it  -v /:/host trajano/bat /etc/passwd

but not sure what that some magic here would look like

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
  • Just bind-mount whatever you want to `bat` into the container. – super Jan 28 '22 at 21:46
  • (Yes, that can be the entire host filesystem; yes, you can add a `docker run -u root` option to run as root; yes, absolutely nothing stops you from editing the host's `/etc/passwd` or `/etc/sudoers` this way.) – David Maze Jan 29 '22 at 01:13

1 Answers1

0

I did a simplistic sh script which checked the first character

FROM alpine
RUN apk add --no-cache bat
COPY bat.sh /bat
RUN chmod 700 /bat
ENTRYPOINT [ "/bat" ]

bat.sh

#!/bin/sh
beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
if [ ! "$1" ]
then
    /usr/bin/bat
elif beginswith "/" "$1"
then
    /usr/bin/bat /host$1
else
    /usr/bin/bat /host$HPWD/$1
fi

And to alias it

alias bat="command docker run --rm -e HPWD=`pwd` -v /:/host:ro -it trajano/bat"

Code in https://github.com/trajano/docker-bat.git

It does not handle multiple paths or command line parameters but it does what I need it to do.

Though in the end I did this

mkdir -p $HOME/.local/bin
curl -sL https://github.com/sharkdp/bat/releases/download/v0.19.0/bat-v0.19.0-x86_64-unknown-linux-musl.tar.gz | tar  zxf - -C $HOME/.local/bin bat-v0.19.0-x86_64-unknown-linux-musl/bat
mv $HOME/.local/bin/bat-v0.19.0-x86_64-unknown-linux-musl/bat $HOME/.local/bin 
rmdir $HOME/.local/bin/bat-v0.19.0-x86_64-unknown-linux-musl/
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265