Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cache/repo_cache_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *RepoCache) ResolveIdentityPrefix(prefix string) (*IdentityCache, error)
}

// ResolveIdentityImmutableMetadata retrieve an Identity that has the exact given metadata on
// one of it's version. If multiple version have the same key, the first defined take precedence.
// one of its version. If multiple version have the same key, the first defined take precedence.
func (c *RepoCache) ResolveIdentityImmutableMetadata(key string, value string) (*IdentityCache, error) {
return c.ResolveIdentityMatcher(func(excerpt *IdentityExcerpt) bool {
return excerpt.ImmutableMetadata[key] == value
Expand Down
32 changes: 0 additions & 32 deletions commands/add_test.go

This file was deleted.

42 changes: 0 additions & 42 deletions commands/bridge.go

This file was deleted.

43 changes: 43 additions & 0 deletions commands/bridge/bridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package bridgecmd

import (
"github.com/spf13/cobra"

"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/commands/execenv"
)

func NewBridgeCommand() *cobra.Command {
env := execenv.NewEnv()

cmd := &cobra.Command{
Use: "bridge",
Short: "List bridges to other bug trackers",
PreRunE: execenv.LoadBackend(env),
RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridge(env)
}),
Args: cobra.NoArgs,
}

cmd.AddCommand(newBridgeAuthCommand())
cmd.AddCommand(newBridgeNewCommand())
cmd.AddCommand(newBridgePullCommand())
cmd.AddCommand(newBridgePushCommand())
cmd.AddCommand(newBridgeRm())

return cmd
}

func runBridge(env *execenv.Env) error {
configured, err := bridge.ConfiguredBridges(env.Backend)
if err != nil {
return err
}

for _, c := range configured {
env.Out.Println(c)
}

return nil
}
20 changes: 10 additions & 10 deletions commands/bridge_auth.go → commands/bridge/bridge_auth.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package commands
package bridgecmd

import (
"sort"
"strings"

"github.com/spf13/cobra"

text "github.com/MichaelMure/go-term-text"
"github.com/spf13/cobra"

"github.com/MichaelMure/git-bug/bridge/core/auth"
"github.com/MichaelMure/git-bug/commands/execenv"
"github.com/MichaelMure/git-bug/util/colors"
)

func newBridgeAuthCommand() *cobra.Command {
env := newEnv()
env := execenv.NewEnv()

cmd := &cobra.Command{
Use: "auth",
Short: "List all known bridge authentication credentials.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
Short: "List all known bridge authentication credentials",
PreRunE: execenv.LoadBackend(env),
RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgeAuth(env)
}),
Args: cobra.NoArgs,
Expand All @@ -32,8 +32,8 @@ func newBridgeAuthCommand() *cobra.Command {
return cmd
}

func runBridgeAuth(env *Env) error {
creds, err := auth.List(env.backend)
func runBridgeAuth(env *execenv.Env) error {
creds, err := auth.List(env.Backend)
if err != nil {
return err
}
Expand All @@ -54,7 +54,7 @@ func runBridgeAuth(env *Env) error {
sort.Strings(meta)
metaFmt := strings.Join(meta, ",")

env.out.Printf("%s %s %s %s %s\n",
env.Out.Printf("%s %s %s %s %s\n",
colors.Cyan(cred.ID().Human()),
colors.Yellow(targetFmt),
colors.Magenta(cred.Kind()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package commands
package bridgecmd

import (
"bufio"
Expand All @@ -14,6 +14,8 @@ import (
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/bridge/core/auth"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/completion"
"github.com/MichaelMure/git-bug/commands/execenv"
)

type bridgeAuthAddTokenOptions struct {
Expand All @@ -23,14 +25,14 @@ type bridgeAuthAddTokenOptions struct {
}

func newBridgeAuthAddTokenCommand() *cobra.Command {
env := newEnv()
env := execenv.NewEnv()
options := bridgeAuthAddTokenOptions{}

cmd := &cobra.Command{
Use: "add-token [TOKEN]",
Short: "Store a new token",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
PreRunE: execenv.LoadBackendEnsureUser(env),
RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgeAuthAddToken(env, options, args)
}),
Args: cobra.MaximumNArgs(1),
Expand All @@ -41,17 +43,17 @@ func newBridgeAuthAddTokenCommand() *cobra.Command {

flags.StringVarP(&options.target, "target", "t", "",
fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
cmd.RegisterFlagCompletionFunc("target", completeFrom(bridge.Targets()))
cmd.RegisterFlagCompletionFunc("target", completion.From(bridge.Targets()))
flags.StringVarP(&options.login,
"login", "l", "", "The login in the remote bug-tracker")
flags.StringVarP(&options.user,
"user", "u", "", "The user to add the token to. Default is the current user")
cmd.RegisterFlagCompletionFunc("user", completeUser(env))
cmd.RegisterFlagCompletionFunc("user", completion.User(env))

return cmd
}

func runBridgeAuthAddToken(env *Env, opts bridgeAuthAddTokenOptions, args []string) error {
func runBridgeAuthAddToken(env *execenv.Env, opts bridgeAuthAddTokenOptions, args []string) error {
// Note: as bridgeAuthAddTokenLogin is not checked against the remote bug-tracker,
// it's possible to register a credential with an incorrect login (including bad case).
// The consequence is that it will not get picked later by the bridge. I find that
Expand All @@ -76,7 +78,7 @@ func runBridgeAuthAddToken(env *Env, opts bridgeAuthAddTokenOptions, args []stri
} else {
// Read from Stdin
if isatty.IsTerminal(os.Stdin.Fd()) {
env.err.Println("Enter the token:")
env.Err.Println("Enter the token:")
}
reader := bufio.NewReader(os.Stdin)
raw, err := reader.ReadString('\n')
Expand All @@ -90,9 +92,9 @@ func runBridgeAuthAddToken(env *Env, opts bridgeAuthAddTokenOptions, args []stri
var err error

if opts.user == "" {
user, err = env.backend.GetUserIdentity()
user, err = env.Backend.GetUserIdentity()
} else {
user, err = env.backend.ResolveIdentityPrefix(opts.user)
user, err = env.Backend.ResolveIdentityPrefix(opts.user)
}
if err != nil {
return err
Expand Down Expand Up @@ -121,11 +123,11 @@ func runBridgeAuthAddToken(env *Env, opts bridgeAuthAddTokenOptions, args []stri
return errors.Wrap(err, "invalid token")
}

err = auth.Store(env.repo, token)
err = auth.Store(env.Repo, token)
if err != nil {
return err
}

env.out.Printf("token %s added\n", token.ID())
env.Out.Printf("token %s added\n", token.ID())
return nil
}
41 changes: 41 additions & 0 deletions commands/bridge/bridge_auth_rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package bridgecmd

import (
"github.com/spf13/cobra"

"github.com/MichaelMure/git-bug/bridge/core/auth"
"github.com/MichaelMure/git-bug/commands/completion"
"github.com/MichaelMure/git-bug/commands/execenv"
)

func newBridgeAuthRm() *cobra.Command {
env := execenv.NewEnv()

cmd := &cobra.Command{
Use: "rm BRIDGE_ID",
Short: "Remove a credential",
PreRunE: execenv.LoadRepo(env),
RunE: func(cmd *cobra.Command, args []string) error {
return runBridgeAuthRm(env, args)
},
Args: cobra.ExactArgs(1),
ValidArgsFunction: completion.BridgeAuth(env),
}

return cmd
}

func runBridgeAuthRm(env *execenv.Env, args []string) error {
cred, err := auth.LoadWithPrefix(env.Repo, args[0])
if err != nil {
return err
}

err = auth.Remove(env.Repo, cred.ID())
if err != nil {
return err
}

env.Out.Printf("credential %s removed\n", cred.ID())
return nil
}
60 changes: 60 additions & 0 deletions commands/bridge/bridge_auth_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package bridgecmd

import (
"fmt"
"sort"
"strings"
"time"

"github.com/spf13/cobra"

"github.com/MichaelMure/git-bug/bridge/core/auth"
"github.com/MichaelMure/git-bug/commands/completion"
"github.com/MichaelMure/git-bug/commands/execenv"
)

func newBridgeAuthShow() *cobra.Command {
env := execenv.NewEnv()

cmd := &cobra.Command{
Use: "show",
Short: "Display an authentication credential",
PreRunE: execenv.LoadBackend(env),
RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgeAuthShow(env, args)
}),
Args: cobra.ExactArgs(1),
ValidArgsFunction: completion.BridgeAuth(env),
}

return cmd
}

func runBridgeAuthShow(env *execenv.Env, args []string) error {
cred, err := auth.LoadWithPrefix(env.Repo, args[0])
if err != nil {
return err
}

env.Out.Printf("Id: %s\n", cred.ID())
env.Out.Printf("Target: %s\n", cred.Target())
env.Out.Printf("Kind: %s\n", cred.Kind())
env.Out.Printf("Creation: %s\n", cred.CreateTime().Format(time.RFC822))

switch cred := cred.(type) {
case *auth.Token:
env.Out.Printf("Value: %s\n", cred.Value)
}

env.Out.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)

env.Out.Print(strings.Join(meta, ""))

return nil
}
Loading