git-bug/bridge/core/config.go

53 lines
1.2 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/entities/identity"
2022-11-19 13:33:12 +03:00
"github.com/MichaelMure/git-bug/entity"
2020-01-24 02:30:13 +03:00
)
func FinishConfig(repo *cache.RepoCache, metaKey string, login string) error {
// if no user exist with the given login metadata
_, err := repo.Identities().ResolveIdentityImmutableMetadata(metaKey, login)
2022-11-19 13:33:12 +03:00
if err != nil && !entity.IsErrNotFound(err) {
2020-01-24 02:30:13 +03:00
// 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.Identities().NewFromGitUserRaw(map[string]string{
2020-01-24 02:30:13 +03:00
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
}