From 4eb3a9d2a74d75005c970b3b913099b02b2dd32e Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Thu, 19 Sep 2024 17:56:17 +0000 Subject: [PATCH] add system config type --- internal/config/system.go | 81 ++++++++++++++++++++++++++++++++++ internal/config/system_test.go | 20 +++++++++ 2 files changed, 101 insertions(+) create mode 100644 internal/config/system.go create mode 100644 internal/config/system_test.go diff --git a/internal/config/system.go b/internal/config/system.go new file mode 100644 index 0000000..90e33cc --- /dev/null +++ b/internal/config/system.go @@ -0,0 +1,81 @@ +// Copyright 2024 Matthew Rich . All rights reserved. + +package config + +import ( + "context" + "encoding/json" + "net/url" + "fmt" + "decl/internal/data" + "decl/internal/folio" + "runtime" +) + +// Collects facts about the system + +var ( + buildValues = map[string]any{ + "GOOS": runtime.GOOS, + "GOARCH": runtime.GOARCH, + } +) + +func init() { + folio.DocumentRegistry.ConfigurationTypes.Register([]string{"system"}, func(u *url.URL) data.Configuration { + s := NewSystem() + return s + }) +} + +type System Generic[any] + +func NewSystem() *System { + s := make(System) + for k, v := range buildValues { + s[k] = v + } + return &s +} + +func (s *System) URI() string { + return fmt.Sprintf("%s://%s", s.Type(), "") +} + +func (s *System) SetURI(uri string) error { + return nil +} + +func (s *System) SetParsedURI(uri *url.URL) error { + return nil +} + +func (s *System) Clone() data.Configuration { + jsonSystem, _ := json.Marshal(s) + clone := NewSystem() + if unmarshalErr := json.Unmarshal(jsonSystem, clone); unmarshalErr != nil { + panic(unmarshalErr) + } + return clone +} + +func (s *System) Has(key string) (ok bool) { + _, ok = (*s)[key] + return +} + +func (s *System) Type() string { + return "system" +} + +func (s *System) Read(context.Context) ([]byte, error) { + return nil, nil +} + +func (s *System) GetValue(name string) (result any, err error) { + var ok bool + if result, ok = (*s)[name]; !ok { + err = ErrUnknownConfigurationKey + } + return +} diff --git a/internal/config/system_test.go b/internal/config/system_test.go new file mode 100644 index 0000000..54b0f25 --- /dev/null +++ b/internal/config/system_test.go @@ -0,0 +1,20 @@ +// Copyright 2024 Matthew Rich . All rights reserved. + +package config + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewSystemConfig(t *testing.T) { + s := NewSystem() + assert.NotNil(t, s) +} + +func TestSystemConfig(t *testing.T) { + s := NewSystem() + assert.NotNil(t, s) + + assert.True(t, s.Has("GOARCH")) +}