This commit is contained in:
despiegk 2024-01-09 07:27:19 +03:00
parent 83f0869cbf
commit 0996f96513
26 changed files with 474 additions and 8 deletions

View File

@ -11,27 +11,37 @@ poc for how to work with nushell and get administration done
```bash
curl -L https://nixos.org/nix/install | sh -s -- --daemon
mkdir ~/nixhero
mkdir -p ~/nixhero
cd ~/nixhero
curl https://git.ourworld.tf/freeflowuniverse/nu/raw/branch/main/nixshells/default/shell.nix > shell.nix
#needed to allow nix to install apps which are not fully free and ipensource
export NIXPKGS_ALLOW_UNFREE=1
#the following will start a 100% reproduceable dev environment
nix-shell
# now in the shell do the following
#load nu shell
## load nu shell
nu
#add your ssh key
```
## now we have our shell, whats next
now in the shell do the following (more info how to use it see https://www.nushell.sh)
```bash
##add your ssh key
ssh-add ~/.ssh/despiegk
#get this repo code
let coderoot = "~/code/git.ourworld.tf/freeflowuniverse" ; cd $coderoot
##get this repo code
let coderoot = $"($env.HOME)/code/git.ourworld.tf/freeflowuniverse" ; cd $coderoot
git clone git@git.ourworld.tf:freeflowuniverse/nu.git
#source some nushell scripts which will give you some super powers
ln -s $"($coderoot)/nu/nupower" ~/nixhero/nupower
```
you now can use

View File

@ -11,4 +11,4 @@ pkgs.mkShell {
mc
vscode
];
}
o

View File

@ -0,0 +1,3 @@
ifconfig | lines -s | each { |row| (find "tun") } | each { |row| (find "prefixlen") }
| split column --collapse-empty --regex "[% ]" "interface" "mac" "dev" "pl"
| select "dev" "mac"

32
nupower/examples/menu.nu Normal file
View File

@ -0,0 +1,32 @@
let new_menu = {
name: new_menu
only_buffer_difference: true
marker: "# "
type: {
layout: list
page_size: 10
}
style: {
text: green
selected_text: green_reverse
description_text: yellow
}
source: { |buffer, position|
$nu.scope.vars
| where name =~ $buffer
| sort-by name
| each { |it| {value: $it.name description: $it.type} }
}
}
$env.config.menus = ($env.config.menus | append $new_menu)
# let $nu.scope.vars = [{
# value: # The value that will be inserted in the buffer
# description: # Optional. Description that will be display with the selected value
# span: { # Optional. Span indicating what section of the string will be replaced by the value
# start:
# end:
# }
# extra: [string] # Optional. A list of strings that will be displayed with the selected value. Only works with a description menu
# }]

17
nupower/examples/paths.nu Normal file
View File

@ -0,0 +1,17 @@
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}
# -i means case insensitive
# -n means not

View File

@ -0,0 +1,26 @@
let l = http get https://blog.rust-lang.org/feed.xml
let data = $l.content | into value | where tag == 'author'
echo $data | to json
# now we get one list of the content
$data | get content | each { get content | each { get content } } | flatten | flatten | to json | save /tmp/test.json
let input = "182.178.55.3:4333"
$input | parse --regex '(?P<addr>\d{1,3}(\.\d{1,3}){3})(:(?P<port>\d+))?' | default port 2222 | select addr port
let input = "182.178.55.3"
$input | parse --regex '(?P<addr>\d{1,3}(\.\d{1,3}){3})(:(?P<port>\d+))?' | default port 2222 | select addr port
let input = "182.178.55.3:4333"
$input | parse --regex '(?P<addr>\d{1,3}(\.\d{1,3}){3})(:(?P<port>\d+))?' |
each { |row| if $row.port == $"" { $row | upsert port 2222 } else { $row } } | select addr port
let ipaddr_ = $input | parse --regex '(?P<addr>\d{1,3}(\.\d{1,3}){3})(:(?P<port>\d+))?' | select addr port
let ipaddr = $ipaddr_.port ???
$input | parse '{addr}:{port}'
let data = open data.json | from json
let x = "sss"

View File

@ -0,0 +1,23 @@
#!/usr/bin/env nu
let baseurl = 'https://www.schiit.co.uk/'
let pages = ['headphone-amps' 'dacs']
# Simple script to check stock of https://schiit.co.uk store
def schiit [] {
$pages | par-each { |page|
http get ($baseurl + $page)
|query web -q '.price, .stock, .product-item h5'
|str trim
|group 3
|each {
|x| {
name: $x.0,
avail: $x.1,
price: $x.2
}
}
}
|sort-by avail
}
schiit

