aboutsummaryrefslogtreecommitdiff
path: root/cmd/cli/main.go
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2020-11-20 19:56:29 -0500
committerMitch Riedstra <mitch@riedstra.us>2020-11-20 19:56:29 -0500
commit971cc396e1d01f53f65a86278fd0ac4490565335 (patch)
tree19d0a1e861590b5058ca682c2f5a33828414cf3d /cmd/cli/main.go
parent9ff47bdc17c70f87c78520d0636b2ff22918a408 (diff)
downloadsteam-export-971cc396e1d01f53f65a86278fd0ac4490565335.tar.gz
steam-export-971cc396e1d01f53f65a86278fd0ac4490565335.tar.xz
Reorganize. Update to use go modules.
Diffstat (limited to 'cmd/cli/main.go')
-rw-r--r--cmd/cli/main.go196
1 files changed, 196 insertions, 0 deletions
diff --git a/cmd/cli/main.go b/cmd/cli/main.go
new file mode 100644
index 0000000..3292d30
--- /dev/null
+++ b/cmd/cli/main.go
@@ -0,0 +1,196 @@
+package main
+
+import (
+ "errors"
+ "flag"
+ "fmt"
+ "os"
+
+ "riedstra.dev/mitch/steam-export/config"
+ "riedstra.dev/mitch/steam-export/steam"
+)
+
+var (
+ errorHandling flag.ErrorHandling = flag.ExitOnError
+
+ steamLib *steam.Library = &steam.Library{}
+)
+
+func parseArgs(args []string) error {
+ if len(args) < 2 {
+ return errors.New("Not enough arguments")
+ }
+ aa := args[2:]
+ switch a := args[1]; a {
+ case "list":
+ return listGames(aa)
+ case "package":
+ return packageGame(aa)
+ case "extract":
+ return extractGame(aa)
+ case "delete":
+ return deleteGame(aa)
+ case "edit-config":
+ return editConfig(aa)
+ default:
+ printHelp()
+ }
+
+ return nil
+}
+
+func printHelp() {
+ fmt.Printf(`---
+Program usage:
+steam-export-cli $subcommand $options
+Subcommands:
+ list
+ package
+ extract
+ delete
+ server
+ edit-config
+
+Type in a subcommand -h or -help for more information
+`)
+
+}
+
+func editConfig(args []string) error {
+ fl := flag.NewFlagSet("edit-config", errorHandling)
+ e := fl.String("e", "", "Editor to invoke")
+ if err := fl.Parse(args); err != nil {
+ return err
+ }
+ return config.EditDefaultConfig(*e)
+}
+
+func listGames(args []string) error {
+ fl := flag.NewFlagSet("list", errorHandling)
+ lib := fl.String("l", steam.DefaultLib,
+ "Path to library in question. All the way to the 'steamapps' folder")
+
+ l := steam.DefaultLib
+ fl.Parse(args)
+ if fl.Parsed() {
+ l = string(*lib)
+ }
+
+ if err := steamLib.ProcessLibrary(l); err != nil {
+ return err
+ }
+
+ fmt.Println(steamLib)
+
+ return nil
+}
+
+func packageGame(args []string) error {
+ fl := flag.NewFlagSet("package", errorHandling)
+ lib := fl.String("l", steam.DefaultLib,
+ "Path to library in question. All the way to the 'steamapps' folder")
+ fileName := fl.String("f", "export",
+ "Name of the archive file to be created. Please do not include the file extension")
+ game := fl.Int("g", -1,
+ "Index of the game to be exported. Please see `list` for the index")
+ compress := fl.String("z", "gz",
+ "Compression type. Default 'gz' '' is no compression")
+
+ var g int
+ l := steam.DefaultLib
+ fl.Parse(args)
+ if fl.Parsed() {
+ l = string(*lib)
+ g = int(*game)
+ }
+
+ if err := steamLib.ProcessLibrary(l); err != nil {
+ return err
+ }
+
+ if len(steamLib.Games) < g || g == -1 {
+ return errors.New("Cannot find game for index provided or no index provided")
+ }
+
+ if *fileName == "export" {
+ fileName = &steamLib.Games[g]
+ }
+
+ switch *compress {
+ case "gz":
+ return steamLib.PackageGameToFile(g, *fileName+".tar.gz", *compress)
+ default:
+ return steamLib.PackageGameToFile(g, *fileName+".tar", *compress)
+ }
+
+}
+
+func extractGame(args []string) error {
+ fl := flag.NewFlagSet("extract", errorHandling)
+ lib := fl.String("l", steam.DefaultLib,
+ "Path to library in question. All the way to the 'steamapps' folder")
+ fileName := fl.String("f", "",
+ "Name of the archive file to be extracted. Please include the file extension")
+ compress := fl.String("z", "gz",
+ "Compression type. Default 'gz' '' is no compression")
+
+ l := steam.DefaultLib
+ fl.Parse(args)
+ if fl.Parsed() {
+ l = string(*lib)
+ }
+
+ if err := steamLib.ProcessLibrary(l); err != nil {
+ return err
+ }
+
+ if *fileName == "" {
+ return errors.New("No filename provided")
+ }
+
+ return steamLib.ExtractGameFromFile(*fileName, *compress)
+
+}
+
+func deleteGame(args []string) error {
+ fl := flag.NewFlagSet("delete", errorHandling)
+ lib := fl.String("l", steam.DefaultLib,
+ "Path to library in question. All the way to the 'steamapps' folder")
+ game := fl.Int("g", -1,
+ "Index of the game to be deleted. Please see `list` for the index")
+
+ var g int
+ l := steam.DefaultLib
+ fl.Parse(args)
+ if fl.Parsed() {
+ l = string(*lib)
+ g = int(*game)
+ }
+
+ if err := steamLib.ProcessLibrary(l); err != nil {
+ return err
+ }
+
+ if len(steamLib.Games) < g || g == -1 {
+ return errors.New("Cannot find game for index provided or no index provided")
+ }
+
+ return steamLib.DeleteGame(g)
+}
+
+func main() {
+ config, err := config.LoadConfig()
+ if err != nil {
+ fmt.Println(err)
+ } else {
+ if config.DefaultRepository != "" {
+ steam.DefaultLib = config.DefaultRepository
+ }
+ }
+
+ if err := parseArgs(os.Args); err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ printHelp()
+ }
+}
+