pulling
This commit is contained in:
88
cmd/cli/commands/pull/pull.go
Normal file
88
cmd/cli/commands/pull/pull.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package pull
|
||||
|
||||
import (
|
||||
"cloudsave/pkg/game"
|
||||
"cloudsave/pkg/remote/client"
|
||||
"cloudsave/pkg/tools/archive"
|
||||
"cloudsave/pkg/tools/prompt/credentials"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
)
|
||||
|
||||
type (
|
||||
PullCmd struct {
|
||||
}
|
||||
)
|
||||
|
||||
func (*PullCmd) Name() string { return "pull" }
|
||||
func (*PullCmd) Synopsis() string { return "pull a game save from the remote" }
|
||||
func (*PullCmd) Usage() string {
|
||||
return `list:
|
||||
Pull a game save from the remote
|
||||
`
|
||||
}
|
||||
|
||||
func (p *PullCmd) SetFlags(f *flag.FlagSet) {
|
||||
|
||||
}
|
||||
|
||||
func (p *PullCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
if f.NArg() != 3 {
|
||||
fmt.Fprintln(os.Stderr, "error: missing arguments")
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
|
||||
url := f.Arg(0)
|
||||
gameID := f.Arg(1)
|
||||
path := f.Arg(2)
|
||||
|
||||
username, password, err := credentials.Read()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to read std output: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
cli := client.New(url, username, password)
|
||||
|
||||
if err := cli.Ping(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to connect to the remote: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
archivePath := filepath.Join(game.DatastorePath(), gameID, "data.tar.gz")
|
||||
|
||||
m, err := cli.Metadata(gameID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to get metadata: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
err = game.Register(m, path)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to register local metadata: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
if err := cli.Pull(gameID, archivePath); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to pull from the remote: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
fi, err := os.OpenFile(archivePath, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to open archive: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
if err := archive.Untar(fi, path); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to unarchive file: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
@@ -2,9 +2,12 @@ package version
|
||||
|
||||
import (
|
||||
"cloudsave/pkg/constants"
|
||||
"cloudsave/pkg/remote/client"
|
||||
"cloudsave/pkg/tools/prompt/credentials"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
@@ -13,6 +16,7 @@ import (
|
||||
|
||||
type (
|
||||
VersionCmd struct {
|
||||
remote bool
|
||||
}
|
||||
)
|
||||
|
||||
@@ -25,14 +29,60 @@ func (*VersionCmd) Usage() string {
|
||||
}
|
||||
|
||||
func (p *VersionCmd) SetFlags(f *flag.FlagSet) {
|
||||
f.BoolVar(&p.remote, "a", false, "get a remote version information")
|
||||
}
|
||||
|
||||
func (p *VersionCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
if p.remote {
|
||||
if f.NArg() != 1 {
|
||||
fmt.Fprintln(os.Stderr, "error: missing remote url")
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
|
||||
username, password, err := credentials.Read()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to read std output: %s", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
if err := remote(f.Arg(0), username, password); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
local()
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
func local() {
|
||||
fmt.Println("Client: CloudSave cli")
|
||||
fmt.Println(" Version: " + constants.Version)
|
||||
fmt.Println(" API version: " + strconv.Itoa(constants.ApiVersion))
|
||||
fmt.Println(" Go version: " + runtime.Version())
|
||||
fmt.Println(" OS/Arch: " + runtime.GOOS + "/" + runtime.GOARCH)
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
func remote(url, username, password string) error {
|
||||
cli := client.New(url, username, password)
|
||||
|
||||
if err := cli.Ping(); err != nil {
|
||||
return fmt.Errorf("failed to connect to the remote: %w", err)
|
||||
}
|
||||
|
||||
info, err := cli.Version()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load games from remote: %w", err)
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println("Remote:", url)
|
||||
fmt.Println("---")
|
||||
fmt.Println("Server:")
|
||||
fmt.Println(" Version: " + info.Version)
|
||||
fmt.Println(" API version: " + strconv.Itoa(info.APIVersion))
|
||||
fmt.Println(" Go version: " + info.GoVersion)
|
||||
fmt.Println(" OS/Arch: " + info.OSName + "/" + info.OSArchitecture)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user