diff options
| author | Runxi Yu <me@runxiyu.org> | 2025-11-12 00:31:04 +0800 |
|---|---|---|
| committer | Runxi Yu <me@runxiyu.org> | 2025-11-12 00:31:04 +0800 |
| commit | db03e1a2a84b5edfd95ce190c849eb5afa5a464e (patch) | |
| tree | df2bc2b2445cc7fb684575b0d5ca258960db430b /forged/internal | |
| parent | Refactor (diff) | |
| download | forge-master.tar.gz forge-master.tar.zst forge-master.zip | |
Diffstat (limited to '')
| -rw-r--r-- | forged/internal/common/scfg/reader.go | 3 | ||||
| -rw-r--r-- | forged/internal/common/scfg/scfg.go | 7 | ||||
| -rw-r--r-- | forged/internal/common/scfg/struct.go | 17 | ||||
| -rw-r--r-- | forged/internal/common/scfg/unmarshal.go | 16 | ||||
| -rw-r--r-- | forged/internal/common/scfg/writer.go | 5 | ||||
| -rw-r--r-- | forged/internal/config/config.go | 94 |
6 files changed, 73 insertions, 69 deletions
diff --git a/forged/internal/common/scfg/reader.go b/forged/internal/common/scfg/reader.go index b0e2cc0..3ec45e4 100644 --- a/forged/internal/common/scfg/reader.go +++ b/forged/internal/common/scfg/reader.go @@ -1,7 +1,8 @@ // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (c) 2020 Simon Ser <https://emersion.fr> +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> -package scfg +package scfgs import ( "bufio" diff --git a/forged/internal/common/scfg/scfg.go b/forged/internal/common/scfg/scfg.go index 4533e63..3128b1c 100644 --- a/forged/internal/common/scfg/scfg.go +++ b/forged/internal/common/scfg/scfg.go @@ -1,9 +1,10 @@ // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (c) 2020 Simon Ser <https://emersion.fr> +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> -// Package scfg parses and formats configuration files. -// Note that this fork of scfg behaves differently from upstream scfg. -package scfg +// Package scfgs parses and formats configuration files. +// Note that this fork of scfgs behaves differently from upstream scfg. +package scfgs import ( "fmt" diff --git a/forged/internal/common/scfg/struct.go b/forged/internal/common/scfg/struct.go index 98ec943..c222385 100644 --- a/forged/internal/common/scfg/struct.go +++ b/forged/internal/common/scfg/struct.go @@ -1,7 +1,8 @@ // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (c) 2020 Simon Ser <https://emersion.fr> +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> -package scfg +package scfgs import ( "fmt" @@ -10,7 +11,7 @@ import ( "sync" ) -// structInfo contains scfg metadata for structs. +// structInfo contains scfgs metadata for structs. type structInfo struct { param int // index of field storing parameters children map[string]int // indices of fields storing child directives @@ -37,12 +38,12 @@ func getStructInfo(t reflect.Type) (*structInfo, error) { for i := 0; i < t.NumField(); i++ { f := t.Field(i) if f.Anonymous { - return nil, fmt.Errorf("scfg: anonymous struct fields are not supported") + return nil, fmt.Errorf("scfgs: anonymous struct fields are not supported") } else if !f.IsExported() { continue } - tag := f.Tag.Get("scfg") + tag := f.Tag.Get("scfgs") parts := strings.Split(tag, ",") k, options := parts[0], parts[1:] if k == "-" { @@ -57,21 +58,21 @@ func getStructInfo(t reflect.Type) (*structInfo, error) { case "param": isParam = true default: - return nil, fmt.Errorf("scfg: invalid option %q in struct tag", opt) + return nil, fmt.Errorf("scfgs: invalid option %q in struct tag", opt) } } if isParam { if info.param >= 0 { - return nil, fmt.Errorf("scfg: param option specified multiple times in struct tag in %v", t) + return nil, fmt.Errorf("scfgs: param option specified multiple times in struct tag in %v", t) } if parts[0] != "" { - return nil, fmt.Errorf("scfg: name must be empty when param option is specified in struct tag in %v", t) + return nil, fmt.Errorf("scfgs: name must be empty when param option is specified in struct tag in %v", t) } info.param = i } else { if _, ok := info.children[k]; ok { - return nil, fmt.Errorf("scfg: key %q specified multiple times in struct tag in %v", k, t) + return nil, fmt.Errorf("scfgs: key %q specified multiple times in struct tag in %v", k, t) } info.children[k] = i } diff --git a/forged/internal/common/scfg/unmarshal.go b/forged/internal/common/scfg/unmarshal.go index 8befc10..e60bcc7 100644 --- a/forged/internal/common/scfg/unmarshal.go +++ b/forged/internal/common/scfg/unmarshal.go @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2020 Simon Ser <https://emersion.fr> // SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> -package scfg +package scfgs import ( "encoding" @@ -12,7 +12,7 @@ import ( "strconv" ) -// Decoder reads and decodes an scfg document from an input stream. +// Decoder reads and decodes an scfgs document from an input stream. type Decoder struct { r io.Reader unknownDirectives []*Directive @@ -29,7 +29,7 @@ func (dec *Decoder) UnknownDirectives() []*Directive { return dec.unknownDirectives } -// Decode reads scfg document from the input and stores it in the value pointed +// Decode reads scfgs document from the input and stores it in the value pointed // to by v. // // If v is nil or not a pointer, Decode returns an error. @@ -60,7 +60,7 @@ func (dec *Decoder) UnknownDirectives() []*Directive { // unmarshaled into the value. Children blocks are not allowed. // // The decoding of each struct field can be customized by the format string -// stored under the "scfg" key in the struct field's tag. The tag contains the +// stored under the "scfgs" key in the struct field's tag. The tag contains the // name of the field possibly followed by a comma-separated list of options. // The name may be empty in order to specify options without overriding the // default field name. As a special case, if the field name is "-", the field @@ -74,7 +74,7 @@ func (dec *Decoder) Decode(v interface{}) error { rv := reflect.ValueOf(v) if rv.Kind() != reflect.Ptr || rv.IsNil() { - return fmt.Errorf("scfg: invalid value for unmarshaling") + return fmt.Errorf("scfgs: invalid value for unmarshaling") } return dec.unmarshalBlock(block, rv) @@ -92,7 +92,7 @@ func (dec *Decoder) unmarshalBlock(block Block, v reflect.Value) error { switch v.Kind() { case reflect.Map: if t.Key().Kind() != reflect.String { - return fmt.Errorf("scfg: map key type must be string") + return fmt.Errorf("scfgs: map key type must be string") } if v.IsNil() { v.Set(reflect.MakeMap(t)) @@ -134,12 +134,12 @@ func (dec *Decoder) unmarshalBlock(block Block, v reflect.Value) error { continue } if _, ok := seen[fieldIndex]; !ok { - return fmt.Errorf("scfg: missing required directive %q", name) + return fmt.Errorf("scfgs: missing required directive %q", name) } } default: - return fmt.Errorf("scfg: unsupported type for unmarshaling blocks: %v", t) + return fmt.Errorf("scfgs: unsupported type for unmarshaling blocks: %v", t) } return nil diff --git a/forged/internal/common/scfg/writer.go b/forged/internal/common/scfg/writer.go index 02a07fe..979d71f 100644 --- a/forged/internal/common/scfg/writer.go +++ b/forged/internal/common/scfg/writer.go @@ -1,7 +1,8 @@ // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (c) 2020 Simon Ser <https://emersion.fr> +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> -package scfg +package scfgs import ( "errors" @@ -9,7 +10,7 @@ import ( "strings" ) -var errDirEmptyName = errors.New("scfg: directive with empty name") +var errDirEmptyName = errors.New("scfgs: directive with empty name") // Write writes a parsed configuration to the provided io.Writer. func Write(w io.Writer, blk Block) error { diff --git a/forged/internal/config/config.go b/forged/internal/config/config.go index 1825882..414a811 100644 --- a/forged/internal/config/config.go +++ b/forged/internal/config/config.go @@ -10,79 +10,79 @@ import ( ) type Config struct { - DB DB `scfg:"db"` - Web Web `scfg:"web"` - Hooks Hooks `scfg:"hooks"` - LMTP LMTP `scfg:"lmtp"` - SSH SSH `scfg:"ssh"` - IRC IRC `scfg:"irc"` - Git Git `scfg:"git"` - General General `scfg:"general"` - Pprof Pprof `scfg:"pprof"` + DB DB `scfgs:"db"` + Web Web `scfgs:"web"` + Hooks Hooks `scfgs:"hooks"` + LMTP LMTP `scfgs:"lmtp"` + SSH SSH `scfgs:"ssh"` + IRC IRC `scfgs:"irc"` + Git Git `scfgs:"git"` + General General `scfgs:"general"` + Pprof Pprof `scfgs:"pprof"` } type DB struct { - Conn string `scfg:"conn"` + Conn string `scfgs:"conn"` } type Web struct { - Net string `scfg:"net"` - Addr string `scfg:"addr"` - Root string `scfg:"root"` - CookieExpiry int `scfg:"cookie_expiry"` - ReadTimeout uint32 `scfg:"read_timeout"` - WriteTimeout uint32 `scfg:"write_timeout"` - IdleTimeout uint32 `scfg:"idle_timeout"` - MaxHeaderBytes int `scfg:"max_header_bytes"` - ReverseProxy bool `scfg:"reverse_proxy"` - ShutdownTimeout uint32 `scfg:"shutdown_timeout"` - TemplatesPath string `scfg:"templates_path"` - StaticPath string `scfg:"static_path"` + Net string `scfgs:"net"` + Addr string `scfgs:"addr"` + Root string `scfgs:"root"` + CookieExpiry int `scfgs:"cookie_expiry"` + ReadTimeout uint32 `scfgs:"read_timeout"` + WriteTimeout uint32 `scfgs:"write_timeout"` + IdleTimeout uint32 `scfgs:"idle_timeout"` + MaxHeaderBytes int `scfgs:"max_header_bytes"` + ReverseProxy bool `scfgs:"reverse_proxy"` + ShutdownTimeout uint32 `scfgs:"shutdown_timeout"` + TemplatesPath string `scfgs:"templates_path"` + StaticPath string `scfgs:"static_path"` } type Hooks struct { - Socket string `scfg:"socket"` - Execs string `scfg:"execs"` + Socket string `scfgs:"socket"` + Execs string `scfgs:"execs"` } type LMTP struct { - Socket string `scfg:"socket"` - Domain string `scfg:"domain"` - MaxSize int64 `scfg:"max_size"` - WriteTimeout uint32 `scfg:"write_timeout"` - ReadTimeout uint32 `scfg:"read_timeout"` + Socket string `scfgs:"socket"` + Domain string `scfgs:"domain"` + MaxSize int64 `scfgs:"max_size"` + WriteTimeout uint32 `scfgs:"write_timeout"` + ReadTimeout uint32 `scfgs:"read_timeout"` } type SSH struct { - Net string `scfg:"net"` - Addr string `scfg:"addr"` - Key string `scfg:"key"` - Root string `scfg:"root"` - ShutdownTimeout uint32 `scfg:"shutdown_timeout"` + Net string `scfgs:"net"` + Addr string `scfgs:"addr"` + Key string `scfgs:"key"` + Root string `scfgs:"root"` + ShutdownTimeout uint32 `scfgs:"shutdown_timeout"` } type IRC struct { - Net string `scfg:"net"` - Addr string `scfg:"addr"` - TLS bool `scfg:"tls"` - SendQ uint `scfg:"sendq"` - Nick string `scfg:"nick"` - User string `scfg:"user"` - Gecos string `scfg:"gecos"` + Net string `scfgs:"net"` + Addr string `scfgs:"addr"` + TLS bool `scfgs:"tls"` + SendQ uint `scfgs:"sendq"` + Nick string `scfgs:"nick"` + User string `scfgs:"user"` + Gecos string `scfgs:"gecos"` } type Git struct { - RepoDir string `scfg:"repo_dir"` - Socket string `scfg:"socket"` + RepoDir string `scfgs:"repo_dir"` + Socket string `scfgs:"socket"` } type General struct { - Title string `scfg:"title"` + Title string `scfgs:"title"` } type Pprof struct { - Net string `scfg:"net"` - Addr string `scfg:"addr"` + Net string `scfgs:"net"` + Addr string `scfgs:"addr"` } func Open(path string) (config Config, err error) { @@ -97,7 +97,7 @@ func Open(path string) (config Config, err error) { _ = configFile.Close() }() - decoder := scfg.NewDecoder(bufio.NewReader(configFile)) + decoder := scfgs.NewDecoder(bufio.NewReader(configFile)) err = decoder.Decode(&config) if err != nil { err = fmt.Errorf("decode config file: %w", err) |
