summaryrefslogtreecommitdiff
path: root/src/lib/config.cli.source.sh
blob: 0d66c05d357ff1fbc66f8dcd9dc466013686d003 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
cli_option_property() {
    local -r option="$1"; shift
    local -r property="$1"; shift
    local spec
    local field
    local -a fields=()

    spec="${CLI_OPTION_SPEC[$option]:-}"
    read -r -a fields <<< "$spec"

    for field in "${fields[@]}"; do
        case "$field" in
            "$property="*)
                printf '%s\n' "${field#*=}"
                return
                ;;
        esac
    done
}

option_value() {
    local -r option="$1"; shift
    local argument

    argument=$(cli_option_property "$option" argument)
    argument="${argument:-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 "SHURIKEN_CLI_OVERRIDES[$config_target]" ]]; then
        printf -v "$config_target" '%s' \
            "${SHURIKEN_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 (( ${#SHURIKEN_CLI_SYNC_DESTINATIONS[@]} > 0 )); then
        SYNC_DESTINATIONS=("${SHURIKEN_CLI_SYNC_DESTINATIONS[@]}")
    fi
}

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

    append_target=$(cli_option_property "$option" append)
    if [ -n "$append_target" ]; then
        local -n append_ref="$append_target"

        append_ref+=("$value")
        SHURIKEN_CLI_HAS_CONFIG_OVERRIDES='yes'
        return
    fi

    config_target=$(cli_option_property "$option" config)
    if [ -n "$config_target" ]; then
        SHURIKEN_CLI_OVERRIDES["$config_target"]="$value"
        SHURIKEN_CLI_HAS_CONFIG_OVERRIDES='yes'
        return
    fi

    target=$(cli_option_property "$option" target)
    printf -v "$target" '%s' "$value"
}

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

    value=$(cli_option_property "$option" value)
    config_target=$(cli_option_property "$option" config)
    if [ -n "$config_target" ]; then
        SHURIKEN_CLI_OVERRIDES["$config_target"]="$value"
        SHURIKEN_CLI_HAS_CONFIG_OVERRIDES='yes'
        return
    fi

    target=$(cli_option_property "$option" target)
    printf -v "$target" '%s' "$value"
}

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

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

    SHURIKEN_CLI_ACTION="$selected_action"
}

parse_cli_arguments() {
    local option_arg
    local option
    local option_kind

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

        option_kind=$(cli_option_property "$option" kind)
        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
}