aboutsummaryrefslogtreecommitdiffstats
path: root/ves-var-add.sh
blob: d36647cb4eee967a7ab15b9e16dc50db228ade65 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh
ves_var_add() {
    if [ "$#" -lt 2 ]; then
        printf "ERROR: Insufficient arguments provided.\n"
        return 2
    fi

    env=""

    while :; do
        case $1 in
            --env=*)
                env=$( echo $1 | cut -d "=" -f 2 )
                shift
                break
                ;;
            --)
                shift
                break
                ;;

            -?*)
                printf "ERROR: Invalid option [%s]\n" $1 > /dev/stderr
                return 2
                ;;

            *)
                break
        esac
        shift
    done

    active_env=0
    if [ -z "$env" ]; then
        if ! [ -z "$SHVES_ENV_NM"]; then
            env="$SHVES_ENV_NM"
            active_env=1
        else
            printf "ERROR: No valid environment active or specified\n" > /dev/stderr
            return 1
        fi
    fi

    if ! _shves_check_env_name "$env"; then
        return 1
    fi

    if ! _shves_check_env_exists "$env"; then
        return 1;
    fi

    # First, we will get the variable value from the environment file and 
    # create our new version of it.
    fname="$SHVES_ENV_DIR"/"$env"
    var_value=$(grep "export_var:$var=" $SHVES_ENV_DIR/test2 | cut -d '=' -f 2)
    if [ -z "$var_value" ]; then
        var_value="$2"
    else
        var_value="$var_value:$2"
    fi

    # And then update the file to use the new version.
    sed -i "s/export_var:$var=.*"/export_var:$var=$var_value"/" "$fname"

    # If we are updating the currently active environment, export the
    # new value as well,
    if [ $active_env -eq 1 ]; then
        export "$var"="$var_value"
    fi

    # Ah, bash would have made this so much nicer... alas
    # if [ -z ${!1+x} ]; then ...
#    if [ "$active_env" -eq 1 ] && eval [ -z \${$1+x} ]; then
#        printf "ERROR: Specified variable [%s] does not exist in environment.\n" $1 > /dev/stderr
#        printf "\tYou can create it with\n\t ves export %s\n " $1 > /dev/stderr
#        return 2
#    fi
}