git-committerconfig/pkg/config/config.go
2024-06-24 15:02:06 +02:00

57 lines
1.2 KiB
Go

// Package config defines the format of the configuration file
package config
import (
"fmt"
"os"
"regexp"
"gopkg.in/yaml.v3"
)
type (
// Config defines each entry for the File matching a remote and
// containing a configuration to apply for the repo
Config struct {
Match string `yaml:"match"`
EMail string `yaml:"email"`
Keyname string `json:"keyname"`
SigningKey string `json:"signingkey"`
CommitOpts string `json:"commit-opts"`
}
// File holds a list of Config entries
File []Config
)
// Load reads the configuration file and parses it
func Load(fn string) (f File, err error) {
fh, err := os.Open(fn) //#nosec:G304 // Intended
if err != nil {
return f, fmt.Errorf("opening config: %w", err)
}
defer fh.Close() //nolint:errcheck // Will be closed by tool exec
if err = yaml.NewDecoder(fh).Decode(&f); err != nil {
return f, fmt.Errorf("decoding config: %w", err)
}
return f, nil
}
// ConfigForRemote returns the first Config matching the given remote
// or nil if none matched
func (f File) ConfigForRemote(remote string) *Config {
for i := range f {
conf := f[i]
if !regexp.MustCompile(conf.Match).MatchString(remote) {
continue
}
return &conf
}
return nil
}