summaryrefslogtreecommitdiff
path: root/src/lib/config.cli.source.sh
blob: 1df4b840eae981195befbde4d91c9a0bfe85bf7e (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
option_value() {
    local -r option="$1"; shift
    local -r argument="${CLI_OPTION_ARGUMENT[$option]:-value}"

    if (( $# == 0 )) || [ -z "$1" ]; then
        printf 'Error: %s requires a %s\n' "$option" "$argument" >&2
        usage
        exit 1
    fi

    printf '%s\n' "$1"
}

apply_cli_override() {
    local -r config_target="$1"; shift

    if [[ -v "cli_overrides[$config_target]" ]]; then
        printf -v "$config_target" '%s' "${cli_overrides[$config_target]}"
    fi
}

apply_cli_overrides() {
    local config_target

    for config_target in "${CLI_CONFIG_OVERRIDE_TARGETS[@]}"; do
        apply_cli_override "$config_target"
    done

    if (( ${#cli_sync_destinations[@]} > 0 )); then
        SYNC_DESTINATIONS=("${cli_sync_destinations[@]}")
    fi
}

set_cli_option_value() {
    local -r option="$1"; shift
    local -r value="$1"; shift
    local config_target

    if [ "$option" = --sync-destination ]; then
        cli_sync_destinations+=("$value")
        has_config_overrides='yes'
        return
    fi

    config_target="${CLI_OPTION_CONFIG_TARGET[$option]:-}"
    if [ -n "$config_target" ]; then
        cli_overrides["$config_target"]="$value"
        has_config_overrides='yes'
        return
    fi

    printf -v "${CLI_OPTION_TARGET[$option]}" '%s' "$value"
}

set_cli_constant_option() {
    local -r option="$1"; shift
    local -r value="${CLI_OPTION_VALUE[$option]}"
    local config_target

    config_target="${CLI_OPTION_CONFIG_TARGET[$option]:-}"
    if [ -n "$config_target" ]; then
        cli_overrides["$config_target"]="$value"
        has_config_overrides='yes'
        return
    fi

    printf -v "${CLI_OPTION_TARGET[$option]}" '%s' "$value"
}

set_cli_action() {
    local -r selected_action="$1"; shift

    if [ -n "$action" ]; then
        usage
        exit 1
    fi

    action="$selected_action"
}

parse_cli_arguments() {
    local option_arg
    local option
    local option_kind

    while (( $# > 0 )); do
        option="$1"
        shift

        option_kind="${CLI_OPTION_KIND[$option]:-}"
        case "$option_kind" in
            value)
                option_arg=$(option_value "$option" "$@")
                set_cli_option_value "$option" "$option_arg"
                shift
                ;;
            flag|output)
                set_cli_constant_option "$option"
                ;;
            action)
                set_cli_action "$option"
                ;;
            *)
                usage
                exit 1
                ;;
        esac
    done
}