aboutsummarylogtreecommitdiffstats
path: root/cf.sh
blob: 188db27a47c80a3d4c7a755f01f09d08d34aa38b (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!/bin/sh

VERSION="1.0.2"

if [ -f "${HOME}/.config/easy-config/config.conf" ]; then
    CONFIG_FILE="${HOME}/.config/easy-config/config.conf"
elif [ -f "${HOME}/.config/easy-config/config.ini" ]; then
    CONFIG_FILE="${HOME}/.config/easy-config/config.ini"
elif [ -f "/etc/easy-config/config.conf" ]; then
    CONFIG_FILE="/etc/easy-config/config.conf"
elif [ -f "./config.conf" ]; then
    CONFIG_FILE="./config.conf"
else
    CONFIG_FILE=""
fi

if [ -f "${HOME}/.config/easy-config/config.conf.default" ]; then
    DEFAULT_CONFIG="${HOME}/.config/easy-config/config.conf.default"
elif [ -f "/usr/share/easy-config/config.conf.default" ]; then
    DEFAULT_CONFIG="/usr/share/easy-config/config.conf.default"
elif [ -f "./config.conf.default" ]; then
    DEFAULT_CONFIG="./config.conf.default"
else
    DEFAULT_CONFIG="${HOME}/.config/easy-config/config.conf.default"
fi

SMART_SEARCH=true
CONFIG_PATH="${HOME}/.config"
ROOT_CONFIG_PATH="/etc"
MAX_RESULTS=20
USE_FZF=true
DEFAULT_EDITOR_FILE="${EDITOR:-nano}"
DEFAULT_EDITOR_FOLDER="${EDITOR:-code}"
AUTO_SELECT=false

CACHE_ENABLED=true
CACHE_FILE="${HOME}/.config/easy-config/cache.conf"

trim() {
    _trim_var="$1"
    _trim_var="${_trim_var#"${_trim_var%%[![:space:]]*}"}"
    _trim_var="${_trim_var%"${_trim_var##*[![:space:]]}"}"
    printf '%s' "$_trim_var"
}

err() {
    printf 'cf: %s\n' "$*" >&2
}

expand_tilde() {
    printf '%s' "$1" | sed "s|^~|$HOME|"
}

parse_list() {
    _raw="$1"
    _raw=$(trim "$_raw")

    case "$_raw" in
        \[*\])
            _raw="${_raw#\[}"
            _raw="${_raw%\]}"
            ;;
    esac

    _result=""
    _remaining="$_raw"
    while [ -n "$_remaining" ]; do
        case "$_remaining" in
            *,*)
                _item="${_remaining%%,*}"
                _remaining="${_remaining#*,}"
                ;;
            *)
                _item="$_remaining"
                _remaining=""
                ;;
        esac
        _item=$(trim "$_item")
        [ -z "$_item" ] && continue
        if [ -n "$_result" ]; then
            _result="${_result}:${_item}"
        else
            _result="$_item"
        fi
    done
    printf '%s' "$_result"
}

check_config() {
    if [ -z "$CONFIG_FILE" ] || [ ! -f "$CONFIG_FILE" ]; then
        err "No config file found."
        err "Run 'cf-setup' to initialize your configuration."
        return 1
    fi
    return 0
}

show_help() {
    cat << EOF
EasyConfig $VERSION

Usage: cf [OPTIONS] <name> [command]

Arguments:
  <name>      Name or path of config file to open
  [command]   Command to run on the target (default: editor)

Options:
  -r, --refresh           Skip cache, re-search for target
  --help                  Show this help message
  --version               Show version
  --list                  List all configured targets
  --config restore        Restore all default settings
  --config restore SECTION
                          Restore specific section (settings/targets/aliases/cache)

Examples:
  cf nvim                 # Edit neovim config
  cf cf                   # Edit cf config
  cf fish code            # Edit fish config with code
  cf kitty cat            # Print kitty config with cat
  cf kitty 'wc -l'        # Count lines in kitty config
  cf kitty 'rm -rf'       # Delete kitty config
  cf -r kitty             # Skip cache, re-search for kitty
  cf --list               # Show all targets
  cf --config restore     # Restore whole config
  cf --config restore settings
                          # Restore only settings section
EOF
}

