Some checks failed
CloudSave/pipeline/head Something is wrong with the build of this commit
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package login
|
|
|
|
import (
|
|
"cloudsave/cmd/cli/tools/prompt/credentials"
|
|
"cloudsave/pkg/remote/client"
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/google/subcommands"
|
|
)
|
|
|
|
type (
|
|
LoginCmd struct {
|
|
}
|
|
)
|
|
|
|
func (*LoginCmd) Name() string { return "login" }
|
|
func (*LoginCmd) Synopsis() string { return "save IN PLAIN TEXT your credentials" }
|
|
func (*LoginCmd) Usage() string {
|
|
return `Usage: cloudsave login <SERVER_HOSTNAME>
|
|
|
|
Warning: this command saves the login into a plain text json file
|
|
|
|
Options:
|
|
`
|
|
}
|
|
|
|
func (p *LoginCmd) SetFlags(f *flag.FlagSet) {
|
|
}
|
|
|
|
func (p *LoginCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
|
if f.NArg() != 1 {
|
|
fmt.Fprintf(os.Stderr, "error: this command take 1 argument")
|
|
return subcommands.ExitUsageError
|
|
}
|
|
|
|
server := f.Arg(0)
|
|
|
|
username, password, err := credentials.Read(server)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error: failed to read std output: %s", err)
|
|
return subcommands.ExitFailure
|
|
}
|
|
|
|
cli := client.New(server, username, password)
|
|
if _, err := cli.Version(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "error: failed to login: %s", err)
|
|
return subcommands.ExitFailure
|
|
}
|
|
|
|
if err := credentials.Login(username, password, server); err != nil {
|
|
fmt.Fprintf(os.Stderr, "error: failed to save login: %s", err)
|
|
return subcommands.ExitFailure
|
|
}
|
|
|
|
fmt.Println("login information saved!")
|
|
return subcommands.ExitSuccess
|
|
}
|