more complete

This commit is contained in:
2024-01-13 13:01:54 +03:00
parent e5d888de1c
commit 833d3eeb43
37 changed files with 1696 additions and 6 deletions

11
nushell/tools/dns.nu Normal file
View File

@@ -0,0 +1,11 @@
use runonce.nu
export def init [] {
runonce run 'nu_plugin_dns' {
cargo install nu_plugin_dns
register ~/.cargo/bin/nu_plugin_port_list
}
}
# runonce once 'lls' { ls / } --reset true

View File

@@ -0,0 +1,35 @@
# Function to extract archives with different extensions.
export def extract [path:string] {
let $destdir = $path | path dirname
let $name = $path | split row '/' | last
if not ($path | path exists) {
print $"could not find ($path) to expand"
exit 1
}
let handlers = [ [extension command];
['tar\.bz2|tbz|tbz2' 'tar xvjf']
['tar\.gz|tgz' 'tar xvzf']
['tar\.xz|txz' 'tar xvf']
['tar\.Z' 'tar xvZf']
['bz2' 'bunzip2']
['deb' 'ar x']
['gz' 'gunzip']
['pkg' 'pkgutil --expand']
['rar' 'unrar x']
['tar' 'tar xvf']
['xz' 'xz --decompress']
['zip|war|jar|nupkg' 'unzip']
['Z' 'uncompress']
['7z' '7za x']
]
let maybe_handler = ($handlers | where $name =~ $'\.(($it.extension))$')
if ($maybe_handler | is-empty) {
error make { msg: "unsupported file extension" }
} else {
let handler = ($maybe_handler | first)
cd $destdir
nu -c ($handler.command + ' ' + $name)
}
}

61
nushell/tools/git.nu Normal file
View File

@@ -0,0 +1,61 @@
def gitcheck [] {
let $email = ^git config user.email
# Check if Git email is set
if ( $email | str stats | get words) < 2 {
print "Git email is not set!"
let $email = input "Enter your Git email: "
# Set the Git email
^git config --global user.email "$git_email"
echo "Git email set to '$git_email'."
}
}
export def git_configure [] {
gitcheck
mkdir ~/.ssh
let $nr_known_hosts = ^grep github.com ~/.ssh/known_hosts | str stats| get lines
# Check if Git email is set
if $nr_known_hosts < 1 {n
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global pull.rebase false
}
}
export def git_clone [ name: string url: string , --reset=false , --pull=false ] string {
print "git clone"
git_configure
let $dest = $"($env.BASE)/code/($name)"
if $reset {
print $"remove git dir: ($dest)"
rm -f $dest
}
if ($dest | path exists) and ( ( ls $dest | length ) > 0 ) {
mkdir $dest
cd $dest
if $pull == true {
print $"git pull for ($url)"
git pull
}
} else {
print $"git clone ($url)"
cd $"($env.BASE)/code"
git clone --depth 1 --no-single-branch $url
}
# git checkout $CLBRANCH
return $dest
}
def sshagent_loaded [ ] bool {
return ( (ssh-add -l | lines -s | length) > 0 )
}
# if not shagent_loaded() {
# print "could not find ssh-agent please load key in ssh-agent"
# exit 1
# }

78
nushell/tools/hero.nu Normal file
View File

