jx/internal/config/system.go
Matthew Rich 8feb7b8d56
Some checks failed
Lint / golangci-lint (push) Failing after 10m1s
Declarative Tests / test (push) Failing after 14s
add support of import search paths [doublejynx/jx#7]
2024-10-16 10:26:42 -07:00

98 lines
1.8 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package config
import (
"context"
"encoding/json"
"net/url"
"fmt"
"decl/internal/data"
"decl/internal/folio"
"runtime"
"decl/internal/system"
)
// 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
}
s.CurrentUser()
s["importpath"] = []string {
"/etc/jx/lib",
}
return &s
}
func (s *System) CurrentUser() {
processUser := system.ProcessUser()
processGroup := system.ProcessGroup(processUser)
(*s)["user"] = processUser.Username
(*s)["gecos"] = processUser.Name
(*s)["home"] = processUser.HomeDir
(*s)["uid"] = processUser.Uid
(*s)["group"] = processGroup.Name
(*s)["gid"] = processUser.Gid
}
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 data.URIParser) 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 = data.ErrUnknownConfigurationKey
}
return
}