dotfiles/nushell/config/gclone.nu

134 lines
3.5 KiB
Plaintext
Raw Normal View History

2023-11-13 09:59:32 +00:00
# gclone aliases
2023-11-17 15:25:05 +00:00
2023-11-23 18:50:26 +00:00
def _gclone_dirs [] {
_ls ($env.GCLONE_PATH + "/*")
| append (_ls ($env.GCLONE_PATH + "/*/*"))
| append (_ls ($env.GCLONE_PATH + "/*/*/*"))
| each { $in.name | path basename }
2023-11-13 09:59:32 +00:00
| sort
| uniq
}
2023-11-24 09:14:42 +00:00
# cd to a git directory
2023-11-17 15:25:05 +00:00
def --env cdg [key: string@_gclone_dirs] {
2023-11-23 18:50:26 +00:00
let dir = _ls ($env.GCLONE_PATH + "/*")
| append (_ls ($env.GCLONE_PATH + "/*/*"))
| append (_ls ($env.GCLONE_PATH + "/*/*/*"))
| where { $in != null and ($in.name | path basename) == $key }
2023-11-20 09:39:11 +00:00
2023-11-17 15:25:05 +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 => {
2023-11-20 09:39:11 +00:00
cd $dir.0.name
2023-11-17 15:25:05 +00:00
}
_ => {
print (
(ansi attr_bold) + "info:" +
(ansi reset) + " multiple entries found, please select one"
)
2023-11-23 18:50:26 +00:00
echo $dir
2023-11-17 15:25:05 +00:00
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)
}
}
}
2023-11-24 09:14:42 +00:00
# clone repository in the right place
2023-11-17 15:25:05 +00:00
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
}
}
2023-11-13 09:59:32 +00:00