aboutsummaryrefslogtreecommitdiff
path: root/cmd/cli/main.go
blob: 989592a6a58f3762d008b057f7991361c71a6f1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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()
	}
}