From: Ian Kelling Date: Wed, 24 Apr 2024 19:34:05 +0000 (-0400) Subject: minor docs update X-Git-Url: https://iankelling.org/git/?p=cedit;a=commitdiff_plain;h=HEAD;hp=9ee05f569ce5da96047000722dbde36f689d826d minor docs update --- diff --git a/README b/README index 279bfe3..0287269 100644 --- a/README +++ b/README @@ -1,3 +1,15 @@ +# The following is the GNU All-permissive License as recommended in +# + +# Copyright (C) 2024 Ian Kelling + +# Copying and distribution of this file, with or without modification, +# are permitted in any medium without royalty provided the copyright +# notice and this notice are preserved. This file is offered as-is, +# without any warranty. + +Create/modify a comment-delimited section in a config file + The main documentation is availiable via --help and near the top of the bash script file next to this file. diff --git a/cedit b/cedit index e6d128d..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. @@ -14,8 +21,8 @@ # limitations under the License. cedit() { - local help="Usage: [-h|--help ] [-v] [SECTION_NAME] FILE -Create/modify a section in a config file + local help="Usage: [-h|--help ] [-v] [SECTION_NAME] 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. @@ -24,98 +31,135 @@ The section is #comment delimited. Reads STDIN for the contents of the 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 - - case $1 in - -v) verbose=true; shift ;; - -h|--help) echo "$help"; return ;; - esac - - if (( $# == 2 )); then - name=": $1" - shift + 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 + + if (( $# == 2 )); then + name=": $1" + shift + fi + + local file="$1" + local file_name="${file##*/}" + + 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 + s=sudo + $s mkdir -p $file_dir || return 2 fi - - 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. #_#_#" - - if [[ ! -e $file_dir ]]; then - if ! mkdir -p $file_dir; then - s=sudo - $s mkdir -p $file_dir || return 2 - fi + fi + if [[ ! -e $file ]]; then + exists=false + if ! $s touch $file; then + s=sudo + $s touch $file || return 2 fi - if [[ ! -e $file ]]; then - exists=false - if ! $s touch $file; then - s=sudo - $s touch $file || return 2 - fi + fi + + [[ -w $file ]] || s=sudo + + + local in_section=false + if $exists; then + local tailn=1 + local temp="$(mktemp -d)/$file_name" + cp "$file" "$temp" + cp /dev/null "$file" + while IFS= read -r line; do + tailn=$(( tailn + 1 )) + if [[ $line == "$begin" ]]; then + in_section=true; + break + fi + printf '%s\n' "$line" >> $file + done < "$temp" + fi + + IFS= read -d '' -n 1 -r init + if [[ $init ]]; then + $s tee -a "$file" >/dev/null <<<"$begin" + printf '%s' "$init" | $s tee -a "$file" >/dev/null + $s tee -a "$file" >/dev/null + $s tee -a "$file" >/dev/null <<<"$end" + fi + + if $exists && $in_section; then + while IFS= read -r line; do + if [[ $line == "$begin" ]]; then + in_section=true; + fi + if ! $in_section; then + printf '%s\n' "$line" >> $file + fi + if [[ $line == $end ]]; then + in_section=false; + fi + done < <(tail -n +$tailn "$temp") + fi + + + if ! $exists; then + ret=1 + if $verbose; then + echo "New file $file:" + cat "$file" fi - - [[ -w $file ]] || s=sudo - - - if $exists; then - local temp="$(mktemp -d)/$file_name" - cp "$file" "$temp" - cp /dev/null "$file" - local in_section=false - while IFS= read -r line; do - if [[ $line == $begin ]]; then - in_section=true; - fi - if ! $in_section; then - printf '%s\n' "$line" >> $file - fi - if [[ $line == $end ]]; then - in_section=false; - fi - done < "$temp" + elif type -t diff &>/dev/null; then + diff=$(diff -u "$temp" "$file") + ret=$? + if (( $ret )) && ! $quiet; then + echo "backup of original at $temp" + echo diff -u "$temp" "$file": + echo "$diff" + #elif $debug; then + # echo "No changes made to $file" fi - - IFS= read -d '' -n 1 -r init - if [[ $init ]]; then - $s tee -a "$file" >/dev/null <<<"$begin" - printf '%s' "$init" | $s tee -a "$file" >/dev/null - $s tee -a "$file" >/dev/null - $s tee -a "$file" >/dev/null <<<"$end" - fi - - if ! $exists; then - ret=1 - if $verbose; then - echo "New file $file:" - cat "$file" - fi - elif type -t diff &>/dev/null; then - diff=$(diff -u "$temp" "$file") - ret=$? - if $verbose; then - if (( $ret == 0 )); then - echo "No changes made to $file" - fi - else - echo "backup of original at $temp" - echo diff -u "$temp" "$file": - echo "$diff" - fi - else - # for systems like openwrt which don't have diff - diff=$(cmp "$temp" "$file") - ret=$? - if $verbose; then - echo "$diff" - fi + else + # for systems like openwrt which don't have diff + diff=$(cmp "$temp" "$file") + ret=$? + if $verbose; then + echo "$diff" fi + fi + if ! $backup && $exists; then + rm -r "$temp" + fi + if $silent; then + case $ret in + 0|1) return 0 ;; + *) return $ret ;; + esac + else return $ret + fi } cedit "$@" diff --git a/test/test b/test/test new file mode 100755 index 0000000..63c2f65 --- /dev/null +++ b/test/test @@ -0,0 +1,60 @@ +#!/bin/bash +# 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. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +cd $(mktemp -d) + +cat >f <<'EOF' +this +little +EOF + + +cedit -v f <<'EOF' +piggy +went +to +market +EOF + + +cedit -v sec2 f <<'EOF' +another little piggy +stayed home +EOF + +cat >>f <<'EOF' +wee wee wee +EOF + +cedit -v f <<'EOF' +piggy +went +to +market +EOF + + +cedit -v sec2 f <<'EOF' +another little piggy +stayed home +EOF