View File

@ -0,0 +1,7 @@
# string interpolation
let name = "this is a string"
$"s ($name)"
https://www.nushell.sh/book/working_with_strings.html#string-comparison

View File

@ -0,0 +1,20 @@
"
Band,Album,Year
Fugazi,In On The Kill Taker,1993
Fugazi,The Argument,2001{$x}
Fugazi,7 Songs,1988
Fugazi,Repeater,1990
Fugazi,Steady Diet of Nothing,1991
" | save -f /tmp/test.txt
#-s is skip empty lines
let data = open /tmp/test.txt | lines -s | split column "," Band Album Year | skip 1 | sort-by Year
$data | each { |row| $row.Band + ":" + $row.Album + ":" + $row.Year }
$data |to csv

5
nupower/load.nu Normal file
View File

@ -0,0 +1,5 @@
$env.PATH = (path_add)
use tools/varia.nu
use tools/runonce.nu

View File

@ -0,0 +1,4 @@
cargo install nu_plugin_clipboard
register ~/.cargo/bin/nu_plugin_clipboard
# DOESNT WORK

10
nupower/plugins/dns.nu Normal file
View File

@ -0,0 +1,10 @@
# https://github.com/dead10ck/nu_plugin_dns
use ../tools/runonce.nu
runonce run 'nu_plugin_dns' {
^cargo install nu_plugin_dns
register ~/.cargo/bin/nu_plugin_dns
}

View File

@ -0,0 +1,8 @@
# https://github.com/FMotalleb/nu_plugin_port_list
run_once 'nu_plugin_highlight' {
cargo install nu_plugin_highlight
register ~/.cargo/bin/nu_plugin_highlight
}

11
nupower/plugins/net.nu Normal file
View File

@ -0,0 +1,11 @@
use ../tools/runonce.nu
runonce run 'nu_plugin_net' {
cargo install nu_plugin_net
register ~/.cargo/bin/nu_plugin_net
}
# https://docs.rs/crate/nu_plugin_net/1.3.0

View File

@ -0,0 +1,32 @@
# https://github.com/FMotalleb/nu_plugin_desktop_notifications
cargo install nu_plugin_desktop_notifications
register ~/.cargo/bin/nu_plugin_desktop_notifications
notify -t "test notification body" --summary "test title"
# OTHER IMPLEMENTATION
def "notify_on_done" [
task: closure
] {
let start = date now
let result = do $task
let end = date now
let total = $end - $start | format duration sec
let body = $"given task finished in ($total)"
notify "task is done" $body
return $result
}
def notify [title message] {
# ^osascript -e 'display notification "' + $message + '" with title "' + $title + '"'
let msg = 'display notification "' + $message + '" with title "' + $title + '"'
$msg
^osascript -e $msg
}
notify_on_done { port scan 8.8.8.8 53 }

View File

@ -0,0 +1,2 @@
# https://github.com/Trivernis/nu-plugin-dialog

View File

@ -0,0 +1,10 @@
# https://github.com/FMotalleb/nu_plugin_port_list
run_once 'nu_plugin_port_list' {
cargo install nu_plugin_port_list
register ~/.cargo/bin/nu_plugin_port_list
}
port list -4 -t | where state == LISTEN | select [pid,local_port ] | sort-by local_port

View File

@ -0,0 +1,8 @@
# https://github.com/FMotalleb/nu_plugin_port_scan
cargo install nu_plugin_port_scan
register ~/.cargo/bin/nu_plugin_port_scan
50..60 | par-each { |it| port scan 8.8.8.8 $it -t 100ms } | where is_open | collect { $in }

View File

@ -0,0 +1,11 @@
use ../tools/runonce.nu
runonce run 'nu_plugin_query' {
cargo install nu_plugin_query
register ~/.cargo/bin/nu_plugin_query
}
# https://www.nushell.sh/commands/docs/query_web.html

11
nupower/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,26 @@
# Function to extract archives with different extensions.
export def extract [name:string] {
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)
nu -c ($handler.command + ' ' + $name)
}
}

28
nupower/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 aria2
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
nupower/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."

26
nupower/tools/varia.nu Normal file
View File

@ -0,0 +1,26 @@
use runonce.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 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
# }