list_targets() {
    check_config || return 1

    printf 'Config Path:'
    _remaining="$CONFIG_PATH"
    while [ -n "$_remaining" ]; do
        case "$_remaining" in
            *:*)
                _path="${_remaining%%:*}"
                _remaining="${_remaining#*:}"
                ;;
            *)
                _path="$_remaining"
                _remaining=""
                ;;
        esac
        printf '\n  %s' "$_path"
    done
    printf '\n\n'

    printf 'Configured targets:\n'
    in_targets=0
    while IFS= read -r line || [ -n "$line" ]; do
        case "$line" in
            "[targets]") in_targets=1; continue ;;
            "["*"]") in_targets=0 ;;
        esac

        if [ $in_targets -eq 1 ] && [ -n "$line" ]; then
            _key="${line%%=*}"
            _val="${line#*=}"
            _key=$(trim "$_key")
            _val=$(trim "$_val")
            [ -n "$_key" ] && printf '  %-16s -> %s\n' "$_key" "$_val"
        fi
    done < "$CONFIG_FILE"

    printf '\nAliases:\n'
    in_alias=0
    while IFS= read -r line || [ -n "$line" ]; do
        case "$line" in
            "[aliases]") in_alias=1; continue ;;
            "["*"]") in_alias=0 ;;
        esac

        if [ $in_alias -eq 1 ] && [ -n "$line" ]; then
            _key="${line%%=*}"
            _val="${line#*=}"
            _key=$(trim "$_key")
            _val=$(trim "$_val")
            [ -n "$_key" ] && printf '  %-16s -> [%s]\n' "$_key" "$_val"
        fi
    done < "$CONFIG_FILE"
}

reset_defaults() {
    section="$1"
    user_config_file="${HOME}/.config/easy-config/config.conf"
    config_dir="$(dirname "$user_config_file")"

    mkdir -p "$config_dir"

    if [ ! -f "$DEFAULT_CONFIG" ]; then
        err "Default config not found at $DEFAULT_CONFIG"
        err "Please ensure config.conf.default is installed"
        return 1
    fi

    if [ -z "$section" ]; then
        cp "$DEFAULT_CONFIG" "$user_config_file"
        echo "Restored all default settings"
        return 0
    fi

    case "$section" in
        settings|targets|aliases|cache)
            if [ -f "$user_config_file" ]; then
                cp "$user_config_file" "$user_config_file.bak"
                _rebuild_config "$section" "$user_config_file" "$DEFAULT_CONFIG"
                echo "Restored [$section] section (backup at config.conf.bak)"
            else
                cp "$DEFAULT_CONFIG" "$user_config_file"
                echo "No existing config found; copied defaults"
            fi
            ;;
        *)
            err "Unknown section: $section"
            err "Valid sections: settings, targets, aliases, cache"
            return 1
            ;;
    esac
}

_rebuild_config() {
    _section="$1"
    _current="$2"
    _defaults="$3"
    _tmpfile="${_current}.new"

    _default_section=$(sed -n "/^\\[${_section}\\]/,/^\\[/p" "$_defaults" | sed '${ /^\[/d }')

    _in_skip=0
    _inserted=0
    : > "$_tmpfile"

    while IFS= read -r _line || [ -n "$_line" ]; do
        case "$_line" in
            "[$_section]"*)
                printf '%s\n' "$_default_section" >> "$_tmpfile"
                _in_skip=1
                _inserted=1
                ;;
            "["*"]"*)
                if [ $_in_skip -eq 1 ]; then
                    _in_skip=0
                    printf '%s\n' "$_line" >> "$_tmpfile"
                else
                    printf '%s\n' "$_line" >> "$_tmpfile"
                fi
                ;;
            *)
                if [ $_in_skip -eq 0 ]; then
                    printf '%s\n' "$_line" >> "$_tmpfile"
                fi
                ;;
        esac
    done < "$_current"

    if [ $_inserted -eq 0 ]; then
        printf '\n%s\n' "$_default_section" >> "$_tmpfile"
    fi

    mv "$_tmpfile" "$_current"
}

