---
# See `google-cloud-sdk/lib/googlecloudsdk/core/config.json`
- name: gcloud | Get the latest gcloud version from remote metadata file
  vars:
    gcloud_latest_version: '{{ lookup("ansible.builtin.url",
      "https://dl.google.com/dl/cloudsdk/channels/rapid/components-2.json",
      validate_certs="True",
      split_lines="False") | from_json | json_query("version") }}'
  get_url:
    url: https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-{{ gcloud_latest_version }}-linux-{{ gcloud.arch }}.tar.gz
    dest: /tmp/gcloud.tar.gz
    mode: '644'

- name: gcloud | Ensure no previous google-cloud-sdk installation is presented
  file:
    path: '{{ gcloud.dir }}/google-cloud-sdk'
    state: absent

- name: gcloud | Extract downloaded release tarball # noqa: command-instead-of-module
  command:
    cmd: tar -xzf /tmp/gcloud.tar.gz
    chdir: '{{ gcloud.dir }}'
    creates: '{{ gcloud.dir }}/google-cloud-sdk/bin/gcloud'

- name: gcloud | Symlink executables
  file:
    src: '{{ gcloud.dir }}/google-cloud-sdk/bin/{{ item }}'
    dest: '{{ xdg_dir.bin_home }}/{{ item }}'
    state: link
    force: true
  loop:
    - gcloud
    - gsutil
    - bq

- name: gcloud | Create configuration directory
  file:
    path: '{{ xdg_dir.config_home }}/gcloud/configurations'
    state: directory
    mode: '755'

- name: gcloud | Set some configuration options
  copy:
    content: |
      [core]
      disable_usage_reporting = true
      enable_feature_flags = false

      [component_manager]
      disable_update_check = true

      [gcloudignore]
      enabled = true
    dest: '{{ xdg_dir.config_home }}/gcloud/configurations/config_default'
    mode: '644'
