X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=cedit;h=60bb4719aebf9729230b8b05a08ca8b30bb70667;hb=a6992c8fdacb3db1bd68b782a8f301f50629e620;hp=68efcae69398a0cb73e60134eaa9972c2579db0d;hpb=6cc921147f723f23586dd0b0b036e028e90c8e3d;p=cedit diff --git a/cedit b/cedit index 68efcae..60bb471 100755 --- a/cedit +++ b/cedit @@ -13,97 +13,113 @@ # See the License for the specific language governing permissions and # limitations under the License. -cedit() { # [-v] [section_name] FILE +cedit() { + local help="Usage: [-h|--help ] [-v] [SECTION_NAME] FILE +Create/modify a section in a config file - local s diff name init - local file_dir="$(dirname "$file")" - local exists=true - local verbose=false +Returns 1 if the file is modified by this command, 2 or higher +for other problems. - if [[ $1 == -v ]]; then - verbose=true - shift - fi - if (( $# == 2 )); then - name=": $1" - shift - fi +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. + +-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 file="$1" - local file_name="${file##*/}" + case $1 in + -b) backup=true; shift ;; + -v) verbose=true; shift ;; + -h|--help) echo "$help"; return ;; + esac - local begin="#_#_# start delimiter of cedit section$name. do not modify. #_#_#" - local end="#_#_# end delimiter of cedit section$name. do not modify. #_#_#" + if (( $# == 2 )); then + name=": $1" + shift + fi - if [[ ! -e $file_dir ]]; then - if ! mkdir -p $file_dir; then - s=sudo - $s mkdir -p $file_dir || return 1 - 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 - if [[ ! -e $file ]]; then - exists=false - if ! $s touch $file; then - s=sudo - $s touch $file || return 1 - fi + 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 - - - 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" - fi + [[ -w $file ]] || s=sudo - 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=0 - 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 - diff=$(cmp "$temp" "$file") - ret=$? - if $verbose; then - echo "$diff" - fi + 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" + 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 (( $ret )); then + echo "backup of original at $temp" + echo diff -u "$temp" "$file": + echo "$diff" + elif $verbose; then + echo "No changes made to $file" + fi + else + # for systems like openwrt which don't have diff + diff=$(cmp "$temp" "$file") + ret=$? + if $verbose; then + echo "$diff" fi - return $ret + fi + if ! $backup; then + rm -r $"$temp" + fi + return $ret } cedit "$@"