~cadence/bibliogram-updater

ref: 3ad455fe206c705f4858ea2bc42af8cd6da2553a bibliogram-updater/utils/helpers.fish -rw-r--r-- 1.5 KiB
3ad455fe — Cadence Ember add counter restart feature 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env fish

# Write the value of variables into a file, using the names of the variables
# as the keys.
# argv[1]: the path to the template.
# argv[...]: the variable names.
# text: the new contents
function write_config
	set -l file_path $argv[1]
	set -l scripts
	for var in $argv[2..-1]
		set -l name (string split ',' $var)[1]
		set -l surround (string split ',' $var)[2]
		set -a scripts "s!\$$name!$surround$$name$surround!"
	end
	sed -e (string join ';' $scripts) $file_path
end

# Attempts to update.
# exit code: 0 if updated, 1 if no updates available
function do_update
	env LANG=C git pull | grep -v 'Already up[ -]to[ -]date.'
end

# Update the URL for the git remote origin in the working directory.
# argv[1]: the previous URL. this must match, or it will not be changed.
# argv[2]: the new URL to change to.
# exit code: non-zero if problems
function update_origin_url
	set -l original_url_regex $argv[1]
	set -l new_url $argv[2]
	set -l current_url (git remote get-url origin)
	if string match --regex --entire $original_url_regex $current_url > /dev/null
		echo "Changing Git remote URL from $current_url to $new_url"
		git remote set-url origin $new_url; or return 1
		git fetch --quiet; or return 1
	end
end

# Alter a variable with math.
# argv[1]: the variable to alter.
# argv[2...]: arguments to math.
# text: the new value of the variable.
function quick_math
	set -l variable $argv[1]
	set -l math_args $argv[2..-1]
	set $variable (math $$variable $math_args)
	echo $$variable
end