aboutsummaryrefslogtreecommitdiff
path: root/config/fish/functions/upgrade.fish
blob: d017ff283d8795ced77cdcfe088e2943315015a2 (plain)
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
#TODO update dotfile directory

function upgrade -d "universal upgrade function"
	function is_command -a cmd -d "check if a command is available"
        type $cmd >/dev/null 2>/dev/null
    end

    function maybe_root -a cmd -d "execute a cmd if it fails try to use sudo"
        set res (eval $cmd 2>&1)
        # cmd failed
        if test $status -gt 0
            # try to guess cause
            if string match -r ".*root|permission.*" "$res"
               set s_cmd ""
               for c in (string split ";" $cmd)
                   set s_cmd $s_cmd (echo $c | sed -r "s/^ ?(and |or )?/\1sudo /" -)
               end
               eval (string join ";" $s_cmd)
            else
                echo "$res" >&2
                return $status
            end
        end
    end

    # Arch
    if is_command pacman
        if is_command yay
            yay -Syu
        else if is_command pacaur
            pacaur -Syu
        else if is_command yaourt
            yaourt -Syua
        else
            maybe_root "pacman -Syu"
        end
    end

    # Debian based
    if is_command apt-get
        if is_command apt
            maybe_root "apt update; and apt dist-upgrade"
        else
            maybe_root "apt-get update; and apt-get dist-upgrade"
        end
    end
end