aboutsummaryrefslogtreecommitdiffstats
path: root/ves-copy.sh
blob: 11dde25ec335be83a8046d166b9d3555b30d78ff (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
#!/bin/sh
#
# Copy and rename sh-ves environments. Environments are single files, so
# both operations are simple file manipulations; only the name header
# needs rewriting.
#
ves_copy() {
    if [ "$#" -lt 2 ]; then
        printf "ERROR: Insufficient arguments. usage: ves copy <src> <dst>\n" >&2
        return 2
    fi

    src="$1"
    dst="$2"

    if ! _shves_check_env_name "$src" || ! _shves_check_env_name "$dst"; then
        return 1
    fi

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

    if _shves_check_env_exists "$dst" 1; then
        printf "ERROR: Environment [%s] already exists.\n" "$dst" >&2
        return 1
    fi

    printf "name: %s\n" "$dst" > "$SHVES_ENV_DIR/$dst"
    grep "^export_var:" "$SHVES_ENV_DIR/$src" >> "$SHVES_ENV_DIR/$dst"

    printf "Environment [%s] copied to [%s]\n" "$src" "$dst"

    return 0
}

ves_rename() {
    if [ "$#" -lt 2 ]; then
        printf "ERROR: Insufficient arguments. usage: ves rename <old> <new>\n" >&2
        return 2
    fi

    if [ "$1" = "$SHVES_ENV_NM" ]; then
        printf "ERROR: Environment [%s] is currently active. Deactivate it first.\n" "$1" >&2
        return 1
    fi

    if ! ves_copy "$1" "$2" > /dev/null; then
        return 1
    fi

    rm -f "$SHVES_ENV_DIR/$1"
    printf "Environment [%s] renamed to [%s]\n" "$1" "$2"

    return 0
}