diff options
Diffstat (limited to 'steam/steam.go')
| -rw-r--r-- | steam/steam.go | 52 |
1 files changed, 21 insertions, 31 deletions
diff --git a/steam/steam.go b/steam/steam.go index aca20df..ff0ce5a 100644 --- a/steam/steam.go +++ b/steam/steam.go @@ -1,10 +1,10 @@ -// This is designed to be a rather simplistic library to help pull information -// from a local steam library +// Package steam is designed to be a rather simplistic library to help pull +// information from a local steam library and run basic operations like +// archiving, restore and deleting games package steam import ( "bufio" - "errors" "fmt" "io/ioutil" "os" @@ -13,11 +13,15 @@ import ( "strings" ) +// Library is used to represent the steam library, the Games map is populated +// by NewLibrary when called or when ProcessLibrary is called directly type Library struct { Folder string Games map[string]Game } +// Game represents an actual game in the steam Library, allowing you to perform +// a delete, package, and such. type Game struct { Name string LibraryPath string @@ -26,24 +30,14 @@ type Game struct { var slugregexp = regexp.MustCompile(`[^-0-9A-Za-z_:.]`) +// Slug returns a safer version of the name with spaces and other chars +// transformed into dashes for use in HTML element ids and such. func (g Game) Slug() string { - // return strings.ReplaceAll(g.Name, " ", "-") return slugregexp.ReplaceAllString(g.Name, "-") } -func ProcessMultipleLibraries(r []string) ([]*Library, error) { - var libs []*Library - for _, i := range r { - lib := &Library{} - err := lib.ProcessLibrary(i) - if err != nil { - return nil, err - } - libs = append(libs, lib) - } - return libs, nil -} - +// NewLibrary returns a pointer to a processed library and an error +// if any func NewLibrary(path string) (*Library, error) { l := &Library{} err := l.ProcessLibrary(path) @@ -53,6 +47,8 @@ func NewLibrary(path string) (*Library, error) { return l, err } +// NewLibraryMust is the same as NewLibrary but calls panic if there +// is any error func NewLibraryMust(path string) *Library { l, err := NewLibrary(path) if err != nil { @@ -61,10 +57,11 @@ func NewLibraryMust(path string) *Library { return l } -// Populate the "Folder" and "Games" fields based on the provided directory +// ProcessLibrary Populates the "Folder" and "Games" fields based on the +// provided directory. func (s *Library) ProcessLibrary(r string) error { if !hasCommon(r) { - return errors.New(fmt.Sprintf("No common directory in: %s", r)) + return fmt.Errorf("No common directory in: %s", r) } s.Games = make(map[string]Game) @@ -89,12 +86,9 @@ func (s *Library) ProcessLibrary(r string) error { return nil } -// Find the ACF files related to this video game +// FindACF will return the filename of the ACF file for a given `game` func FindACF(libraryPath, game string) (string, error) { - if err := os.Chdir(libraryPath); err != nil { - return "", err - } - files, err := filepath.Glob("*.acf") + files, err := filepath.Glob(filepath.Join(libraryPath, "*.acf")) if err != nil { return "", err } @@ -110,26 +104,22 @@ func FindACF(libraryPath, game string) (string, error) { // Open up the file f, err := os.Open(fn) - defer f.Close() if err != nil { return "", err } + defer f.Close() + scanner := bufio.NewScanner(f) for scanner.Scan() { - // Finally check and see if the file has the video game name if strings.Contains(scanner.Text(), game) { return fn, nil - // fmt.Printf("%s/%s:%d: %s\n", root, path, i, scanner.Text()) } } } - str := "Couldn't find ACF file related to Game: %s" - return "", errors.New(fmt.Sprintf(str, game)) + return "", fmt.Errorf("Couldn't find ACF file related to Game: %s", game) } -// This is automatically called to print out the contents of the struct -// when things like fmt.Println are used func (s *Library) String() (str string) { str = fmt.Sprintf("Library: %s\n", s.Folder) str = str + "----\n" |
