#!/usr/bin/env nu 
# Info: Script to run Provisioning
# Author: JesusPerezLorenzo 
# Release: 1.0.4
# Date: 6-2-2024
 
export def _ansi [
  arg: string
]: nothing -> nothing {
  ansi $arg
}

export def get_latest_tag [
  url: string
  use_rc: bool
] nothing:string {
  #let res = (http get $url -r )
  let res = (^curl -s $url | complete)
  let html_content = if ($res.exit_code != 0) {
      print $"🛑 Error (_ansi red)($url)(_ansi reset):\n ($res.exit_code) ($res.stderr)"
      return ""
  } else { $res.stdout }
  let id_target = "releases/tag" 

  let versions = ($html_content | parse --regex '<h2 (?<a>.*?)</a>' | get -o a | each {|it|
    ($it | parse --regex 'releases/tag/(?<version>.*?)"' | get version | get -o 0 | default "") 
  })
  let list = if $use_rc { 
     $versions
  } else {  
     ($versions | filter {|it| 
        not ($it | str contains "-rc") 
     })
  }
  $list | sort -r  | get -o 0 | default ""
}


let version = (get_latest_tag "https://github.com/project-zot/zot/tags" false)
