diff options
| -rw-r--r-- | archive/unarchive.go | 64 | ||||
| -rw-r--r-- | steam-export-cli.go | 70 | ||||
| -rw-r--r-- | steam/package.go | 61 | ||||
| -rw-r--r-- | steam/steam.go | 2 |
4 files changed, 191 insertions, 6 deletions
diff --git a/archive/unarchive.go b/archive/unarchive.go new file mode 100644 index 0000000..5089370 --- /dev/null +++ b/archive/unarchive.go @@ -0,0 +1,64 @@ +package archive + +import ( + "archive/tar" + fp "path/filepath" + + "io" + "os" +) + +type Unarchive struct { + Input string + inputFile *os.File + tarReader *tar.Reader +} + +// Extracts a tar arcive to the current working directory +// This will overwrite everything. So be careful +func (u *Unarchive) UnTar() error { + var err error + u.inputFile, err = os.Open(u.Input) + if err != nil { + return err + } + defer u.inputFile.Close() + u.tarReader = tar.NewReader(u.inputFile) + + for { + hdr, err := u.tarReader.Next() + if err == io.EOF { + // We've reached the end! Whoee + break + } + if err != nil { + return err + } + + info := hdr.FileInfo() + if info.IsDir() { + if err = os.MkdirAll(hdr.Name, info.Mode()); err != nil { + return err + } + continue + } + + if err = os.MkdirAll(fp.Dir(hdr.Name), info.Mode()); err != nil { + return err + } + + // Create a file handle to work with + // f, err := os.Create(hdr.Name) + f, err := os.OpenFile(hdr.Name, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode()) + if err != nil { + return err + } + defer f.Close() + if _, err := io.Copy(f, u.tarReader); err != nil { + return err + } + + } + + return nil +} diff --git a/steam-export-cli.go b/steam-export-cli.go new file mode 100644 index 0000000..46f96dd --- /dev/null +++ b/steam-export-cli.go @@ -0,0 +1,70 @@ +package main + +import ( + "errors" + "flag" + "fmt" + "os" + + "git.riedstra.us/mitch/steam-export/steam" +) + +var ( + errorHandling flag.ErrorHandling = flag.ExitOnError + + defaultLib string = "C:\\Program Files (x86)\\Steam\\steamapps" + steamLib *steam.Library = &steam.Library{} +) + +func parseArgs(args []string) error { + if len(args) < 2 { + return errors.New("Not enough arguments") + } + switch a := args[1]; a { + case "list": + return listGames(args[2:]) + default: + printHelp() + } + + return nil +} + +func printHelp() { + fmt.Printf(`Program usage: +steam-export-cli $subcommand $options +Subcommands: + list -l $steam_library + package -l $steam_library -f $output_file [ -G $game_index | -g "game_name" ] + extract -l $steam_library -f $input_file + delete -l $steam_library [ -G $game_index | -g $game_name ] + server -c $config_file -l $steam_library -L $listen_addr`) + +} + +func listGames(args []string) error { + fl := flag.NewFlagSet("list", errorHandling) + lib := fl.String("l", defaultLib, + "Path to library in question. All the way to the 'steamapps' folder") + + l := 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 main() { + if err := parseArgs(os.Args); err != nil { + fmt.Fprintln(os.Stderr, err) + printHelp() + } +} diff --git a/steam/package.go b/steam/package.go index 72d29ee..8a4ff8b 100644 --- a/steam/package.go +++ b/steam/package.go @@ -1,27 +1,30 @@ package steam import ( - // "fmt" "git.riedstra.us/mitch/steam-export/archive" "os" - // "strings" + "path/filepath" ) -func (l *Library) PackageGame(g string) error { +func (l *Library) PackageGameToFile(index int, file string) error { + g := l.Games[index] + working_dir, err := os.Getwd() if err != nil { return err } - output := working_dir + "/" + g + ".tar" + // output := working_dir + "/" + g + ".tar" + output, err := filepath.Abs(file) + if err != nil { + return err + } os.Chdir(l.Folder) acf, err := l.FindACF(g) if err != nil { return err } - // acf = strings.Replace(acf, l.Folder, "", -1) input := []string{"common/" + g, acf} - // fmt.Fprintf(os.Stderr, "input arguments for archive: %s\n", input) a := archive.Archive{Output: output, Input: input} err = a.Tar() if err != nil { @@ -32,3 +35,49 @@ func (l *Library) PackageGame(g string) error { return nil } + +func (l *Library) ExtractGameFromFile(f string) error { + working_dir, err := os.Getwd() + if err != nil { + return err + } + + f, err = filepath.Abs(f) + if err != nil { + return err + } + + if err = os.Chdir(l.Folder); err != nil { + return err + } + u := &archive.Unarchive{ + Input: f, + } + if err := u.UnTar(); err != nil { + return err + } + + if err = os.Chdir(working_dir); err != nil { + return err + } + return nil +} + +/* +func (l *Library) DeleteGame(i index) error { + g := l.Games[i] + + working_dir, err := os.Getwd() + if err != nil { + return err + } + + // os.RemoveAll() + + if err = os.Chdir(working_dir); err != nil { + return err + } + + return nil +} +*/ diff --git a/steam/steam.go b/steam/steam.go index 9715f38..ea27e5a 100644 --- a/steam/steam.go +++ b/steam/steam.go @@ -46,6 +46,8 @@ func (s *Library) ProcessLibrary(r string) error { s.Games = append(s.Games, f.Name()) } } + } else { + return errors.New(fmt.Sprintf("No common directory in: %s", r)) } return nil } |
