VERSION 0.8
FROM --pass-args ..+base

all:
    BUILD +git-clone-private-https-set-by-args
    BUILD +git-clone-private-https-set-by-config-command
    BUILD +git-clone-private-https-set-by-config-file
    BUILD +git-clone-private-https-set-by-netrc


git-clone-private-https-base:
    FROM --pass-args ../git-webserver+server-with-user-directory
    RUN echo "#!/bin/sh
set -ex

# first ensure these two /etc/hosts entries are working
ping -c 1 selfsigned.example.com
ping -c 1 buildkitsandbox

# ensure we cant clone without a password
curl https://selfsigned.example.com/testuser/repo.git/info/refs?service=git-upload-pack 2>&1 | grep '401 Authorization Required'

test -z \"\$SSH_AUTH_SOCK\"
" >/tmp/test-earthly-script && chmod +x /tmp/test-earthly-script


git-clone-private-https-set-by-args:
    FROM +git-clone-private-https-base
    RUN echo "
# the git-username and git-password flags are deprecated and only work for github.com, gitlab.com, or bitbucket.com
# however, if we enable a new config and set it to auto here; this will allow the flags to override the username and password settings
earthly --config \$earthly_config  config git \"{selfsigned.example.com: {auth: auto}}\"

# test https password can be set via command line args
#earthly --config \$earthly_config -VD --git-username=testuser --git-password=keepitsecret +test
earthly --config \$earthly_config -VD --git-username=testuser --git-password=keepitsecret selfsigned.example.com/testuser/repo:main+hello
" >> /tmp/test-earthly-script
    DO --pass-args +RUN_EARTHLY_ARGS --pre_command=start-nginx-with-git --earthfile=git-clone-private-https.earth --exec_cmd=/tmp/test-earthly-script


git-clone-private-https-set-by-config-command:
    FROM +git-clone-private-https-base
    RUN echo "
earthly --config \$earthly_config  config git \"{selfsigned.example.com: {auth: https, user: testuser, password: keepitsecret}}\"
earthly --config \$earthly_config --verbose -D +test
earthly --config \$earthly_config --verbose -D selfsigned.example.com/testuser/repo:main+hello
" >> /tmp/test-earthly-script
    DO --pass-args +RUN_EARTHLY_ARGS --pre_command=start-nginx-with-git --earthfile=git-clone-private-https.earth --exec_cmd=/tmp/test-earthly-script


git-clone-private-https-set-by-config-file:
    FROM +git-clone-private-https-base
    RUN echo "
cat << EOF >> \$earthly_config
git:
  selfsigned.example.com:
    auth: https
    user: testuser
    password: keepitsecret
EOF

earthly --config \$earthly_config --verbose -D +test
earthly --config \$earthly_config --verbose -D selfsigned.example.com/testuser/repo:main+hello
" >> /tmp/test-earthly-script
    DO --pass-args +RUN_EARTHLY_ARGS --pre_command=start-nginx-with-git --earthfile=git-clone-private-https.earth --exec_cmd=/tmp/test-earthly-script


git-clone-private-https-set-by-netrc:
    FROM +git-clone-private-https-base
    RUN echo "
echo \"machine selfsigned.example.com
login testuser
password keepitsecret\" > ~/.netrc

earthly --config \$earthly_config --verbose -D +test
earthly --config \$earthly_config --verbose -D selfsigned.example.com/testuser/repo:main+hello
" >> /tmp/test-earthly-script
    DO --pass-args +RUN_EARTHLY_ARGS --pre_command=start-nginx-with-git --earthfile=git-clone-private-https.earth --exec_cmd=/tmp/test-earthly-script

git-clone-private-https-with-pattern-substitute:
    FROM +git-clone-private-https-base
    RUN mkdir -p /var/git/repos/testuser/testproject/testsubproject
    RUN mv /var/git/repos/testuser/repo.git /var/git/repos/testuser/testproject/testsubproject/nestedrepo.git
    RUN echo "
earthly --config \$earthly_config  config git \"{selfsigned.example.com: {auth: https, pattern: 'selfsigned.example.com/testuser/testproject/([^/]+)/([^/]+)', substitute: 'https://testuser:keepitsecret@selfsigned.example.com/testuser/testproject/\\\$1/\\\$2.git'}}\"
cat \$earthly_config
earthly --config \$earthly_config --verbose -D +test
earthly --config \$earthly_config --verbose -D selfsigned.example.com/testuser/testproject/testsubproject/nestedrepo:main+hello
" >> /tmp/test-earthly-script
    DO --pass-args +RUN_EARTHLY_ARGS --pre_command=start-nginx-with-git --earthfile=git-clone-private-https-nestedrepo.earth --exec_cmd=/tmp/test-earthly-script

RUN_EARTHLY_ARGS:
    FUNCTION
    ARG earthfile
    ARG pre_command
    ARG exec_cmd
    DO ..+RUN_EARTHLY \
        --earthfile=$earthfile \
        --pre_command=$pre_command \
        --exec_cmd=$exec_cmd
