diff --git a/api.go b/api.go index ceef69de5bcc5fe993a91b414ef07579c59690e0..809b9e7760edb422167e13e7a6de5c8a76ed261a 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 eeaac847ca4f2c32101284b7e595c3db6cfe3459..e1db1e5b6f9992d77d079b82408e6a839482cdfb 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 93fb42aba5b0838499a6091a41df783577d2739c..99ef4b4f0f23846ded5139cc5e318686d604b065 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 25de8743ed6b37e0cad391c4832191bac750e217..cb7d68877c2d45c357557ccf42218c73a2ba3177 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 7adc224c7e263ce2850bed3838160c45e33e3d57..29b1c472cea22c72608c131dd02c6bd356fc9fb8 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 61649a1d3bd35beceaddfe98a2b63335ae6788f3..ef8db6106dc4b16669318a9b8b2f78c4acc99054 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 24a7da0d977a903d4cc71842e06ff4f44f3b5f6a..cfadfb754639a1ef1e32f70f32634e7612358d7b 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 8bdaa9cb98d73f38b1dd964b8552834d8feaf29e..6fa1e4c6b5c2bd9411643855a71a491e947ee0a5 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 c1e94f90fba6564a412b2808dc04e6fe3208e900..f282f9701a1bfd87c507f57de691b61ea92d70ae 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 ed49efa4a692160cbf4e9821595fd6284d88de75..9940ba4f39483bfd7cec5708550410ef150b8fb0 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 d66c2100702949c18ced0ef4491c802674426b78..b310b9ef143e5a00b217e1abc5e476193cebe5ce 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"