This commit is contained in:
@@ -1,60 +1,60 @@
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,43 +1,43 @@
|
||||
package logout
|
||||
|
||||
import (
|
||||
"cloudsave/cmd/cli/tools/prompt/credentials"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
)
|
||||
|
||||
type (
|
||||
LogoutCmd struct {
|
||||
}
|
||||
)
|
||||
|
||||
func (*LogoutCmd) Name() string { return "logout" }
|
||||
func (*LogoutCmd) Synopsis() string { return "logout from a server" }
|
||||
func (*LogoutCmd) Usage() string {
|
||||
return `Usage: cloudsave logout <SERVER_HOSTNAME>
|
||||
|
||||
Options:
|
||||
`
|
||||
}
|
||||
|
||||
func (p *LogoutCmd) SetFlags(f *flag.FlagSet) {
|
||||
}
|
||||
|
||||
func (p *LogoutCmd) 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
|
||||
}
|
||||
|
||||
if err := credentials.Logout(f.Arg(0)); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to logout: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
fmt.Println("bye!")
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
package logout
|
||||
|
||||
import (
|
||||
"cloudsave/cmd/cli/tools/prompt/credentials"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
)
|
||||
|
||||
type (
|
||||
LogoutCmd struct {
|
||||
}
|
||||
)
|
||||
|
||||
func (*LogoutCmd) Name() string { return "logout" }
|
||||
func (*LogoutCmd) Synopsis() string { return "logout from a server" }
|
||||
func (*LogoutCmd) Usage() string {
|
||||
return `Usage: cloudsave logout <SERVER_HOSTNAME>
|
||||
|
||||
Options:
|
||||
`
|
||||
}
|
||||
|
||||
func (p *LogoutCmd) SetFlags(f *flag.FlagSet) {
|
||||
}
|
||||
|
||||
func (p *LogoutCmd) 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
|
||||
}
|
||||
|
||||
if err := credentials.Logout(f.Arg(0)); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to logout: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
fmt.Println("bye!")
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
@@ -1,51 +1,51 @@
|
||||
package show
|
||||
|
||||
import (
|
||||
"cloudsave/pkg/data"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
)
|
||||
|
||||
type (
|
||||
ShowCmd struct {
|
||||
Service *data.Service
|
||||
}
|
||||
)
|
||||
|
||||
func (*ShowCmd) Name() string { return "show" }
|
||||
func (*ShowCmd) Synopsis() string { return "show metadata about game" }
|
||||
func (*ShowCmd) Usage() string {
|
||||
return `Usage: cloudsave show <GAME_ID>
|
||||
|
||||
Show metdata about a game
|
||||
`
|
||||
}
|
||||
|
||||
func (p *ShowCmd) SetFlags(f *flag.FlagSet) {
|
||||
}
|
||||
|
||||
func (p *ShowCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
if f.NArg() != 1 {
|
||||
fmt.Fprintln(os.Stderr, "error: missing game ID")
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
|
||||
gameID := f.Arg(0)
|
||||
g, err := p.Service.One(gameID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to apply: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
fmt.Println(g.Name)
|
||||
fmt.Println("------")
|
||||
fmt.Println("Version: ", g.Version)
|
||||
fmt.Println("Path: ", g.Path)
|
||||
fmt.Println("MD5: ", g.MD5)
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
package show
|
||||
|
||||
import (
|
||||
"cloudsave/pkg/data"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
)
|
||||
|
||||
type (
|
||||
ShowCmd struct {
|
||||
Service *data.Service
|
||||
}
|
||||
)
|
||||
|
||||
func (*ShowCmd) Name() string { return "show" }
|
||||
func (*ShowCmd) Synopsis() string { return "show metadata about game" }
|
||||
func (*ShowCmd) Usage() string {
|
||||
return `Usage: cloudsave show <GAME_ID>
|
||||
|
||||
Show metdata about a game
|
||||
`
|
||||
}
|
||||
|
||||
func (p *ShowCmd) SetFlags(f *flag.FlagSet) {
|
||||
}
|
||||
|
||||
func (p *ShowCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
if f.NArg() != 1 {
|
||||
fmt.Fprintln(os.Stderr, "error: missing game ID")
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
|
||||
gameID := f.Arg(0)
|
||||
g, err := p.Service.One(gameID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: failed to apply: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
fmt.Println(g.Name)
|
||||
fmt.Println("------")
|
||||
fmt.Println("Version: ", g.Version)
|
||||
fmt.Println("Path: ", g.Path)
|
||||
fmt.Println("MD5: ", g.MD5)
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ func save(store map[string]credential) error {
|
||||
Store: store,
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(filepath.Join(datastorePath, "credential.json"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
|
||||
f, err := os.OpenFile(filepath.Clean(filepath.Join(datastorePath, "credential.json")), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open datastore: %w", err)
|
||||
}
|
||||
@@ -104,7 +104,7 @@ func save(store map[string]credential) error {
|
||||
}
|
||||
|
||||
func load() (map[string]credential, error) {
|
||||
f, err := os.OpenFile(filepath.Join(datastorePath, "credential.json"), os.O_RDONLY, 0)
|
||||
f, err := os.OpenFile(filepath.Clean(filepath.Join(datastorePath, "credential.json")), os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return make(map[string]credential), nil
|
||||
|
||||
Reference in New Issue
Block a user