load_config() {
    [ -z "$CONFIG_FILE" ] && return
    [ ! -f "$CONFIG_FILE" ] && return

    _current_section=""
    while IFS= read -r line || [ -n "$line" ]; do
        case "$line" in
            "["*"]")
                _current_section="${line#\[}"
                _current_section="${_current_section%\]}"
                continue
                ;;
        esac

        _key="${line%%=*}"
        _val="${line#*=}"
        _key=$(trim "$_key")
        _val=$(trim "$_val")

        case "$_current_section" in
            settings)
                case "$_key" in
                    default_editor_file)     [ -n "$_val" ] && DEFAULT_EDITOR_FILE="$_val" ;;
                    default_editor_folder)   [ -n "$_val" ] && DEFAULT_EDITOR_FOLDER="$_val" ;;
                    smart_search)            SMART_SEARCH="$_val" ;;
                    config_path)             [ -n "$_val" ] && CONFIG_PATH=$(parse_list "$_val") ;;
                    root_config_path)        [ -n "$_val" ] && ROOT_CONFIG_PATH=$(parse_list "$_val") ;;
                    use_fzf)                 USE_FZF="$_val" ;;
                    max_results)             MAX_RESULTS="$_val" ;;
                    auto_select_first_found_item) AUTO_SELECT="$_val" ;;
                esac
                ;;
            cache)
                case "$_key" in
                    enabled)    CACHE_ENABLED="$_val" ;;
                    cache_path) [ -n "$_val" ] && CACHE_FILE=$(expand_tilde "$_val") ;;
                esac
                ;;
        esac
    done < "$CONFIG_FILE"

    _expanded=""
    _remaining="$CONFIG_PATH"
    while [ -n "$_remaining" ]; do
        case "$_remaining" in
            *:*)
                _path="${_remaining%%:*}"
                _remaining="${_remaining#*:}"
                ;;
            *)
                _path="$_remaining"
                _remaining=""
                ;;
        esac
        _path=$(expand_tilde "$_path")
        if [ -n "$_expanded" ]; then
            _expanded="${_expanded}:${_path}"
        else
            _expanded="$_path"
        fi
    done
    CONFIG_PATH="$_expanded"

    _expanded=""
    _remaining="$ROOT_CONFIG_PATH"
    while [ -n "$_remaining" ]; do
        case "$_remaining" in
            *:*)
                _path="${_remaining%%:*}"
                _remaining="${_remaining#*:}"
                ;;
            *)
                _path="$_remaining"
                _remaining=""
                ;;
        esac
        _path=$(expand_tilde "$_path")
        if [ -n "$_expanded" ]; then
            _expanded="${_expanded}:${_path}"
        else
            _expanded="$_path"
        fi
    done
    ROOT_CONFIG_PATH="$_expanded"
}

get_target() {
    key="$1"

    if [ "$SKIP_CACHE" != "true" ] && [ "$CACHE_ENABLED" = "true" ] && [ -f "$CACHE_FILE" ]; then
        while IFS= read -r line || [ -n "$line" ]; do
            case "$line" in
                "${key}="*)
                    value="${line#*=}"
                    value="$(trim "$value")"
                    printf '%s' "$value"
                    return 0
                    ;;
            esac
        done < "$CACHE_FILE"
    fi

    check_config || return 1

    in_targets=0
    while IFS= read -r line || [ -n "$line" ]; do
        case "$line" in
            "[targets]") in_targets=1; continue ;;
            "["*"]") in_targets=0 ;;
        esac

        if [ $in_targets -eq 1 ] && [ -n "$line" ]; then
            case "$line" in
                "["*"]") ;;
                *)
                    _tkey="${line%%=*}"
                    _tkey=$(trim "$_tkey")
                    if [ "$_tkey" = "$key" ]; then
                        value="${line#*=}"
                        value="$(trim "$value")"
                        printf '%s' "$value"
                        return 0
                    fi
                    ;;
            esac
        fi
    done < "$CONFIG_FILE"

    in_alias=0
    while IFS= read -r line || [ -n "$line" ]; do
        case "$line" in
            "[aliases]") in_alias=1; continue ;;
            "["*"]") in_alias=0 ;;
        esac

        if [ $in_alias -eq 1 ] && [ -n "$line" ]; then
            target_name="${line%%=*}"
            _aliases_raw="${line#*=}"
            target_name=$(trim "$target_name")
            _aliases_raw=$(trim "$_aliases_raw")

            _aliases_parsed=$(parse_list "$_aliases_raw")

            _remaining_a="$_aliases_parsed"
            while [ -n "$_remaining_a" ]; do
                case "$_remaining_a" in
                    *:*)
                        _alias="${_remaining_a%%:*}"
                        _remaining_a="${_remaining_a#*:}"
                        ;;
                    *)
                        _alias="$_remaining_a"
                        _remaining_a=""
                        ;;
                esac
                _alias=$(trim "$_alias")
                if [ "$_alias" = "$key" ]; then
                    get_target "$target_name"
                    return $?
                fi
            done
        fi
    done < "$CONFIG_FILE"

    return 1
}

