0

I refer to my another question Using cURL, jq, and declaration and for loop condition, I tried to download multiple files from a GitLab private repo, but it downloaded only one. The question was solved, but the only problem is to create automatically the relative paths of the directories based on URLs hierarchy. I wanted to use -o and --create-dirs, but nothing worked. Here is the complete source code, observe the two alternatives at the end of source code.

#!/bin/zsh

branch="master"
id="XXYYYZZ"
repo="my-dotfiles"
private_token="XXX_yyyyyy-zzzzzzzzz"
username="gusbemacbe"

declare -a context_dirs=(
  "home/.config/Code - Insiders/Preferences"
  "home/.config/Code - Insiders/languagepacks.json"
  "home/.config/Code - Insiders/rapid_render.json"
  "home/.config/Code - Insiders/storage.json"
)

declare -a second_dirs=(
  "home/.config/Code - Insiders/Preferences"
  "home/.config/Code - Insiders/languagepacks.json"
  "home/.config/Code - Insiders/rapid_render.json"
  "home/.config/Code - Insiders/storage.json"
)

# curl

# 1st alternative
for urlencode in "${context_dirs[@]}"; do
  file=$(jq -nr --arg v "$urlencode" '$v|@uri')
  curl -sLJ --create-dirs -o "$HOME/Vídeos/" -H "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$username%2F$repo/repository/files/$file/raw?ref=$branch"
done

# 2nd alternative
for urlencode in "${second_dirs[@]}"; do
   file=$(jq -nr --arg v "$urlencode" '$v|@uri')
   cd "$HOME/Imagens/"; curl -sL -J --create-dirs -o -H "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$username%2F$repo/repository/files/$file/raw?ref=$branch"
done

I also tried with:

# The cut -d'/' -f2- will remove the last filename
path="$(${context_dirs[@]} | rev | cut -d'/' -f2- | rev)"

# create the new directoru based on URL address path
directory=$(mkdir -p "$HOME/Vídeos/$path")

for urlencode in "${context_dirs[@]}"; do
  file=$(jq -nr --arg v "$urlencode" '$v|@uri')
  cd "$HOME/Vídeos/$directory"; curl -sLOJH "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$username%2F$repo/repository/files/$file/raw?ref=$branch"
done

The output should be like:

  "~/Vídeos/home/.config/Code - Insiders/Preferences"
  "~/Vídeos/home/.config/Code - Insiders/languagepacks.json"
  "~/Vídeos/home/.config/Code - Insiders/rapid_render.json"
  "~/Vídeos/home/.config/Code - Insiders/storage.json"
  "~/Imagens/home/.config/Code - Insiders/Preferences"
  "~/Imagens/home/.config/Code - Insiders/languagepacks.json"
  "~/Imagens/home/.config/Code - Insiders/rapid_render.json"
  "~/Imagens/home/.config/Code - Insiders/storage.json"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.