diff options
author | Michael Muré <batolettre@gmail.com> | 2020-07-16 20:06:20 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-07-16 20:06:20 +0200 |
commit | e38d702132dca04ec95178f68c500a9591c3631b (patch) | |
tree | 1609e36fc7a35e55114335729c0f0b0fa878de66 | |
parent | c448cf8cd3c03e71f2ea40d2aac68f77b963f993 (diff) | |
download | git-bug-e38d702132dca04ec95178f68c500a9591c3631b.tar.gz |
some light code reorg
-rw-r--r-- | identity/identity.go | 105 | ||||
-rw-r--r-- | identity/identity_user.go | 71 |
2 files changed, 93 insertions, 83 deletions
diff --git a/identity/identity.go b/identity/identity.go index bf0e853c..c43cfd97 100644 --- a/identity/identity.go +++ b/identity/identity.go @@ -4,7 +4,6 @@ package identity import ( "encoding/json" "fmt" - "os" "reflect" "strings" "time" @@ -70,6 +69,28 @@ func NewIdentityFull(name string, email string, login string, avatarUrl string) } } +// NewFromGitUser will query the repository for user detail and +// build the corresponding Identity +func NewFromGitUser(repo repository.Repo) (*Identity, error) { + name, err := repo.GetUserName() + if err != nil { + return nil, err + } + if name == "" { + return nil, errors.New("user name is not configured in git yet. Please use `git config --global user.name \"John Doe\"`") + } + + email, err := repo.GetUserEmail() + if err != nil { + return nil, err + } + if email == "" { + return nil, errors.New("user name is not configured in git yet. Please use `git config --global user.email johndoe@example.com`") + } + + return NewIdentity(name, email), nil +} + // MarshalJSON will only serialize the id func (i *Identity) MarshalJSON() ([]byte, error) { return json.Marshal(&IdentityStub{ @@ -198,88 +219,6 @@ func readAllIdentities(repo repository.ClockedRepo, refPrefix string) <-chan Str return out } -// NewFromGitUser will query the repository for user detail and -// build the corresponding Identity -func NewFromGitUser(repo repository.Repo) (*Identity, error) { - name, err := repo.GetUserName() - if err != nil { - return nil, err - } - if name == "" { - return nil, errors.New("user name is not configured in git yet. Please use `git config --global user.name \"John Doe\"`") - } - - email, err := repo.GetUserEmail() - if err != nil { - return nil, err - } - if email == "" { - return nil, errors.New("user name is not configured in git yet. Please use `git config --global user.email johndoe@example.com`") - } - - return NewIdentity(name, email), nil -} - -// SetUserIdentity store the user identity's id in the git config -func SetUserIdentity(repo repository.RepoConfig, identity *Identity) error { - return repo.LocalConfig().StoreString(identityConfigKey, identity.Id().String()) -} - -// GetUserIdentity read the current user identity, set with a git config entry -func GetUserIdentity(repo repository.Repo) (*Identity, error) { - id, err := GetUserIdentityId(repo) - if err != nil { - return nil, err - } - - i, err := ReadLocal(repo, id) - if err == ErrIdentityNotExist { - innerErr := repo.LocalConfig().RemoveAll(identityConfigKey) - if innerErr != nil { - _, _ = fmt.Fprintln(os.Stderr, errors.Wrap(innerErr, "can't clear user identity").Error()) - } - return nil, err - } - - return i, nil -} - -func GetUserIdentityId(repo repository.Repo) (entity.Id, error) { - configs, err := repo.LocalConfig().ReadAll(identityConfigKey) - if err != nil { - return entity.UnsetId, err - } - - if len(configs) == 0 { - return entity.UnsetId, ErrNoIdentitySet - } - - if len(configs) > 1 { - return entity.UnsetId, ErrMultipleIdentitiesSet - } - - var id entity.Id - for _, val := range configs { - id = entity.Id(val) - } - - if err := id.Validate(); err != nil { - return entity.UnsetId, err - } - - return id, nil -} - -// IsUserIdentitySet say if the user has set his identity -func IsUserIdentitySet(repo repository.Repo) (bool, error) { - configs, err := repo.LocalConfig().ReadAll(identityConfigKey) - if err != nil { - return false, err - } - - return len(configs) == 1, nil -} - type Mutator struct { Name string Login string diff --git a/identity/identity_user.go b/identity/identity_user.go new file mode 100644 index 00000000..60622c12 --- /dev/null +++ b/identity/identity_user.go @@ -0,0 +1,71 @@ +package identity + +import ( + "fmt" + "os" + + "github.com/pkg/errors" + + "github.com/MichaelMure/git-bug/entity" + "github.com/MichaelMure/git-bug/repository" +) + +// SetUserIdentity store the user identity's id in the git config +func SetUserIdentity(repo repository.RepoConfig, identity *Identity) error { + return repo.LocalConfig().StoreString(identityConfigKey, identity.Id().String()) +} + +// GetUserIdentity read the current user identity, set with a git config entry +func GetUserIdentity(repo repository.Repo) (*Identity, error) { + id, err := GetUserIdentityId(repo) + if err != nil { + return nil, err + } + + i, err := ReadLocal(repo, id) + if err == ErrIdentityNotExist { + innerErr := repo.LocalConfig().RemoveAll(identityConfigKey) + if innerErr != nil { + _, _ = fmt.Fprintln(os.Stderr, errors.Wrap(innerErr, "can't clear user identity").Error()) + } + return nil, err + } + + return i, nil +} + +func GetUserIdentityId(repo repository.Repo) (entity.Id, error) { + configs, err := repo.LocalConfig().ReadAll(identityConfigKey) + if err != nil { + return entity.UnsetId, err + } + + if len(configs) == 0 { + return entity.UnsetId, ErrNoIdentitySet + } + + if len(configs) > 1 { + return entity.UnsetId, ErrMultipleIdentitiesSet + } + + var id entity.Id + for _, val := range configs { + id = entity.Id(val) + } + + if err := id.Validate(); err != nil { + return entity.UnsetId, err + } + + return id, nil +} + +// IsUserIdentitySet say if the user has set his identity +func IsUserIdentitySet(repo repository.Repo) (bool, error) { + configs, err := repo.LocalConfig().ReadAll(identityConfigKey) + if err != nil { + return false, err + } + + return len(configs) == 1, nil +} |