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.

muru
  • 69,900
  • 13
  • 192
  • 292
Oo'-
  • 233
  • 1
  • 7

1 Answers1

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
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • What if `-o "$HOME/Vídeos/"`? – Oo'- Jun 01 '20 at 07: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 01 '20 at 07: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. – Oo'- Jun 01 '20 at 07: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 01 '20 at 07: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. – Oo'- Jun 01 '20 at 09:26
  • @GustavoReis As mentioned in the manual, `--created-dirs` only works with `-o` and will create the directories mentioned in the output filename if they don't already exist. – Kusalananda Jun 01 '20 at 09:51
  • Ah, I tried with `curl -sL -J --create-dirs -o -H` and nothing worked. – Oo'- Jun 01 '20 at 09:56
  • @GustavoReis The `-o` option takes an argument, the pathname of the output file. The command that you show seems to want to create a file called `-H`. – Kusalananda Jun 01 '20 at 10:02
  • Then I also tried `curl -sLJ --create-dirs -o "$HOME/Vídeos/" -H` or `curl -sLJ -o "$HOME/Vídeos/" --create-dirs -H` – Oo'- Jun 01 '20 at 10:04
  • @GustavoReis The `-o` option of `curl` takes the name of the output file to write to, not a directory name. – Kusalananda Jun 01 '20 at 10:07
  • Too bad, is there some solution? Sorry for not adding extra example about creating automatically directories based on URL hierarchy in my question. Should I update my question? – Oo'- Jun 01 '20 at 10:13
  • @GustavoReis You may want to ask a separate question about this. I don't usually answer follow-up questions in comments, and it's a separate issue from what your current question asks about. – Kusalananda Jun 01 '20 at 10:41
  • OK, done. Closed. – Oo'- Jun 01 '20 at 11:20