nu_scripts/sourced/update_hosts.nu
Mel Massadian c47ccd42b8
refactor: (#418)
* refactor:  move in one commit

Eveything in modules should probably be changed to `exported` defs.
The idea is to move everything first to keep proper history.

* refactor: 📝 add modules readme (wip)

* refactor:  small move

* refactor: 📝 changed nestring, updated modules readme

* refactor: 📝 to document or not to document

* fix: 🐛 themes

replaced the template to use `main` and regenerated them
from lemnos themes.

* Revert "fix: 🐛 themes"

This reverts commit 4918d3633c.

* refactor:  introduce sourced

- Created a source `root` in which sourcable demos are stored.
  Some might get converted to modules later on.
- Moved some files to bin too.

* fix: 🐛 fehbg.nu

* fix: 🐛 modules/after.nu

* moved some other stuff around

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-04-25 17:56:25 -05:00

64 lines
2.2 KiB
Plaintext

#!/usr/bin/nu
# /etc/hosts update handler
module hosts {
def display_heads [old, new] {
echo "Current file:"
echo $old
echo "New file:"
echo $new
print ""
}
def are_the_same [old_head, new_head] {
(($old_head | first) == ($new_head | first))
}
# Updater function for /etc/hosts
export def update [
--force (-f) # force replace /etc/hosts
] {
# just sample values, feel free to change it but note that it works for StevenBlack files
let LINK = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling-porn/hosts"
let WHITELIST = ["multipasko"]
let BLACKLIST = ["0.0.0.0 tiktok.com"]
let whitelisted = "(" + ($WHITELIST | str join "|") + ")"
let pattern = ($"0.0.0.0.*($whitelisted).*$")
let OLD_FILE = "/etc/hosts"
let TMP_FILE = (fetch $LINK | lines)
if ($env.LAST_EXIT_CODE == 0) {
let OLD_HEAD = (open $OLD_FILE --raw | lines | first 8 | last 3)
let TMP_HEAD = ($TMP_FILE | first 8 | last 3)
display_heads $OLD_HEAD $TMP_HEAD
if (not ((are_the_same $OLD_HEAD $TMP_HEAD) and (not $force))) {
echo "Do you want to update the /etc/hosts file? [Y/n]"
let choice = (input)
if $choice in ["" "Y" "y"] {
let TMP_FILE = if ($WHITELIST|is-empty) {
($TMP_FILE)
} else {
($TMP_FILE | where {|line| $line !~ $pattern})
}
let TMP_FILE = ($TMP_FILE | append $BLACKLIST)
$TMP_FILE | save /tmp/temphostsfile
if ($env.LAST_EXIT_CODE == 0) {
sudo mv /tmp/temphostsfile $OLD_FILE
echo "Done!"
} else {
error make -u {msg: "Something went wrong while overwriting the /etc/hosts file"}
}
}
} else {
echo "No updates available."
}
} else {
error make -u {msg: "Failed downloading the hosts file, try again."}
}
}
}