102 lines
2.3 KiB
Go
102 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"regexp"
|
|
|
|
"git.luzifer.io/luzifer/git-committerconfig/pkg/config"
|
|
"git.luzifer.io/luzifer/git-committerconfig/pkg/git"
|
|
"github.com/mitchellh/go-homedir"
|
|
"github.com/pkg/errors"
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/Luzifer/rconfig/v2"
|
|
)
|
|
|
|
var (
|
|
cfg = struct {
|
|
Config string `flag:"config" default:"~/.config/git-committerconfig.yaml" description:"File containing the configuration"`
|
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
|
}{}
|
|
|
|
sshKeyMatch = regexp.MustCompile(`^(?:ssh|ecdsa|key::)`)
|
|
|
|
version = "dev"
|
|
)
|
|
|
|
func initApp() error {
|
|
rconfig.AutoEnv(true)
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
|
return errors.Wrap(err, "parsing cli options")
|
|
}
|
|
|
|
l, err := logrus.ParseLevel(cfg.LogLevel)
|
|
if err != nil {
|
|
return errors.Wrap(err, "parsing log-level")
|
|
}
|
|
logrus.SetLevel(l)
|
|
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
var err error
|
|
if err = initApp(); err != nil {
|
|
logrus.WithError(err).Fatal("initializing app")
|
|
}
|
|
|
|
if cfg.VersionAndExit {
|
|
logrus.WithField("version", version).Info("git-committerconfig")
|
|
os.Exit(0)
|
|
}
|
|
|
|
confPath, err := homedir.Expand(cfg.Config)
|
|
if err != nil {
|
|
logrus.WithError(err).Fatal("expanding config path")
|
|
}
|
|
|
|
conf, err := config.Load(confPath)
|
|
if err != nil {
|
|
logrus.WithError(err).Fatal("loading config")
|
|
}
|
|
|
|
remote, err := git.GetPushRemote()
|
|
if err != nil {
|
|
if errors.Is(err, git.ErrRemoteNotFound) {
|
|
logrus.Debug("no remote found, not applying config")
|
|
return
|
|
}
|
|
|
|
logrus.WithError(err).Fatal("getting push-remote")
|
|
}
|
|
|
|
c := conf.ConfigForRemote(remote)
|
|
if c == nil {
|
|
logrus.Debug("no config for remote found, not applying config")
|
|
return
|
|
}
|
|
|
|
settings := map[string]string{
|
|
"user.email": c.EMail,
|
|
}
|
|
|
|
if c.SigningKey != "" {
|
|
settings["user.signingkey"] = c.SigningKey
|
|
settings["gpg.format"] = "openpgp"
|
|
if sshKeyMatch.MatchString(c.SigningKey) {
|
|
settings["gpg.format"] = "ssh"
|
|
}
|
|
}
|
|
|
|
if c.CommitOpts != "" {
|
|
settings["commit.cliopts"] = c.CommitOpts
|
|
}
|
|
|
|
for param, value := range settings {
|
|
if err = git.SetLocalConfig(param, value); err != nil {
|
|
logrus.WithError(err).WithField("param", param).Fatal("applying param")
|
|
}
|
|
}
|
|
}
|