2019-09-18 22:00:07 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-11-10 16:46:55 +03:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mattn/go-isatty"
|
2019-10-12 12:10:44 +03:00
|
|
|
"github.com/pkg/errors"
|
2019-09-18 22:00:07 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-11-10 16:46:55 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge"
|
2019-09-18 22:00:07 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
2019-12-08 23:15:06 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core/auth"
|
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2019-09-18 22:00:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-10 19:32:14 +03:00
|
|
|
bridgeAuthAddTokenTarget string
|
2019-09-18 22:00:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func runBridgeTokenAdd(cmd *cobra.Command, args []string) error {
|
2019-11-10 16:46:55 +03:00
|
|
|
var value string
|
|
|
|
|
2019-11-10 19:32:14 +03:00
|
|
|
if bridgeAuthAddTokenTarget == "" {
|
2019-12-08 23:15:06 +03:00
|
|
|
return fmt.Errorf("flag --target is required")
|
2019-11-10 16:46:55 +03:00
|
|
|
}
|
|
|
|
|
2019-11-10 19:32:14 +03:00
|
|
|
if !core.TargetExist(bridgeAuthAddTokenTarget) {
|
2019-11-10 16:46:55 +03:00
|
|
|
return fmt.Errorf("unknown target")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
value = args[0]
|
|
|
|
} else {
|
|
|
|
// Read from Stdin
|
|
|
|
if isatty.IsTerminal(os.Stdin.Fd()) {
|
|
|
|
fmt.Println("Enter the token:")
|
|
|
|
}
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
raw, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading from stdin: %v", err)
|
|
|
|
}
|
|
|
|
value = strings.TrimSuffix(raw, "\n")
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
user, err := identity.GetUserIdentity(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
token := auth.NewToken(user.Id(), value, bridgeAuthAddTokenTarget)
|
2019-11-08 16:49:24 +03:00
|
|
|
if err := token.Validate(); err != nil {
|
2019-10-12 12:10:44 +03:00
|
|
|
return errors.Wrap(err, "invalid token")
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
err = auth.Store(repo, token)
|
2019-11-10 16:46:55 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("token %s added\n", token.ID())
|
|
|
|
return nil
|
2019-09-18 22:00:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-10 19:32:14 +03:00
|
|
|
var bridgeAuthAddTokenCmd = &cobra.Command{
|
|
|
|
Use: "add-token [<token>]",
|
2019-11-10 16:46:55 +03:00
|
|
|
Short: "Store a new token",
|
2019-12-08 23:15:06 +03:00
|
|
|
PreRunE: loadRepoEnsureUser,
|
2019-09-18 22:00:07 +03:00
|
|
|
RunE: runBridgeTokenAdd,
|
2019-11-10 16:46:55 +03:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
2019-09-18 22:00:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2019-11-10 19:32:14 +03:00
|
|
|
bridgeAuthCmd.AddCommand(bridgeAuthAddTokenCmd)
|
|
|
|
bridgeAuthAddTokenCmd.Flags().StringVarP(&bridgeAuthAddTokenTarget, "target", "t", "",
|
2019-11-10 16:46:55 +03:00
|
|
|
fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
|
2019-11-10 19:32:14 +03:00
|
|
|
bridgeAuthAddTokenCmd.Flags().SortFlags = false
|
2019-09-18 22:00:07 +03:00
|
|
|
}
|