From 454e209744c83548613f3b8680d74234965e0b14 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Mon, 17 Oct 2022 11:44:13 +0200 Subject: [PATCH] feat add the posibility to recognise the filetype, add HasFile and RemoveFile --- api.go | 2 ++ api_test.go | 2 +- env_test.go | 2 +- error.go | 6 +++++ file.go | 56 ++++++++++++++++++++++++++++++++++++++++++++-- file_test.go | 50 ++++++++++++++++++++++++++++++++++++++++- flags_test.go | 2 +- format.go | 3 ++- import_test.go | 2 +- properties_test.go | 2 +- watch_test.go | 2 +- 11 files changed, 119 insertions(+), 10 deletions(-) diff --git a/api.go b/api.go index ceef69d..809b9e7 100644 --- a/api.go +++ b/api.go @@ -41,6 +41,8 @@ func (s *Settings[C]) SetMnemonic(mnemonic string) *Settings[C] { } // Config() returns the configuration +// Remember that the configuration is a copy of the original configuration. +// Changes to the configuration will not be reflected in the original configuration. func (s *Settings[C]) Config() C { return s.config } diff --git a/api_test.go b/api_test.go index eeaac84..e1db1e5 100644 --- a/api_test.go +++ b/api_test.go @@ -4,7 +4,7 @@ package configuration import ( - "github.com/magiconair/properties/assert" + "github.com/stretchr/testify/assert" "testing" ) diff --git a/env_test.go b/env_test.go index 93fb42a..99ef4b4 100644 --- a/env_test.go +++ b/env_test.go @@ -4,7 +4,7 @@ package configuration import ( - "github.com/magiconair/properties/assert" + "github.com/stretchr/testify/assert" "os" "testing" ) diff --git a/error.go b/error.go index 25de874..cb7d688 100644 --- a/error.go +++ b/error.go @@ -89,3 +89,9 @@ type MismatchedTypeError error func newMismatchedTypeError(expected, actual reflect.Type) MismatchedTypeError { return MismatchedTypeError(errors.New("expected type " + expected.String() + " but got " + actual.String())) } + +type UnknownFileExtensionError error + +func newUnknownFileExtensionError(extension, filename string) UnknownFileExtensionError { + return UnknownFileExtensionError(errors.New("unknown file extension " + extension + " for file " + filename)) +} diff --git a/file.go b/file.go index 7adc224..29b1c47 100644 --- a/file.go +++ b/file.go @@ -8,6 +8,7 @@ import ( "io/fs" "os" "path" + "path/filepath" ) type files struct { @@ -23,9 +24,60 @@ type fileBackend struct { fs fs.FS } +func (s *Settings[c]) HasFile(file string) bool { + for _, f := range s.files.files { + if f.path == file { + return true + } + } + return false +} + // AddFiles adds a file to the list of files to import -func (s *Settings[C]) AddFile(file string, format Format) *Settings[C] { - s.files.files = append(s.files.files, files{file, format}) +func (s *Settings[C]) AddFile(file string, format ...Format) *Settings[C] { + + var f Format + f = RecogniseFormat + + if len(format) <= 1 { + f = format[0] + } else { + panic("too many arguments") + } + + if f == RecogniseFormat { + ext := filepath.Ext(file) + switch ext { + case ".yaml": + f = Yaml + case ".json": + f = Json + case ".toml": + f = Toml + case ".properties": + f = Properties + default: + if ext != "" { + s.errors = append(s.errors, newUnknownFileExtensionError(ext, file)) + return s + } + f = s.files.format + } + } + + s.files.files = append(s.files.files, files{file, f}) + return s +} + +func (s *Settings[C]) RemoveFile(file string) *Settings[C] { + + for i, f := range s.files.files { + if f.path == file { + s.files.files = append(s.files.files[:i], s.files.files[i+1:]...) + return s + } + } + return s } diff --git a/file_test.go b/file_test.go index 61649a1..ef8db61 100644 --- a/file_test.go +++ b/file_test.go @@ -5,7 +5,7 @@ package configuration import ( "bytes" - "github.com/magiconair/properties/assert" + "github.com/stretchr/testify/assert" "io" "io/fs" "os" @@ -510,6 +510,54 @@ func TestSettingAddFile(t *testing.T) { } +func TestRemoveFile(t *testing.T) { + cfg := ConfigStruct2{} + + f, err := os.CreateTemp("", "watch_test") + if err != nil { + t.Error(err) + return + } + + defer os.Remove(f.Name()) + + f.WriteString("A: \"from file\"") + + c := New(cfg) + c.AddFile(f.Name(), Yaml) + assert.Equal(t, c.Config().A, "") + + c.Import() + assert.Equal(t, c.Config().A, "from file") + + assert.True(t, c.HasFile(f.Name())) + c.RemoveFile(f.Name()) + assert.False(t, c.HasFile(f.Name())) + +} + +func TestSettingAddFileWithRecognise(t *testing.T) { + cfg := ConfigStruct2{} + + f, err := os.CreateTemp("", "watch_test") + if err != nil { + t.Error(err) + return + } + + defer os.Remove(f.Name()) + f.WriteString("A: \"from file\"") + + c := New(cfg) + c.AddFile(f.Name(), RecogniseFormat) + + assert.Equal(t, c.Config().A, "") + + c.Import() + assert.Equal(t, c.Config().A, "from file") + +} + func TestSetConfigName(t *testing.T) { cfg := ConfigStruct2{} diff --git a/flags_test.go b/flags_test.go index 24a7da0..cfadfb7 100644 --- a/flags_test.go +++ b/flags_test.go @@ -8,7 +8,7 @@ import ( "os" "testing" - "github.com/magiconair/properties/assert" + "github.com/stretchr/testify/assert" ) // Example for README diff --git a/format.go b/format.go index 8bdaa9c..6fa1e4c 100644 --- a/format.go +++ b/format.go @@ -6,7 +6,8 @@ package configuration type Format int const ( - Yaml Format = iota + RecogniseFormat = -1 + Yaml Format = iota Json Toml Properties diff --git a/import_test.go b/import_test.go index c1e94f9..f282f97 100644 --- a/import_test.go +++ b/import_test.go @@ -4,7 +4,7 @@ package configuration import ( - "github.com/magiconair/properties/assert" + "github.com/stretchr/testify/assert" "testing" ) diff --git a/properties_test.go b/properties_test.go index ed49efa..9940ba4 100644 --- a/properties_test.go +++ b/properties_test.go @@ -4,7 +4,7 @@ package configuration import ( - "github.com/magiconair/properties/assert" + "github.com/stretchr/testify/assert" "testing" ) diff --git a/watch_test.go b/watch_test.go index d66c210..b310b9e 100644 --- a/watch_test.go +++ b/watch_test.go @@ -4,7 +4,7 @@ package configuration import ( - "github.com/magiconair/properties/assert" + "github.com/stretchr/testify/assert" "os" "testing" "time" -- GitLab