nushell/config/gclone.nu

134 lines
3.6 KiB
Plaintext
Raw Normal View History

2023-12-22 16:09:35 +00:00
# gclone aliases
def _gclone_dirs [] {
2024-03-12 19:03:24 +00:00
glob ($env.GCLONE_PATH + "/**") -d 3 | each { $in | path basename } | sort | uniq
2023-12-22 16:09:35 +00:00
}
# cd to a git directory
def --env cdg [key: string@_gclone_dirs] {
2024-03-11 14:19:39 +00:00
let dir = glob ($env.GCLONE_PATH + "/**") -d 3
| where { $in != null and ($in | path basename) == $key }
2023-12-22 16:09:35 +00:00
match ($dir | length) {
0 => {
print (
(ansi red_bold) + "error" +
(ansi reset) + (ansi attr_bold) + ":" +
(ansi reset) + " no such file or directory"
)
return
}
1 => {
2024-03-11 14:19:39 +00:00
cd $dir.0
2023-12-22 16:09:35 +00:00
}
_ => {
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
}
2024-03-11 14:19:39 +00:00
cd ($dir | get $choice)
2023-12-22 16:09:35 +00:00
}
}
}
# clone repository in the right place
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 "ssh://") {
$repo_url | parse "ssh://git@{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 server_portless = $attr.server | split row ':' | get 0
let owner = $attr.owner
let repo = $attr.repo
let target = [$env.GCLONE_PATH, $server_portless, $owner, $repo] | path join
let target = if ($target | str ends-with '.git') {
$target | path parse | update extension "" | get parent stem | str join "/"
} else {
$target
}
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 {
2024-01-15 13:48:20 +00:00
$attr | format pattern "ssh://git@{server}/{owner}/{repo}"
2023-12-22 16:09:35 +00:00
} 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
}
}