diff options
Diffstat (limited to 'commands')
-rw-r--r-- | commands/bridge_auth.go | 26 | ||||
-rw-r--r-- | commands/bridge_auth_addtoken.go | 51 | ||||
-rw-r--r-- | commands/bridge_auth_show.go | 32 | ||||
-rw-r--r-- | commands/user.go | 3 | ||||
-rw-r--r-- | commands/user_adopt.go | 15 | ||||
-rw-r--r-- | commands/user_create.go | 8 |
6 files changed, 72 insertions, 63 deletions
diff --git a/commands/bridge_auth.go b/commands/bridge_auth.go index bfbab33c..3a0e0c29 100644 --- a/commands/bridge_auth.go +++ b/commands/bridge_auth.go @@ -2,6 +2,8 @@ package commands import ( "fmt" + "sort" + "strings" "github.com/spf13/cobra" @@ -26,8 +28,6 @@ func runBridgeAuth(cmd *cobra.Command, args []string) error { return err } - defaultUser, _ := backend.GetUserIdentity() - for _, cred := range creds { targetFmt := text.LeftPadMaxLine(cred.Target(), 10, 0) @@ -37,29 +37,19 @@ func runBridgeAuth(cmd *cobra.Command, args []string) error { value = cred.Value } - var userFmt string - - switch cred.UserId() { - case auth.DefaultUserId: - userFmt = colors.Red("default user") - default: - user, err := backend.ResolveIdentity(cred.UserId()) - if err != nil { - return err - } - userFmt = user.DisplayName() - - if cred.UserId() == defaultUser.Id() { - userFmt = colors.Red(userFmt) - } + meta := make([]string, 0, len(cred.Metadata())) + for k, v := range cred.Metadata() { + meta = append(meta, k+":"+v) } + sort.Strings(meta) + metaFmt := strings.Join(meta, ",") fmt.Printf("%s %s %s %s %s\n", colors.Cyan(cred.ID().Human()), colors.Yellow(targetFmt), colors.Magenta(cred.Kind()), - userFmt, value, + metaFmt, ) } diff --git a/commands/bridge_auth_addtoken.go b/commands/bridge_auth_addtoken.go index 018015e4..9a937f4d 100644 --- a/commands/bridge_auth_addtoken.go +++ b/commands/bridge_auth_addtoken.go @@ -13,24 +13,37 @@ import ( "github.com/MichaelMure/git-bug/bridge" "github.com/MichaelMure/git-bug/bridge/core" "github.com/MichaelMure/git-bug/bridge/core/auth" - "github.com/MichaelMure/git-bug/identity" + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/util/interrupt" ) var ( bridgeAuthAddTokenTarget string + bridgeAuthAddTokenLogin string + bridgeAuthAddTokenUser string ) func runBridgeTokenAdd(cmd *cobra.Command, args []string) error { - var value string - if bridgeAuthAddTokenTarget == "" { return fmt.Errorf("flag --target is required") } + if bridgeAuthAddTokenLogin == "" { + return fmt.Errorf("flag --login is required") + } + + backend, err := cache.NewRepoCache(repo) + if err != nil { + return err + } + defer backend.Close() + interrupt.RegisterCleaner(backend.Close) if !core.TargetExist(bridgeAuthAddTokenTarget) { return fmt.Errorf("unknown target") } + var value string + if len(args) == 1 { value = args[0] } else { @@ -46,12 +59,36 @@ func runBridgeTokenAdd(cmd *cobra.Command, args []string) error { value = strings.TrimSuffix(raw, "\n") } - user, err := identity.GetUserIdentity(repo) + var user *cache.IdentityCache + + if bridgeAuthAddTokenUser == "" { + user, err = backend.GetUserIdentity() + } else { + user, err = backend.ResolveIdentityPrefix(bridgeAuthAddTokenUser) + } if err != nil { return err } - token := auth.NewToken(user.Id(), value, bridgeAuthAddTokenTarget) + metaKey, _ := bridge.LoginMetaKey(bridgeAuthAddTokenTarget) + login, ok := user.ImmutableMetadata()[metaKey] + + switch { + case ok && login == bridgeAuthAddTokenLogin: + // nothing to do + case ok && login != bridgeAuthAddTokenLogin: + return fmt.Errorf("this user is already tagged with a different %s login", bridgeAuthAddTokenTarget) + default: + user.SetMetadata(metaKey, bridgeAuthAddTokenLogin) + err = user.Commit() + if err != nil { + return err + } + } + + token := auth.NewToken(value, bridgeAuthAddTokenTarget) + token.SetMetadata(auth.MetaKeyLogin, bridgeAuthAddTokenLogin) + if err := token.Validate(); err != nil { return errors.Wrap(err, "invalid token") } @@ -77,5 +114,9 @@ func init() { bridgeAuthCmd.AddCommand(bridgeAuthAddTokenCmd) bridgeAuthAddTokenCmd.Flags().StringVarP(&bridgeAuthAddTokenTarget, "target", "t", "", fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ","))) + bridgeAuthAddTokenCmd.Flags().StringVarP(&bridgeAuthAddTokenLogin, + "login", "l", "", "The login in the remote bug-tracker") + bridgeAuthAddTokenCmd.Flags().StringVarP(&bridgeAuthAddTokenUser, + "user", "u", "", "The user to add the token to. Default is the current user") bridgeAuthAddTokenCmd.Flags().SortFlags = false } diff --git a/commands/bridge_auth_show.go b/commands/bridge_auth_show.go index 02c56806..fbbf60a7 100644 --- a/commands/bridge_auth_show.go +++ b/commands/bridge_auth_show.go @@ -2,13 +2,14 @@ package commands import ( "fmt" + "sort" + "strings" "time" "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/bridge/core/auth" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/colors" "github.com/MichaelMure/git-bug/util/interrupt" ) @@ -25,28 +26,9 @@ func runBridgeAuthShow(cmd *cobra.Command, args []string) error { return err } - var userFmt string - - switch cred.UserId() { - case auth.DefaultUserId: - userFmt = colors.Red("default user") - default: - user, err := backend.ResolveIdentity(cred.UserId()) - if err != nil { - return err - } - userFmt = user.DisplayName() - - defaultUser, _ := backend.GetUserIdentity() - if cred.UserId() == defaultUser.Id() { - userFmt = colors.Red(userFmt) - } - } - fmt.Printf("Id: %s\n", cred.ID()) fmt.Printf("Target: %s\n", cred.Target()) fmt.Printf("Kind: %s\n", cred.Kind()) - fmt.Printf("User: %s\n", userFmt) fmt.Printf("Creation: %s\n", cred.CreateTime().Format(time.RFC822)) switch cred := cred.(type) { @@ -54,6 +36,16 @@ func runBridgeAuthShow(cmd *cobra.Command, args []string) error { fmt.Printf("Value: %s\n", cred.Value) } + fmt.Println("Metadata:") + + meta := make([]string, 0, len(cred.Metadata())) + for key, value := range cred.Metadata() { + meta = append(meta, fmt.Sprintf(" %s --> %s\n", key, value)) + } + sort.Strings(meta) + + fmt.Print(strings.Join(meta, "")) + return nil } diff --git a/commands/user.go b/commands/user.go index f669c73f..5cf40cf0 100644 --- a/commands/user.go +++ b/commands/user.go @@ -50,8 +50,6 @@ func runUser(cmd *cobra.Command, args []string) error { Time().Format("Mon Jan 2 15:04:05 2006 +0200")) case "lastModificationLamport": fmt.Printf("%d\n", id.LastModificationLamport()) - case "login": - fmt.Printf("%s\n", id.Login()) case "metadata": for key, value := range id.ImmutableMetadata() { fmt.Printf("%s\n%s\n", key, value) @@ -68,7 +66,6 @@ func runUser(cmd *cobra.Command, args []string) error { fmt.Printf("Id: %s\n", id.Id()) fmt.Printf("Name: %s\n", id.Name()) - fmt.Printf("Login: %s\n", id.Login()) fmt.Printf("Email: %s\n", id.Email()) fmt.Printf("Last modification: %s (lamport %d)\n", id.LastModification().Time().Format("Mon Jan 2 15:04:05 2006 +0200"), diff --git a/commands/user_adopt.go b/commands/user_adopt.go index a7de54d9..7054f1f7 100644 --- a/commands/user_adopt.go +++ b/commands/user_adopt.go @@ -4,11 +4,10 @@ import ( "fmt" "os" - "github.com/MichaelMure/git-bug/bridge/core/auth" + "github.com/spf13/cobra" + "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/util/interrupt" - "github.com/spf13/cobra" ) func runUserAdopt(cmd *cobra.Command, args []string) error { @@ -26,16 +25,6 @@ func runUserAdopt(cmd *cobra.Command, args []string) error { return err } - _, err = backend.GetUserIdentity() - if err == identity.ErrNoIdentitySet { - err = auth.ReplaceDefaultUser(repo, i.Id()) - if err != nil { - return err - } - } else if err != nil { - return err - } - err = backend.SetUserIdentity(i) if err != nil { return err diff --git a/commands/user_create.go b/commands/user_create.go index dab8a879..95e09050 100644 --- a/commands/user_create.go +++ b/commands/user_create.go @@ -23,7 +23,7 @@ func runUserCreate(cmd *cobra.Command, args []string) error { return err } - name, err := input.Prompt("Name", "name", preName, input.Required) + name, err := input.PromptDefault("Name", "name", preName, input.Required) if err != nil { return err } @@ -33,17 +33,17 @@ func runUserCreate(cmd *cobra.Command, args []string) error { return err } - email, err := input.Prompt("Email", "email", preEmail, input.Required) + email, err := input.PromptDefault("Email", "email", preEmail, input.Required) if err != nil { return err } - login, err := input.Prompt("Avatar URL", "avatar", "") + avatarURL, err := input.Prompt("Avatar URL", "avatar") if err != nil { return err } - id, err := backend.NewIdentityRaw(name, email, "", login, nil) + id, err := backend.NewIdentityRaw(name, email, avatarURL, nil) if err != nil { return err } |