Merge pull request #273 from MichaelMure/user-create-set

cmd: "user create" only assign the user identity if not set
This commit is contained in:
Amine 2019-12-10 20:52:01 +01:00 committed by GitHub
commit ef6801a37f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

4
cache/repo_cache.go vendored
View File

@ -877,6 +877,10 @@ func (c *RepoCache) GetUserIdentity() (*IdentityCache, error) {
return cached, nil
}
func (c *RepoCache) IsUserIdentitySet() (bool, error) {
return identity.IsUserIdentitySet(c.repo)
}
// NewIdentity create a new identity
// The new identity is written in the repository (commit)
func (c *RepoCache) NewIdentity(name string, email string) (*IdentityCache, error) {

View File

@ -53,11 +53,18 @@ func runUserCreate(cmd *cobra.Command, args []string) error {
return err
}
err = backend.SetUserIdentity(id)
set, err := backend.IsUserIdentitySet()
if err != nil {
return err
}
if !set {
err = backend.SetUserIdentity(id)
if err != nil {
return err
}
}
_, _ = fmt.Fprintln(os.Stderr)
fmt.Println(id.Id())

View File

@ -261,6 +261,16 @@ func GetUserIdentity(repo repository.Repo) (*Identity, error) {
return i, 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
}
func (i *Identity) AddVersion(version *Version) {
i.versions = append(i.versions, version)
}