package main import ( "errors" "flag" "fmt" "os" "riedstra.dev/mitch/steam-export/steam" ) 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) default: printHelp() } return nil } func printHelp() { fmt.Printf(`--- Program usage: steam-export-cli $subcommand $options Subcommands: list package extract delete server Type in a subcommand -h or -help for more information `) } func listGames(args []string) error { fl := flag.NewFlagSet("list", flag.ExitOnError) lib := fl.String("l", DefaultLib, "Path to library in question. All the way to the 'steamapps' folder") fl.Parse(args) steamLib := &steam.Library{} if err := steamLib.ProcessLibrary(*lib); err != nil { return err } fmt.Println(steamLib) return nil } func packageGame(args []string) error { fl := flag.NewFlagSet("package", flag.ExitOnError) libPth := fl.String("l", DefaultLib, "Path to library in question. All the way to the 'steamapps' folder") fileName := fl.String("f", "", "Name of archive to be created") game := fl.String("g", "", "Name of the game to be exported.") fl.Parse(args) if *fileName == "" { return errors.New("You need to specify a file name") } lib := &steam.Library{} if err := lib.ProcessLibrary(*libPth); err != nil { return err } G, ok := lib.Games[*game] if !ok { return errors.New("Game does not exist") } f, err := os.OpenFile(*fileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0664) if err != nil { return err } defer f.Close() err = G.Package(f) if err != nil { return err } return nil } func extractGame(args []string) error { fl := flag.NewFlagSet("extract", flag.ExitOnError) libPath := fl.String("l", 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. Include the file extension") fl.Parse(args) lib := &steam.Library{} if err := lib.ProcessLibrary(*libPath); err != nil { return err } fh, err := os.Open(*fileName) if err != nil { return err } return lib.Extract(fh) } func deleteGame(args []string) error { fl := flag.NewFlagSet("delete", flag.ExitOnError) libPath := fl.String("l", DefaultLib, "Path to library in question. All the way to the 'steamapps' folder") game := fl.String("g", "", "Name of the game to be deleted.") fl.Parse(args) lib := &steam.Library{} if err := lib.ProcessLibrary(*libPath); err != nil { return err } G, ok := lib.Games[*game] if !ok { return errors.New("Game does not exist") } return G.Delete() } func main() { if err := parseArgs(os.Args); err != nil { fmt.Fprintln(os.Stderr, err) printHelp() } }