smart_search() {
    query="$1"

    case "$query" in
        /*)
            _search_paths="/"
            ;;
        ./)
            _search_paths="."
            ;;
        ~*)
            _search_paths="$HOME"
            ;;
        *)
            if [ "$(id -u)" -eq 0 ]; then
                _search_paths="$ROOT_CONFIG_PATH"
            else
                _search_paths="$CONFIG_PATH"
            fi
            ;;
    esac

    results=""

    _remaining="$_search_paths"
    while [ -n "$_remaining" ]; do
        case "$_remaining" in
            *:*)
                search_path="${_remaining%%:*}"
                _remaining="${_remaining#*:}"
                ;;
            *)
                search_path="$_remaining"
                _remaining=""
                ;;
        esac

        if [ ! -d "$search_path" ]; then
            continue
        fi

        case "$query" in
            */*)
                direct_path="$search_path/$query"
                if [ -e "$direct_path" ]; then
                    printf '%s' "$direct_path"
                    return 0
                fi
                ;;
        esac

        case "$query" in
            */*)
                if command -v fd > /dev/null 2>&1; then
                    _found=$(fd -HI -p "$query" "$search_path" 2>/dev/null | head -n "$MAX_RESULTS")
                else
                    _found=$(find "$search_path" -ipath "*$query*" 2>/dev/null | head -n "$MAX_RESULTS")
                fi
                ;;
            *)
                if command -v fd > /dev/null 2>&1; then
                    _found=$(fd -HI "$query" "$search_path" 2>/dev/null | head -n "$MAX_RESULTS")
                else
                    _found=$(find "$search_path" -iname "*$query*" 2>/dev/null | head -n "$MAX_RESULTS")
                fi
                ;;
        esac

        if [ -n "$_found" ]; then
            if [ -n "$results" ]; then
                results="${results}
${_found}"
            else
                results="$_found"
            fi
        fi
    done

    if [ -z "$results" ]; then
        err "No results found for '$query'"
        return 1
    fi

    count=$(printf '%s\n' "$results" | wc -l)

    if [ "$count" -eq 1 ]; then
        printf '%s' "$results"
        return 0
    fi

    if [ "$AUTO_SELECT" = "true" ]; then
        printf '%s' "$results" | head -1
        return 0
    fi

    printf '%s results found:\n' "$count" >&2

    if [ "$USE_FZF" = "true" ] && command -v fzf > /dev/null 2>&1; then
        selected=$(printf '%s\n' "$results" | fzf)
        if [ -n "$selected" ]; then
            printf '%s' "$selected"
            return 0
        fi
        return 1
    fi

    i=1
    while IFS= read -r result; do
        printf '%d) %s\n' "$i" "$result" >&2
        i=$((i + 1))
    done <<EOF
$results
EOF

    printf 'Select 1-%s: ' "$count" >&2
    read choice < /dev/tty

    case "$choice" in
        ''|*[!0-9]*)
            err "Invalid selection"
            return 1
            ;;
    esac

    if [ "$choice" -lt 1 ] || [ "$choice" -gt "$count" ]; then
        err "Selection out of range (1-$count)"
        return 1
    fi

    printf '%s\n' "$results" | sed -n "${choice}p"
}

save_cache() {
    [ "$CACHE_ENABLED" != "true" ] && return

    key="$1"
    path="$2"

    mkdir -p "$(dirname "$CACHE_FILE")"

    if [ -f "$CACHE_FILE" ]; then
        grep -Fv "${key}=" "$CACHE_FILE" > "$CACHE_FILE.tmp" 2>/dev/null || : > "$CACHE_FILE.tmp"
        mv "$CACHE_FILE.tmp" "$CACHE_FILE"
    fi

    printf '%s=%s\n' "$key" "$path" >> "$CACHE_FILE"
}

pick_command() {
    target="$1"
    override_cmd="$2"

    if [ -n "$override_cmd" ]; then
        printf '%s' "$override_cmd"
        return 0
    fi

    case "$target" in
        */\*) printf '%s' "$DEFAULT_EDITOR_FOLDER" ;;
        *)
            if [ -d "$(expand_tilde "$target")" ]; then
                printf '%s' "$DEFAULT_EDITOR_FOLDER"
            else
                printf '%s' "$DEFAULT_EDITOR_FILE"
            fi
            ;;
    esac
}

