---
# file: roles/go/tasks/main.yml

- name: "get go json release page"
  ansible.builtin.uri:
    url: https://go.dev/dl/?mode=json
    return_content: true
  register: go_json_response
  when: go_version is not defined
  tags: go

- name: "parse latest stable go version"
  ansible.builtin.set_fact:
    go_latest_version: "{{ go_json_response.json | community.general.json_query(query) | first | regex_replace('^go', '') }}"
  vars:
    query: "[?stable].version"
  when: go_version is not defined
  tags: go

- name: "download go {{ go_version | default(go_latest_version) }} archive"
  ansible.builtin.get_url:
    url: https://go.dev/dl/go{{ go_version | default(go_latest_version) }}.linux-amd64.tar.gz
    dest: /dev/shm/go{{ go_version | default(go_latest_version) }}.linux-amd64.tar.gz
    mode: '0644'
  tags: go

- name: "remove existing go installation"
  ansible.builtin.file:
    path: /usr/local/go
    state: absent
  tags: go

- name: "extract go {{ go_version | default(go_latest_version) }} to /usr/local"
  ansible.builtin.unarchive:
    src: /dev/shm/go{{ go_version | default(go_latest_version) }}.linux-amd64.tar.gz
    dest: /usr/local
    remote_src: true
  tags: go
