• Operand
  • no leg, a'sea.

gram:build

> ./config/nushell/hub.nu

Lenses
(coming soon!)


source './disc.nu'

def collapse [source: any] {
  if ($source | length) > 1 { $source } else {$source.0}
}

def "hub place" [--org (-o), ...names] {
  d -b rec-clone; mkdir ~/disc/rec-clone/code # bind code ~/code | print
  collapse (if $org {
   $names | each { [ ~ disc rec-clone code ($in | path basename) ] }
  } else if ($names | length) > 0 {
   $names | each { [ ~ code ($in)] }
  } else { [[ ~ code ]] } | each { path join | path expand })
}

  # let base = (
  #   let p = "~/code/rebase.hub" | path expand;
  #   if not ($p | path exists) { bind code code };
  #   $p; )

  # if ($org | is-empty) { $base } else {
  #   [ $base ($org | path basename) ] | path join | path expand
  # }

def "hub clone" [base: string] {
  let address = $"git@github.com:($base)"; let name = $"(basename $address)";
  if (($name) | path exists) { cd $name; code fetch; code pull;
  } else { git clone $"($address)" } | print; }

# code handles; mnemonic helpers.
def "hub base" [ --org (-o): string, --page: number = 1 ] {
  if ($page == 1) { print $org }
  let bases = http get $"https://api.github.com/users/($org)/repos?per_page=100&page=($page)"
  if ($bases | is-empty) { [] } else {
    $bases ++ (hub base -o $org --page ($page + 1))
  } }

def "hub rebase" [
  --upgrade (-u),
  --org (-o),
  ...names: string,
] {
  if ($upgrade) {
    let place = hub place
    let orgs = (ls (hub place) | where type == 'dir').name | path basename
    $orgs | tee { print } | each {|org| hub rebase -o $org }
  } else if ($org) {
    $names | each {|org| print $org
      let place = hub place -o $org; mkdir $place; cd $place
      let log = $"($place).(clock | str trim).clone.log" | path expand
      (hub base -o $org) | each { |b| hub clone $b.full_name } | tee { print } | save -a $log
    }
  } else if ($names | length) > 0 {
    $names | each {|base| print $base
      let place = hub place $base | path dirname
      mkdir $place; cd $place
      let log = $"($place).(clock | str trim).clone.log" | path expand
      hub clone $base | tee { print } # | save -a $log
    }
  } else {
    echo "Please choose (o)rg-copy mode or upgrade-(a)ll mode. See `hub rebase -h`"
  }
}

# def "hub issue" [
#   --org (-o): string,
#   --base (-b): string,
#   --number (-n): int,
# ] { http get $"https://api.github.com/repos/($org)/($base)/issues/($number)" }

def "hub issue" [
  org: string,
  base: string,
  number?: int,
] { http get $"https://api.github.com/repos/($org)/($base)/issues/($number)" }

def "hub release" [base: string, ...queries: string] {
  let releases = (http get $"https://api.github.com/repos/($base)/releases/latest").assets
  let choices = $releases | where {|r| $queries | all { $r.name =~ $in } }
  let name = if ($choices | length) > 1 { $choices.name | str join "\n" | fzf } else { $choices.0.name }
  let choice = $choices | where name == $name
  $choice.browser_download_url.0
}

def "hub release pull" [
  base: string,
  command?: string,
  ...queries: string,
  --display (-d),
  # --machine (-m): record,
] {
  let address = hub release $base ...$queries

  let zip = $address | parse '{main}.zip' | get -i 0
  let tarball = $address | path basename | parse '{main}.tar.{compression}' | get -i 0

  # print $zip
  # print $tarball

  let unpack = if not ($tarball | get -i compression | is-empty) {
    { xz: $" | tar -O -Jxf - ($tarball.main)/($command)"
    } | get $tarball.compression
  } else if not ($zip | is-empty) {
    $"tar -O -xvf - ($zip.main)/($command)"
  } else {''}
  if ($unpack | is-empty) { print "Possible error! Unrecognized blob schema."; print $address; print $tarball; }

  let cmd = $"wget -O - ($address) ($unpack) > /usr/local/bin/($command) && chmod +x /usr/local/bin/($command)"
  if $display { $cmd } else { ($cmd) }
}

# def "hub release pull" [
#   base: string,
#   ...queries: string,
#   --display (-d),
#   --binary (-b): string,
# ] {
#   let address = hub release $base ...$queries
#   let zip = $address | parse '{main}.zip' | get -i 0
#   let tarball = $address | path basename | parse '{main}.tar.{compression}' | get -i 0

#   let main = if not ($tarball | is-empty) { $tarball.main
#   } else if not ($zip | is-empty) { $zip.main
#   } else { null }

#   let compression = if not ($tarball | is-empty) { $tarball.compression
#   } else if not ($zip | is-empty) { zip
#   } else { null }

#   # let unpack_call = $unpack + if not ($tarball | get -i compression | is-empty) {
#   # ($zip.main)/($command)

#   # let unpack_pipe = if not ($binary | is-empty) { '' } else {
#     # " -O ($tarball.main)/($command) > /usr/local/bin/($binary) && chmod +x /usr/local/bin/($binary)" }

#   let unpack_call =


#    if ($tarball | get -i compression | is-empty) {
#     { xz: $"| tar -Jxf -"
#     } | get $tarball.compression
#   } else if not ($zip | is-empty) {
#     $"| tar -xvf -"
#   } else {''}
#   if ($unpack_call | is-empty) { print "Possible error! Unrecognized blob schema."; print $address; print $tarball; }

#   let cmd = $"wget -O - ($address) ($unpack)"
#   if $display { $cmd } else { ($cmd) }
# }


# "
# let node = mktemp -d
# cd $node
# wget $address
# als ($address | path basename)
# "


# def "drop hx" [ machine: record, --display (-d) ] {
#   hub release drop 'helix-editor/helix' hx "x86_64-linux.tar.xz" -m $machine -d $display
# }

# def "drop miniserve" [machine: record, --display (-d)] {
#   (hub release drop
#     -m $machine
#     -d $display
#     svenstaro/miniserve miniserve
#     x86_64 linux-musl
#   )
# }