Skip to content

Commit

Permalink
move basic helpers from win.ps1 to init.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed Nov 18, 2024
1 parent 7b67648 commit 21ca0b4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
12 changes: 11 additions & 1 deletion init.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
$HELPERS_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path

# load os/<name>.ps1 files
# -- powershell basic --
function log_msg() { Write-Host -ForegroundColor DarkYellow "--" ($args -join " ") }
function log_error() { Write-Host -ForegroundColor DarkRed "--" ($args -join " ") }
function passwd_generate() {
$length = 12
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?'
$password = -join (1..$length | ForEach-Object { Get-Random -Maximum $characters.length | ForEach-Object { $characters[$_] } })
Write-Output $password
}

# -- load os/<name>.ps1 files --

$scriptToLoad = Join-Path -Path $HELPERS_DIR -ChildPath "os/win.ps1"
. $scriptToLoad
Expand Down
1 change: 1 addition & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
HELPERS_DIR="$(dirname "${BASH_SOURCE[0]}")"

# -- bash basic --

function log_error() { echo -e "\033[00;31m-- $* \033[00m"; }
function log_msg() { echo -e "\033[00;33m-- $* \033[00m"; }
alias bashrc_reload='source $HOME/.bashrc'
Expand Down
38 changes: 24 additions & 14 deletions os/ubu.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -- update --

function ubu_update() {
log_msg "apt update"
sudo apt update
Expand All @@ -7,6 +9,8 @@ function ubu_update() {
sudo apt -y autoremove
}

# -- apt --

alias apt_ppa_remove="sudo add-apt-repository --remove"
alias apt_ppa_list="apt policy"
alias apt_autoremove="sudo apt -y autoremove"
Expand Down Expand Up @@ -37,19 +41,6 @@ function deb_install_file_from_url() {
sudo dpkg -i /tmp/$deb_name
}

alias linux_product_name='sudo dmidecode -s system-product-name'
alias linux_list_gpu="lspci -nn | grep -E 'VGA|Display'"
alias linux_initd_services_list='service --status-all'

function user_sudo_no_password() {
if ! test -d /etc/sudoers.d/; then sudo mkdir -p /etc/sudoers.d/; fi
SET_USER=$USER && sudo sh -c "echo $SET_USER 'ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/sudoers-user"
}

function ubu_disable_snap_dir_at_home() {
sudo snap set system experimental.hidden-snap-folder=true
}

function ubu_fix_esam_hook() {
if ! test /etc/apt/apt.conf.d/20apt-esm-hook.conf; then
sudo systemctl mask apt-news.service
Expand All @@ -65,8 +56,25 @@ function ubu_enable_git_ppa() {
fi
}

# -- admin --

alias linux_product_name='sudo dmidecode -s system-product-name'
alias linux_list_gpu="lspci -nn | grep -E 'VGA|Display'"
alias linux_initd_services_list='service --status-all'

function user_sudo_no_password() {
if ! test -d /etc/sudoers.d/; then sudo mkdir -p /etc/sudoers.d/; fi
SET_USER=$USER && sudo sh -c "echo $SET_USER 'ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/sudoers-user"
}

function ubu_disable_snap_dir_at_home() {
sudo snap set system experimental.hidden-snap-folder=true
}

# -- install --

function ubu_install_latex() {
sudo apt install -y latexmk texlive-latex-extra texlive-fonts-extra
sudo apt install -y latexmk texlive-latex-extra texlive-fonts-extra
}

function ubu_install_node_npm() {
Expand Down Expand Up @@ -101,6 +109,8 @@ function ubu_increase_swap() {
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
}

# -- customize --

function gnome_dark_mode() {
# dark mode
gsettings set org.gnome.desktop.interface cursor-theme 'DMZ-Black'
Expand Down
2 changes: 2 additions & 0 deletions os/win.bash
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ else
alias win_dir_as_unix_format='cygpath -m'
fi

# -- install --

function wsl_install_cuda_cudnn() {
# https://canonical-ubuntu-wsl.readthedocs-hosted.com/en/latest/tutorials/gpu-cuda/
sudo apt-key del 7fa2af80
Expand Down
16 changes: 4 additions & 12 deletions os/win.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
function log_msg() { Write-Host -ForegroundColor DarkYellow "--" ($args -join " ") }
function log_error() { Write-Host -ForegroundColor DarkRed "--" ($args -join " ") }
function Test-HasSudo() { if (Get-Command sudo -errorAction SilentlyContinue) { return $true } else { return $false } }
function Test-IsNotAdmin { -not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator') }
function Test-IsAdmin { ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator') }
# -- update --

function win_update() {
log_msg "win_update"
Expand All @@ -25,14 +21,10 @@ function win_update() {
}
}

# -- admin and password --
# -- admin --

function passwd_generate() {
$length = 10
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?'
$password = -join (1..$length | ForEach-Object { Get-Random -Maximum $characters.length | ForEach-Object { $characters[$_] } })
Write-Output $password
}
function Test-IsNotAdmin { -not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator') }
function Test-IsAdmin { ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator') }

function win_enable_sudo() {
# win 11 supports native sudo https://learn.microsoft.com/en-us/windows/sudo/
Expand Down

0 comments on commit 21ca0b4

Please sign in to comment.