diff options
author | Michael Muré <batolettre@gmail.com> | 2020-02-09 20:08:12 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-02-09 20:23:38 +0100 |
commit | b3d3612393387c83fa43f908dbb8e2a71068c834 (patch) | |
tree | e4609e21dc74e535d45b38cd7d0504681c544160 /commands | |
parent | dca85b309a0a82e9993a345964d0831ab2876fb4 (diff) | |
parent | 3caffeef4d2ed25d4eb5d4bfd262f4fc3b92561f (diff) | |
download | git-bug-b3d3612393387c83fa43f908dbb8e2a71068c834.tar.gz |
Merge remote-tracking branch 'origin/master' into cheshirekow-jira
Diffstat (limited to 'commands')
-rw-r--r-- | commands/bridge_auth.go | 19 | ||||
-rw-r--r-- | commands/bridge_auth_addtoken.go | 51 | ||||
-rw-r--r-- | commands/bridge_auth_show.go | 21 | ||||
-rw-r--r-- | commands/bridge_configure.go | 2 | ||||
-rw-r--r-- | commands/bridge_pull.go | 2 | ||||
-rw-r--r-- | commands/commands.go | 4 | ||||
-rw-r--r-- | commands/user.go | 3 | ||||
-rw-r--r-- | commands/user_adopt.go | 3 | ||||
-rw-r--r-- | commands/user_create.go | 8 | ||||
-rw-r--r-- | commands/webui.go | 4 |
10 files changed, 86 insertions, 31 deletions
diff --git a/commands/bridge_auth.go b/commands/bridge_auth.go index 4e8b50c4..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,22 +37,19 @@ func runBridgeAuth(cmd *cobra.Command, args []string) error { value = cred.Value } - 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 5352957d..fbbf60a7 100644 --- a/commands/bridge_auth_show.go +++ b/commands/bridge_auth_show.go @@ -2,14 +2,25 @@ 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/interrupt" ) func runBridgeAuthShow(cmd *cobra.Command, args []string) error { + backend, err := cache.NewRepoCache(repo) + if err != nil { + return err + } + defer backend.Close() + interrupt.RegisterCleaner(backend.Close) + cred, err := auth.LoadWithPrefix(repo, args[0]) if err != nil { return err @@ -25,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/bridge_configure.go b/commands/bridge_configure.go index 26bd7bc2..0e29d06a 100644 --- a/commands/bridge_configure.go +++ b/commands/bridge_configure.go @@ -206,7 +206,7 @@ git bug bridge configure \ --target=github \ --url=https://github.com/michaelmure/git-bug \ --token=$(TOKEN)`, - PreRunE: loadRepoEnsureUser, + PreRunE: loadRepo, RunE: runBridgeConfigure, } diff --git a/commands/bridge_pull.go b/commands/bridge_pull.go index 692ec5e9..2dd3d93e 100644 --- a/commands/bridge_pull.go +++ b/commands/bridge_pull.go @@ -138,7 +138,7 @@ func parseSince(since string) (time.Time, error) { var bridgePullCmd = &cobra.Command{ Use: "pull [<name>]", Short: "Pull updates.", - PreRunE: loadRepoEnsureUser, + PreRunE: loadRepo, RunE: runBridgePull, Args: cobra.MaximumNArgs(1), } diff --git a/commands/commands.go b/commands/commands.go index a30c38a5..bd8cb14d 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -27,9 +27,7 @@ func runCommands(cmd *cobra.Command, args []string) error { cmd := queue[0] queue = queue[1:] allCmds = append(allCmds, cmd) - for _, c := range cmd.Commands() { - queue = append(queue, c) - } + queue = append(queue, cmd.Commands()...) } sort.Sort(commandSorterByName(allCmds)) 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 d84d4048..7054f1f7 100644 --- a/commands/user_adopt.go +++ b/commands/user_adopt.go @@ -4,9 +4,10 @@ import ( "fmt" "os" + "github.com/spf13/cobra" + "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/util/interrupt" - "github.com/spf13/cobra" ) func runUserAdopt(cmd *cobra.Command, args []string) error { diff --git a/commands/user_create.go b/commands/user_create.go index 15b9767e..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.PromptValueRequired("Name", preName) + 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.PromptValueRequired("Email", preEmail) + email, err := input.PromptDefault("Email", "email", preEmail, input.Required) if err != nil { return err } - login, err := input.PromptValue("Avatar URL", "") + 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 } diff --git a/commands/webui.go b/commands/webui.go index 2e1a1bc0..5d3d4b4a 100644 --- a/commands/webui.go +++ b/commands/webui.go @@ -12,7 +12,7 @@ import ( "os/signal" "time" - "github.com/99designs/gqlgen/handler" + "github.com/99designs/gqlgen/graphql/playground" "github.com/gorilla/mux" "github.com/phayes/freeport" "github.com/skratchdot/open-golang/open" @@ -57,7 +57,7 @@ func runWebUI(cmd *cobra.Command, args []string) error { } // Routes - router.Path("/playground").Handler(handler.Playground("git-bug", "/graphql")) + router.Path("/playground").Handler(playground.Handler("git-bug", "/graphql")) router.Path("/graphql").Handler(graphqlHandler) router.Path("/gitfile/{hash}").Handler(newGitFileHandler(repo)) router.Path("/upload").Methods("POST").Handler(newGitUploadFileHandler(repo)) |