From 69be8312f8c210a8541a66e65dae30e97907789d Mon Sep 17 00:00:00 2001 From: GenZmeY Date: Tue, 3 Nov 2020 11:20:24 +0300 Subject: [PATCH] add C-style comment support --- Makefile | 34 ++++++++++++------- cmd/multini/actions.go | 4 +-- cmd/multini/args.go | 4 +-- cmd/multini/main.go | 4 +-- cmd/multini/reader.go | 10 +++--- cmd/multini/writer.go | 2 +- test/data/expected_ini/test_add_keyval.sh.ini | 2 +- .../test_add_keyval_existing.sh.ini | 2 +- .../data/expected_ini/test_add_section.sh.ini | 2 +- test/data/expected_ini/test_del_key.sh.ini | 2 +- .../expected_ini/test_del_key_multiple.sh.ini | 2 +- test/data/expected_ini/test_del_keyval.sh.ini | 2 +- .../data/expected_ini/test_del_section.sh.ini | 2 +- test/data/expected_ini/test_set_keyval.sh.ini | 2 +- test/data/in_ini/test_add_keyval.sh.ini | 2 +- .../in_ini/test_add_keyval_existing.sh.ini | 2 +- test/data/in_ini/test_add_section.sh.ini | 2 +- test/data/in_ini/test_del_key.sh.ini | 2 +- test/data/in_ini/test_del_key_multiple.sh.ini | 2 +- test/data/in_ini/test_del_keyval.sh.ini | 2 +- test/data/in_ini/test_del_section.sh.ini | 2 +- test/data/in_ini/test_get_key.sh.ini | 2 +- test/data/in_ini/test_get_key_multiple.sh.ini | 2 +- test/data/in_ini/test_get_section.sh.ini | 2 +- test/data/in_ini/test_get_section_list.sh.ini | 2 +- test/data/in_ini/test_set_keyval.sh.ini | 2 +- test/run_test.sh | 3 +- 27 files changed, 56 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index a44c991..455820e 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,30 @@ -NAME=multini -VERSION=0.2.3 -GOCMD=go -LDFLAGS:="$(LDFLAGS) -X 'main.Version=$(VERSION)'" -GOBUILD=$(GOCMD) build -ldflags=$(LDFLAGS) -SRCMAIN=. -BINDIR=bin -BIN=$(BINDIR)/$(NAME) -README=README -LICENSE=LICENSE -TEST=./run_test.sh -PREFIX=/usr +NAME = $(shell basename $(shell readlink -e .)) +VERSION = dev_$(shell date +%F_%T) +GOCMD = go +LDFLAGS := "$(LDFLAGS) -X 'main.Version=$(VERSION)'" +GOBUILD = $(GOCMD) build -ldflags=$(LDFLAGS) +SRCMAIN = ./cmd/$(NAME) +SRCDOC = ./doc +SRCTEST = ./test +BINDIR = bin +BIN = $(BINDIR)/$(NAME) +README = $(SRCDOC)/README +LICENSE = LICENSE +TEST = $(SRCTEST)/run_test.sh +PREFIX = /usr + +.PHONY: all prep doc build check-build freebsd-386 darwin-386 linux-386 windows-386 freebsd-amd64 darwin-amd64 linux-amd64 windows-amd64 compile install check-install uninstall clean test all: build prep: clean + go mod init; go mod tidy mkdir $(BINDIR) +doc: check-build + test -d $(SRCDOC) || mkdir $(SRCDOC) + $(BIN) --help > $(README) + build: prep $(GOBUILD) -o $(BIN) $(SRCMAIN) @@ -71,3 +80,4 @@ clean: test: check-build $(TEST) $(BIN) + diff --git a/cmd/multini/actions.go b/cmd/multini/actions.go index 1ce773a..5fd2097 100644 --- a/cmd/multini/actions.go +++ b/cmd/multini/actions.go @@ -1,8 +1,8 @@ package main import ( - "multini/output" - "multini/types" + "multini/internal/output" + "multini/internal/types" ) func chk() int { diff --git a/cmd/multini/args.go b/cmd/multini/args.go index 3f9d8a4..8e88e54 100644 --- a/cmd/multini/args.go +++ b/cmd/multini/args.go @@ -4,8 +4,8 @@ import ( "errors" "os" - "multini/output" - "multini/types" + "multini/internal/output" + "multini/internal/types" "github.com/juju/gnuflag" ) diff --git a/cmd/multini/main.go b/cmd/multini/main.go index a7db735..fd81a02 100644 --- a/cmd/multini/main.go +++ b/cmd/multini/main.go @@ -3,8 +3,8 @@ package main import ( "os" - "multini/output" - "multini/types" + "multini/internal/output" + "multini/internal/types" ) const ( diff --git a/cmd/multini/reader.go b/cmd/multini/reader.go index 7325b04..34f7464 100644 --- a/cmd/multini/reader.go +++ b/cmd/multini/reader.go @@ -7,8 +7,8 @@ import ( "regexp" "strings" - "multini/output" - "multini/types" + "multini/internal/output" + "multini/internal/types" ) var ( @@ -26,15 +26,15 @@ var ( RxBodyPrefix string = `(?P<` + NgPrefix + `>\s+)?` RxSectionName string = `\[(?P<` + NgSection + `>.+)\]` - RxKey string = `(?P<` + NgKey + `>(?:[^;#=]+[^\s=;#]|[^;#=]))?` + RxKey string = `(?P<` + NgKey + `>(?:[^;#/=]+[^\s=;#/]|[^;#/=]))?` RxKeyPostfix string = `(?P<` + NgKeyPostfix + `>\s+)?` RxValuePrefix string = `(?P<` + NgValuePrefix + `>\s+)?` - RxValue string = `(?P<` + NgValue + `>(?:[^;#]+[^\s;#]|[^;#]))?` + RxValue string = `(?P<` + NgValue + `>(?:[^;#/]+[^\s;#/]|[^;#/]))?` RxValuePostfix string = `(?P<` + NgValuePostfix + `>\s+)?` RxKeyVal string = RxKey + RxKeyPostfix + `=` + RxValuePrefix + RxValue + RxValuePostfix RxBody string = `(?:` + RxSectionName + `|` + RxKeyVal + `)?` RxBodyPostfix string = `(?P<` + NgPostifx + `>\s+)?` - RxCommentPrefix string = `(?P<` + NgCommentPrefix + `>[#;]\s*)` + RxCommentPrefix string = `(?P<` + NgCommentPrefix + `>([#;]|//)\s*)` RxCommentText string = `(?P<` + NgComment + `>.+)?` RxComment string = `(?:` + RxCommentPrefix + RxCommentText + `)?` Rx string = RxBodyPrefix + RxBody + RxBodyPostfix + RxComment diff --git a/cmd/multini/writer.go b/cmd/multini/writer.go index 07f8ee8..8fbe486 100644 --- a/cmd/multini/writer.go +++ b/cmd/multini/writer.go @@ -7,7 +7,7 @@ import ( "os" "path/filepath" - "multini/types" + "multini/internal/types" ) // Source: https://gist.github.com/var23rav/23ae5d0d4d830aff886c3c970b8f6c6b diff --git a/test/data/expected_ini/test_add_keyval.sh.ini b/test/data/expected_ini/test_add_keyval.sh.ini index 0394419..3b7486d 100644 --- a/test/data/expected_ini/test_add_keyval.sh.ini +++ b/test/data/expected_ini/test_add_keyval.sh.ini @@ -10,7 +10,7 @@ DefKey3=NoSpaces! Key2 = 2 Key3 = 3 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/expected_ini/test_add_keyval_existing.sh.ini b/test/data/expected_ini/test_add_keyval_existing.sh.ini index 1097509..205e731 100644 --- a/test/data/expected_ini/test_add_keyval_existing.sh.ini +++ b/test/data/expected_ini/test_add_keyval_existing.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/expected_ini/test_add_section.sh.ini b/test/data/expected_ini/test_add_section.sh.ini index 83eb42a..e61b3c1 100644 --- a/test/data/expected_ini/test_add_section.sh.ini +++ b/test/data/expected_ini/test_add_section.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/expected_ini/test_del_key.sh.ini b/test/data/expected_ini/test_del_key.sh.ini index 893fe31..1a529cd 100644 --- a/test/data/expected_ini/test_del_key.sh.ini +++ b/test/data/expected_ini/test_del_key.sh.ini @@ -8,7 +8,7 @@ DefKey3=NoSpaces! [SimpleSection] # Comment For Section Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/expected_ini/test_del_key_multiple.sh.ini b/test/data/expected_ini/test_del_key_multiple.sh.ini index 49b2155..83eeef5 100644 --- a/test/data/expected_ini/test_del_key_multiple.sh.ini +++ b/test/data/expected_ini/test_del_key_multiple.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment [SectionWithIndent] Key=Value diff --git a/test/data/expected_ini/test_del_keyval.sh.ini b/test/data/expected_ini/test_del_keyval.sh.ini index 6d56286..d4a9ac8 100644 --- a/test/data/expected_ini/test_del_keyval.sh.ini +++ b/test/data/expected_ini/test_del_keyval.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 3 diff --git a/test/data/expected_ini/test_del_section.sh.ini b/test/data/expected_ini/test_del_section.sh.ini index 6e8709a..b0c2164 100644 --- a/test/data/expected_ini/test_del_section.sh.ini +++ b/test/data/expected_ini/test_del_section.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/expected_ini/test_set_keyval.sh.ini b/test/data/expected_ini/test_set_keyval.sh.ini index dac4738..eb184f4 100644 --- a/test/data/expected_ini/test_set_keyval.sh.ini +++ b/test/data/expected_ini/test_set_keyval.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = onlyone [SectionWithIndent] diff --git a/test/data/in_ini/test_add_keyval.sh.ini b/test/data/in_ini/test_add_keyval.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_add_keyval.sh.ini +++ b/test/data/in_ini/test_add_keyval.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_add_keyval_existing.sh.ini b/test/data/in_ini/test_add_keyval_existing.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_add_keyval_existing.sh.ini +++ b/test/data/in_ini/test_add_keyval_existing.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_add_section.sh.ini b/test/data/in_ini/test_add_section.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_add_section.sh.ini +++ b/test/data/in_ini/test_add_section.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_del_key.sh.ini b/test/data/in_ini/test_del_key.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_del_key.sh.ini +++ b/test/data/in_ini/test_del_key.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_del_key_multiple.sh.ini b/test/data/in_ini/test_del_key_multiple.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_del_key_multiple.sh.ini +++ b/test/data/in_ini/test_del_key_multiple.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_del_keyval.sh.ini b/test/data/in_ini/test_del_keyval.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_del_keyval.sh.ini +++ b/test/data/in_ini/test_del_keyval.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_del_section.sh.ini b/test/data/in_ini/test_del_section.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_del_section.sh.ini +++ b/test/data/in_ini/test_del_section.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_get_key.sh.ini b/test/data/in_ini/test_get_key.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_get_key.sh.ini +++ b/test/data/in_ini/test_get_key.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_get_key_multiple.sh.ini b/test/data/in_ini/test_get_key_multiple.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_get_key_multiple.sh.ini +++ b/test/data/in_ini/test_get_key_multiple.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_get_section.sh.ini b/test/data/in_ini/test_get_section.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_get_section.sh.ini +++ b/test/data/in_ini/test_get_section.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_get_section_list.sh.ini b/test/data/in_ini/test_get_section_list.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_get_section_list.sh.ini +++ b/test/data/in_ini/test_get_section_list.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/data/in_ini/test_set_keyval.sh.ini b/test/data/in_ini/test_set_keyval.sh.ini index e96cbb7..05bac2e 100644 --- a/test/data/in_ini/test_set_keyval.sh.ini +++ b/test/data/in_ini/test_set_keyval.sh.ini @@ -9,7 +9,7 @@ DefKey3=NoSpaces! Key1 = 1 Key2 = 2 -[MultipleKeySection] +[MultipleKeySection] // C style comment Key = 1 Key = 2 Key = 3 diff --git a/test/run_test.sh b/test/run_test.sh index 096030c..f9446f5 100755 --- a/test/run_test.sh +++ b/test/run_test.sh @@ -1,10 +1,11 @@ #!/bin/bash DEF='\e[0m'; BLD='\e[1m'; RED='\e[31m'; GRN='\e[32m'; WHT='\e[97m' + ScriptFullname=$(readlink -e "$0") ScriptName=$(echo "$ScriptFullname" | awk -F '/' '{print $NF;}') ScriptDir=$(dirname "$ScriptFullname") -TestDir="$ScriptDir/tests" +TestDir="$ScriptDir/data" Multini=$(readlink -e "$1") if [[ -z "$Multini" ]]; then