@@ -0,0 +1,78 @@
use git.nu
use varia.nu
def url_crystal [] {
let $ssha = sshagent_loaded
if $ssha {
return "git@github.com:freeflowuniverse/crystallib.git"
} else {
return "https://github.com/freeflowuniverse/crystallib"
}
}
export def clone_crystal [ --reset=false , --pull=false ] string {
let $url = url_crystal
let $r = git_clone crystallib $url
return $r
}
let os = $nu.os-info.name
let arch = $nu.os-info.arch
def url_mycelium [] string {
if $os == "macos" {
if $arch == "aarch64" {
return "https://github.com/threefoldtech/mycelium/releases/download/v0.2.3/mycelium-aarch64-apple-darwin.tar.gz"
} else if $arch == "x86_64" {
return "https://github.com/threefoldtech/mycelium/releases/download/v0.2.3/mycelium-x86_64-apple-darwin.tar.gz"
}
} else if $os == "linux" {
if $arch == "aarch64" {
return "https://github.com/threefoldtech/mycelium/releases/download/v0.2.3/mycelium-aarch64-unknown-linux-musl.tar.gz"
} else if $arch == "x86_64" {
return "https://github.com/threefoldtech/mycelium/releases/download/v0.2.3/mycelium-x86_64-unknown-linux-musl.tar.gz"
}
} else {
error make { msg: "only support darwin & linux" }
}
}
def install_mycelium [] {
let $name = "mycelium"
let tmppath = $"/tmp/($name)"
let $bin_dir = $"($env.BASE)/bin"
mkdir $bin_dir
let $url = url_mycelium
print $url
download_expand $tmppath $"($url)" --minsize 2
mv $"($tmppath)/($name)" $"($bin_dir)/($name)"
chmod +x $"($bin_dir)/($name)"
}
def url_hero [] string {
if $os == "macos" {
if $arch == "aarch64" {
return "https://f003.backblazeb2.com/file/threefold/macos-arm64/hero"
} else if $arch == "x86_64" {
return "https://f003.backblazeb2.com/file/threefold/macos-i64/hero"
}
} else if $os == "linux" {
if $arch == "aarch64" {
error make { msg: "find url for hero on linux arm" }
} else if $arch == "x86_64" {
return "https://f003.backblazeb2.com/file/threefold/linux-i64/hero"
}
} else {
error make { msg: "only support darwin & linux" }
}
}
def install_hero [] {
let $name = "hero"
let $destpath = $"($env.BASE)/bin/hero"
let $url = url_hero
print $url
download $destpath $"($url)" --minsize 4
chmod +x $destpath
}

28
nushell/tools/mypaths.nu Normal file
View File

@@ -0,0 +1,28 @@
export def init [] {
let envpath = $env.PATH
| where ($it | str contains -n '.apple.security')
| where ($it | str contains -n 'Cryptexes')
| where ($it | str contains -n 'dotnet')
| where ($it | str contains -n -i 'gpg')
| append ($env.HOME | path join .cargo/bin)
| append ($env.HOME | path join hero/bin)
| uniq
| sort
load-env {PATH:$envpath}
}
# runonce once 'lls' { ls / } --reset true
# let envpath = $env.PATH
# | where ($it | str contains -n '.apple.security')
# | where ($it | str contains -n 'Cryptexes')
# | where ($it | str contains -n 'dotnet')
# | where ($it | str contains -n -i 'gpg')
# | append ($env.HOME | path join .cargo/bin)
# | append ($env.HOME | path join hero/bin)
# | uniq
# | sort
# load-env {PATH:$envpath}

View File

@@ -0,0 +1,53 @@
# do "/bin/bash <(curl -L https://nixos.org/nix/install) --daemon"
def nixos_remove [] {
if ( "/etc/bashrc.backup-before-nix" | path exists) {
sudo mv -f "/etc/bashrc.backup-before-nix" /etc/bashrc
sudo rm -f "/etc/bash.bashrc.backup-before-nix"
}
}
def "nix_update" [] {
nix-channel --add https://nixos.org/channels/nixos-23.11 nixpkgs
/nix/var/nix/profiles/default/bin/nix-env -u updates
}
def "nix_install" [ ...names ] {
$env.NIXPKGS_ALLOW_UNFREE = 1
$names | each {
|name| /nix/var/nix/profiles/default/bin/nix-env -i $name
}
}
def nixos_install [] {
http get https://nixos.org/nix/install | save -f /tmp/install.sh
chmod +x /tmp/install.sh
^/tmp/install.sh
nix_update
nix-env -u updates
nix_install mc
nix_install nushell
nix_install mdbook mdbook-toc mdbook-pdf mdbook-man-unstable mdbook-mermaid mdbook-footnote mdbook-linkcheck mdbook-kroki-preprocessor
nix_install zola
nix_install vscode-with-extensions
nix_install rustup tailwindcss
nix_install vlang
nix_install go
# nix_install vscode-with-extensions vscode-extensions.zxh404.vscode-proto3
# nix_install vscode-extensions.yzhang.markdown-all-in-one
}
# for i in 'seq -w 1 n'; do userdel guixbuilder$i; done
# nixos_remove
# nixos_install