cf() {
    if [ $# -lt 1 ]; then
        show_help
        return 1
    fi

    SKIP_CACHE=false
    while [ $# -gt 0 ]; do
        case "$1" in
            --help)
                show_help
                return 0
                ;;
            --version)
                echo "cf version $VERSION"
                return 0
                ;;
            --list)
                load_config
                list_targets
                return $?
                ;;
            --config)
                if [ "$2" = "restore" ]; then
                    reset_defaults "$3"
                    return $?
                else
                    err "Unknown config command: $2"
                    err "Use 'cf --config restore [SECTION]'"
                    return 1
                fi
                ;;
            -r|--refresh)
                SKIP_CACHE=true
                shift
                continue
                ;;
            -*)
                err "Unknown option: $1"
                return 1
                ;;
            *)
                break
                ;;
        esac
    done

    if [ $# -lt 1 ]; then
        show_help
        return 1
    fi

    load_config

    name="$1"
    override_cmd="$2"

    # Validate override_cmd - should not be a flag
    if [ -n "$override_cmd" ] && [ "${override_cmd#-}" != "$override_cmd" ]; then
        err "Flags must come before the target name. Use 'cf [OPTIONS] <target> [command]'"
        return 1
    fi

    if [ "$SKIP_CACHE" = "true" ]; then
        target=$(get_target "$name")
        if [ -z "$target" ] && [ "$SMART_SEARCH" = "true" ]; then
            target=$(smart_search "$name")
            if [ -z "$target" ]; then
                return 1
            fi
            save_cache "$name" "$target"
        fi
    else
        target=$(get_target "$name")
        if [ -z "$target" ] && [ "$SMART_SEARCH" = "true" ]; then
            target=$(smart_search "$name")
            if [ -z "$target" ]; then
                return 1
            fi
            save_cache "$name" "$target"
        fi
    fi

    if [ -z "$target" ]; then
        err "Target '$name' not found"
        return 1
    fi

    target=$(expand_tilde "$target")

    cmd=$(pick_command "$target" "$override_cmd")

    _cmd_name="${cmd%% *}"
    if ! command -v "$_cmd_name" > /dev/null 2>&1; then
        err "Command '$_cmd_name' not found in PATH"
        return 1
    fi

    case "$target" in
        */\*) target="${target%/*}" ;;
    esac

    if [ -d "$target" ]; then
        target_type="(folder)"
    elif [ -f "$target" ]; then
        target_type="(file)"
    else
        err "Warning: '$target' does not exist"
        target_type="(unknown)"
    fi

    echo "Opening $target_type with $cmd: $target"

    case "$cmd" in
        *\ *)
            eval "$cmd" '"$target"'
            ;;
        *)
            $cmd "$target"
            ;;
    esac
}

cf "$@"