1

I learned from the following sources:

Description of the script:

  1. Variables, required by GitLab's Repository files API:

    branch="master"
    repo="my-dotfiles"
    private_token="XXY_wwwwwx-qQQQRRSSS"
    username="gusbemacbe"
    
  2. I used a declaration for multiple files:

    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"
    )
    
  3. I used the condition for loop with jq to convert all files from the declaration context_dirs to encoded URLs:

    for urlencode in "${context_dirs[@]}"; do
      paths=$(jq -nr --arg v "$urlencode" '$v|@uri')
    done
    
  4. I used the condition for loop to download with curl multiple files taken from paths converted by jq. It is important that I used -0 and -J to output the file name, and -H for "PRIVATE-TOKEN: $private_token":

    for file in "${paths[@]}"; do 
        curl -sLOJH "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$username%2F$repo/repository/files/$file/raw?ref=$branch"
    done
    

Complete source code:

branch="master"
id="1911000X"
repo="my-dotfiles"
private_token="XXY_wwwwwx-qQQQRRSSS"
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"
)

for urlencode in "${context_dirs[@]}"; do
  paths=$(jq -nr --arg v "$urlencode" '$v|@uri')
done

for file in "${paths[@]}"; do 
    curl -sLOJH "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$username%2F$repo/repository/files/$file/raw?ref=$branch"
done

But the two conditions for loop output only an encoded path and downloaded only a file.

1

The first loop overwrites the value of the variable paths in each iteration. Since you later expect this to be an array, make sure it is created properly:

paths=()
for urlencode in "${context_dirs[@]}"; do
  paths+=( "$(jq -nr --arg v "$urlencode" '$v|@uri')" )
done

Alternatively, combine the two loops:

for urlencode in "${context_dirs[@]}"; do
  file=$(jq -nr --arg v "$urlencode" '$v|@uri')
  curl -sLOJH "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$username%2F$repo/repository/files/$file/raw?ref=$branch"
done
| improve this answer | |
  • What if -o "$HOME/Vídeos/"? – Gustavo Reis Jun 1 at 7:00
  • @GustavoReis Sorry, I don't quite understand the question. The -o option of curl takes the name of the output file to write to, not a directory name. – Kusalananda Jun 1 at 7:05
  • curl -sLOJH -o "$HOME/Vídeos/". I need to send the files to the specific folder (-o) without giving output name (therefore, -O will do it). Maybe better cd "$HOME/Vídeos"; curl -sLOJH", but I will send different files to different specific folders. – Gustavo Reis Jun 1 at 7:12
  • @GustavoReis If you want to use -O, then you have to cd to the correct directory first, this is actually mentioned in the curl manual, in the description of the -O option. This is also outside of the scope of you original question. – Kusalananda Jun 1 at 7:34
  • Sorry, I wanted to use --create-dirs to create the directories from the URLs hierarchy, for example: cd "$HOME/Vídeos"; curl -sLOJ -H "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$username%2F$repo/repository/files/$file/raw?ref=$branch" --create-dirs, and --create-dirs will create "home/.config/Code - Insiders/. But it did not work. – Gustavo Reis Jun 1 at 9:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.