Files
cloudsave/cmd/cli/commands/add/add.go

68 lines
1.4 KiB
Go

package add
import (
"cloudsave/pkg/remote"
"cloudsave/pkg/repository"
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/google/subcommands"
)
type (
AddCmd struct {
name string
remote string
}
)
func (*AddCmd) Name() string { return "add" }
func (*AddCmd) Synopsis() string { return "add a folder to the sync list" }
func (*AddCmd) Usage() string {
return `Usage: cloudsave add [-name] [-remote] <PATH>
Add a folder to the track list
Options:
`
}
func (p *AddCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.name, "name", "", "Override the name of the game")
f.StringVar(&p.remote, "remote", "", "Defines a remote server to sync with")
}
func (p *AddCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if f.NArg() != 1 {
fmt.Fprintln(os.Stderr, "error: the command is expecting for 1 argument")
return subcommands.ExitUsageError
}
path, err := filepath.Abs(f.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, "error: cannot get the absolute path for this entry:", err)
return subcommands.ExitFailure
}
if p.name == "" {
p.name = filepath.Base(path)
}
m, err := repository.Add(p.name, path)
if err != nil {
fmt.Fprintln(os.Stderr, "error: failed to add game reference:", err)
return subcommands.ExitFailure
}
if len(strings.TrimSpace(p.remote)) > 0 {
remote.Set(m.ID, p.remote)
}
fmt.Println(m.ID)
return subcommands.ExitSuccess
}