X-Git-Url: https://iankelling.org/git/?p=cedit;a=blobdiff_plain;f=cedit;h=1cfa5803c08cdb13945b8ae81fa882f747bcfc89;hp=62331b48c85808fefb44d1866cfec7218484784c;hb=HEAD;hpb=97f167fc565b8393b38196d2c29a870dd911cac5 diff --git a/cedit b/cedit index 62331b4..e47afa5 100755 --- a/cedit +++ b/cedit @@ -1,5 +1,12 @@ #!/bin/bash -# Copyright (C) 2016 Ian Kelling +# I, Ian Kelling, follow the GNU license recommendations at +# https://www.gnu.org/licenses/license-recommendations.en.html. They +# recommend that small programs, < 300 lines, be licensed under the +# Apache License 2.0. This file contains or is part of one or more small +# programs. If a small program grows beyond 300 lines, I plan to switch +# its license to GPL. + +# Copyright 2024 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,7 +22,7 @@ cedit() { local help="Usage: [-h|--help ] [-v] [SECTION_NAME] FILE -Create/modify a section in a config file +Create/modify a comment-delimited section in a config file Returns 1 if the file is modified by this command, 2 or higher for other problems. @@ -25,20 +32,23 @@ section. Without SECTION_NAME, it acts on a global unnamed section. cedit is short for config edit. -q Quiet +-s Silent. Quiet and exit 0 on modified file. -v Verbose -b Keep backup file -h|--help Help" - local s diff name init - local file_dir="$(dirname "$file")" - local exists=true - local verbose=false - local backup=false - local quiet=false + local s diff name init file_dir exists verbose backup quiet silent + file_dir="$(dirname "$file")" + exists=true + verbose=false + backup=false + quiet=false + silent=false case $1 in -b) backup=true; shift ;; -v) verbose=true; shift ;; -q) quiet=true; shift ;; + -s) quiet=true; silent=true; shift ;; -h|--help) echo "$help"; return ;; esac @@ -50,8 +60,15 @@ section. cedit is short for config edit. local file="$1" local file_name="${file##*/}" - local begin="#_#_# start delimiter of cedit section$name. do not modify. #_#_#" - local end="#_#_# end delimiter of cedit section$name. do not modify. #_#_#" + local comment + comment="#_#_#" + + # bind zone files use ; for comments yes, a little hacky detection. + if [[ $file_name == db.* ]]; then + comment=";;_;_;" + fi + local begin="$comment start delimiter of cedit section$name. do not modify. $comment" + local end="$comment end delimiter of cedit section$name. do not modify. $comment" if [[ ! -e $file_dir ]]; then if ! mkdir -p $file_dir; then @@ -136,6 +153,13 @@ section. cedit is short for config edit. if ! $backup && $exists; then rm -r "$temp" fi - return $ret + if $silent; then + case $ret in + 0|1) return 0 ;; + *) return $ret ;; + esac + else + return $ret + fi } cedit "$@"