Switch to Cobra
Also: - Fixed the Makefile - Added a Taskfile.yml (see: https://taskfile.dev) - Updated the manual page Signed-off-by: Shin'ya Minazuki <shinyoukai@laidback.moe>
This commit is contained in:
70
cmd/login.go
Normal file
70
cmd/login.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/term"
|
||||
"go.yarn.social/client"
|
||||
"git.laidback.moe/shinyoukai/mikuru"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
username string
|
||||
password string
|
||||
)
|
||||
|
||||
var loginCmd = &cobra.Command{
|
||||
Use: "login",
|
||||
Short: "Authenticate against a Yarn.social pod",
|
||||
Aliases: []string{"auth", "signin"},
|
||||
Args: cobra.MaximumNArgs(0),
|
||||
Run: func(_ *cobra.Command, _ []string) {
|
||||
cli, err := client.NewClient(client.WithURI(mikuru.Config.Host))
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create client", err)
|
||||
}
|
||||
signin(cli)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
mikuru.ConfInit()
|
||||
rootCmd.AddCommand(loginCmd)
|
||||
}
|
||||
|
||||
|
||||
func signin(cli *client.Client) {
|
||||
fmt.Printf("Username: ")
|
||||
fmt.Scanln(&username)
|
||||
|
||||
if len(username) == 0 {
|
||||
log.Fatal("No value. Bailing out")
|
||||
}
|
||||
|
||||
fmt.Printf("Password: ")
|
||||
data, err := term.ReadPassword(int(syscall.Stdin))
|
||||
if err != nil {
|
||||
log.Fatal("Unable to obtain password", err)
|
||||
}
|
||||
|
||||
password := string(data)
|
||||
|
||||
res, err := cli.Login(username, password)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("Unable to login", err)
|
||||
}
|
||||
|
||||
token := strings.Trim(fmt.Sprintf(res.Token), "{}")
|
||||
|
||||
fmt.Println("Login successful")
|
||||
fmt.Println("Place this token in your configuration file for later use")
|
||||
fmt.Println("Do not share it with anyone, it's classified information!")
|
||||
fmt.Println("")
|
||||
fmt.Printf("token = %v\n", token)
|
||||
}
|
||||
Reference in New Issue
Block a user