git-bug/bridge/core/config.go

52 lines
1.1 KiB
Go
Raw Normal View History

2020-01-12 18:38:16 +03:00
package core
2020-01-24 02:30:13 +03:00
import (
2020-02-15 15:45:14 +03:00
"fmt"
2020-01-24 02:30:13 +03:00
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/identity"
)
func FinishConfig(repo *cache.RepoCache, metaKey string, login string) error {
// if no user exist with the given login metadata
_, err := repo.ResolveIdentityImmutableMetadata(metaKey, login)
if err != nil && err != identity.ErrIdentityNotExist {
// real error
return err
}
if err == nil {
// found an already valid user, all good
return nil
}
// if a default user exist, tag it with the login
user, err := repo.GetUserIdentity()
2020-02-09 00:04:51 +03:00
if err != nil && err != identity.ErrNoIdentitySet {
2020-01-24 02:30:13 +03:00
// real error
return err
}
if err == nil {
2020-02-15 15:45:14 +03:00
fmt.Printf("Current identity %v tagged with login %v\n", user.Id().Human(), login)
2020-01-24 02:30:13 +03:00
// found one
user.SetMetadata(metaKey, login)
return user.CommitAsNeeded()
}
// otherwise create a user with that metadata
i, err := repo.NewIdentityFromGitUserRaw(map[string]string{
metaKey: login,
})
2020-02-09 00:08:35 +03:00
if err != nil {
return err
}
2020-01-24 02:30:13 +03:00
err = repo.SetUserIdentity(i)
if err != nil {
return err
}
2020-02-15 15:45:14 +03:00
fmt.Printf("Identity %v created, set as current\n", i.Id().Human())
2020-01-24 02:30:13 +03:00
return nil
}