60
nushell/tools/runonce.nu Normal file
View File

@@ -0,0 +1,60 @@
#run a command only once
export def "run" [
name: string
task: closure
#is the code which will be executed
--reset: bool = false
#means we redo the command
] {
mkdir $"($env.HOME)/hero/done/)"
let state_file = [ $env.HOME 'hero' done ('nu_done_' + $name) ] | path join
if ($reset or (not ($state_file | path exists ) ) ) {
let start = date now
# let result = do $task
do $task
let end = date now
let total = $end - $start | format duration sec
let body = $"given task finished in ($total)"
"" | save $state_file -f
# return $result
# } else {
# "Command " + $name + " has already been run."
}
}
#reset all the done states
export def "reset" [
prefix: string
] {
ls $"($env.HOME)/hero/done/nu_done*" | each { |it| rm $it.name}
}
# runonce once 'lls' { ls / } --reset true
# def "runonce" [
# name: string
# task: closure
# #is the code which will be executed
# --reset: bool = false
# #means we redo the command
# ] {
# let state_file = [ $env.HOME 'hero' ('nu_done_' + $name) ] | path join
# if ($reset or (not ($state_file | path exists ) ) ) {
# let start = date now
# # let result = do $task
# do $task
# let end = date now
# let total = $end - $start | format duration sec
# let body = $"given task finished in ($total)"
# "" | save $state_file -f
# # return $result
# # } else {
# # "Command " + $name + " has already been run."
# }
# }

View File

@@ -0,0 +1,23 @@
# Attempt to remove Homebrew directories and files
echo "Removing Homebrew directories and files..."
let homebrew_dirs = ["/usr/local/Cellar" "/usr/local/Homebrew"
"~/.cache/Homebrew" "~/Library/Caches/Homebrew" "~/Library/Logs/Homebrew"
"/usr/local/Caskroom" "/opt/homebrew" ]
for dir in $homebrew_dirs {
if ($dir | path exists) {
# rm -rfv $dir
let to_remove = $dir | str replace '~' $env.HOME
echo $to_remove
^sudo rm -r $to_remove
# if ($? == 0) {
# echo "File successfully removed"
# } else {
# echo "Failed to remove file"
# }
echo $"Removed: ($dir)"
}
}
echo "Homebrew cleanup process is complete."

65
nushell/tools/varia.nu Normal file
View File

@@ -0,0 +1,65 @@
use runonce.nu
use extractors.nu
export def "cargo search" [ query: string, --limit=10] {
^cargo search $query --limit $limit
| lines
| each {
|line| if ($line | str contains "#") {
$line | parse --regex '(?P<name>.+) = "(?P<version>.+)" +# (?P<description>.+)'
} else {
$line | parse --regex '(?P<name>.+) = "(?P<version>.+)"'
}
}
| flatten
}
export def "download" [ dest: string url: string , --minsize=1] {
let $destdir = $dest | path dirname
mkdir $destdir
print $"download url:($url) to dest:($dest)"
# Download the file
http get $url | save -f $dest
let minsize = $minsize * 1000000
if ($minsize > 0) {
let p = ls $dest
if (($p.size | into int).0 < $minsize) {
print $"The download was too small for ($dest)"
exit 1
} else {
print $"Download ok for ($dest)"
}
}
# echo $"File size: $file_size bytes"
}
export def "download_expand" [ dest: string url: string , --minsize=1] {
mkdir $dest
print $"download expand url:($url) to dest:($dest)"
let $file_name = $url | split row '/' | last
let $ext = $file_name | split row '.' | skip 1 | | str join "." | str downcase
let $dest2 = $"($dest)/downloadedfile.($ext)"
print $dest2
download $dest2 $url --minsize=$minsize
let $e = extract $dest2
}
# export def init_plugins [] {
# runonce run init_plugins {
# register bin/nu_plugin_dns
# register bin/nu_plugin_desktop_notifications
# register bin/nu_plugin_net
# register bin/nu_plugin_port_list
# register bin/nu_plugin_port_scan
# register bin/nu_plugin_query
# } --reset true
# }