#!/usr/bin/env -S nu --stdin
#echo $"stdin: ($in)"

let url = $"($in)"
let use_rc = false
  #let res = (http get $url -r )

  if ($url | is-empty) { exit }
  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" 
  # curl -s https://github.com/project-zot/zot/tags | grep "<h2 " | grep "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") and not ($it | str contains "-alpha") 
     })
  }
  $list | sort -r  | get -o 0 | default ""


