gclone in nushell

This commit is contained in:
Thomas Forgione 2023-11-17 16:25:05 +01:00
parent e937574d4c
commit f387e134c7
2 changed files with 122 additions and 6 deletions

View File

@ -1,13 +1,129 @@
# gclone aliases
def _gclone_dirs [] {
let slashes = ($env.GCLONE_PATH | split row "/" | length) + 2
open ([$env.GCLONE_PATH, .cdgcache] | path join)
| lines
| where { ($in | split row "/" | length) >= $slashes }
let slashes = ($env.GCLONE_PATH | split row "/" | length) + 3
_ls ($env.GCLONE_PATH + "/**")
| each { $in.name }
| where { ($in | split row "/" | length) <= $slashes }
| each { $in | split row "/" | last }
| sort
| uniq
}
def-env cdg [key: string@_gclone_dirs] { cd (CLICOLOR_FORCE=1 pgd $key) }
def --env cdg [key: string@_gclone_dirs] {
let dir = _ls ($env.GCLONE_PATH + "/**")
| where { ($in.name | path basename) == $key }
match ($dir | length) {
0 => {
print (
(ansi red_bold) + "error" +
(ansi reset) + (ansi attr_bold) + ":" +
(ansi reset) + " no such file or directory"
)
return
}
1 => {
cd ($dir.name)
}
_ => {
print (
(ansi attr_bold) + "info:" +
(ansi reset) + " multiple entries found, please select one"
)
echo $dir
print (
(ansi --escape 'A') +
(ansi attr_bold) + "Enter your choice: " + (ansi reset)
)
let choice = input
if not ($choice =~ "^[0-9]*$") {
print (
(ansi red_bold) + "error" +
(ansi reset) + (ansi attr_bold) + ":" +
(ansi reset) + " couldn't parse integer"
)
return
}
let choice = $choice | into int
if $choice >= ($dir | length) {
print (
(ansi red_bold) + "error" +
(ansi reset) + (ansi attr_bold) + ":" +
(ansi reset) + " integer is outside range"
)
return
}
cd ($dir | get $choice | get name)
}
}
}
def gclone [repo_url: string] {
let attr = if ($repo_url | str starts-with "https://") {
$repo_url | parse "https://{server}/{owner}/{repo}" | get 0
} else if ($repo_url | str starts-with "http://") {
$repo_url | parse "http://{server}/{owner}/{repo}" | get 0
} else if ($repo_url | str starts-with "git@") {
$repo_url | parse "git@{server}:{owner}/{repo}" | get 0
} else {
print (
(ansi red_bold) + "error" +
(ansi reset) + (ansi attr_bold) + ":" +
(ansi reset) + " failed to parse repo url"
)
return
}
let server = $attr.server
let owner = $attr.owner
let repo = $attr.repo
let target = [$env.GCLONE_PATH, $server, $owner, $repo] | path join
if ($target | path exists) {
print (
(ansi red_bold) + "error" +
(ansi reset) + (ansi attr_bold) + ":" +
(ansi reset) + " clone target already exists"
)
return
}
let clone_url = if $env.GCLONE_FORCE_SSH {
$attr | format "git@{server}:{owner}/{repo}"
} else {
$repo_url
}
print -n (
(ansi attr_bold) + "info:" +
(ansi reset) + " cloning " + $clone_url + "..."
)
let clone = do { git clone --recurse-submodules $clone_url $target } | complete
if $clone.exit_code == 0 {
print " done!"
} else {
print (
"\n" + (ansi red_bold) + "error" +
(ansi reset) + (ansi attr_bold) + ":" +
(ansi reset) + " clone failed:"
)
print -n $clone.stderr
}
}

View File

@ -232,7 +232,7 @@ $env.RUST_BACKTRACE = full
# Gclone env
$env.GCLONE_PATH = '/home/thomas/git'
$env.GCLONE_FORCE_SSH = 'true'
$env.GCLONE_FORCE_SSH = true
# Python virtual env
$env.VIRTUAL_ENV = '/home/thomas/.venv'