File: setup.go

package info (click to toggle)
fdroidcl 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,828 kB
  • sloc: makefile: 3
file content (292 lines) | stat: -rw-r--r-- 6,961 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) 2024, Thomas Dickson
// See LICENSE for licensing information

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/pelletier/go-toml/v2"
)

var cmdSetup = &Command{
	UsageLine: "setup",
	Short:     "Manage setups",
	Long: ` 
List, add, remove, edit, and import/export setups.
Setups allow for mass installs onto an android device, excellent for backups.

List setups:

	$ fdroidcl setup                Show all setups
	$ fdroidcl setup list <NAME>    Show details about one setup

Modify setups:

	$ fdroidcl setup new <NAME>
	$ fdroidcl setup remove <NAME>
	$ fdroidcl setup apply <NAME>
	$ fdroidcl setup add-app <NAME> <APP-ID>
	$ fdroidcl setup rm-app <NAME> <APP-ID>
	$ fdroidcl setup add-repo <NAME> <REPO-NAME>
	$ fdroidcl setup rm-repo <NAME> <REPO-NAME>

Export setups:

	$ fdroidcl setup import <FILENAME>
	$ fdroidcl setup export <NAME>
`[1:],
}

func init() {
	cmdSetup.Run = runSetup
}

func runSetup(args []string) error {
	if len(args) == 0 {
		// list repositories
		if len(config.Setups) == 0 {
			fmt.Println("No setups!")
		} else {
			for _, value := range config.Setups {
				fmt.Printf("Name: %s\n", value.ID)
			}
		}
		return nil
	}

	switch args[0] {
	case "list":
		if len(args) != 2 {
			return fmt.Errorf("wrong amount of arguments")
		}
		return listSetup(args[1])
	case "new":
		if len(args) != 2 {
			return fmt.Errorf("wrong amount of arguments")
		}
		return newSetup(args[1])
	case "apply":
		if len(args) != 2 {
			return fmt.Errorf("wrong amount of arguments")
		}
		return applySetup(args[1])
	case "remove":
		if len(args) != 2 {
			return fmt.Errorf("wrong amount of arguments")
		}
		return removeSetup(args[1])
	case "add-app":
		if len(args) != 3 {
			return fmt.Errorf("wrong amount of arguments")
		}
		return addSetupApp(args[1], args[2])
	case "rm-app":
		if len(args) != 3 {
			return fmt.Errorf("wrong amount of arguments")
		}
		return removeSetupApp(args[1], args[2])
	case "add-repo":
		if len(args) != 3 {
			return fmt.Errorf("wrong amount of arguments")
		}
		return removeSetupRepo(args[1], args[2])
	case "rm-repo":
		if len(args) != 3 {
			return fmt.Errorf("wrong amount of arguments")
		}
		return removeSetupRepo(args[1], args[2])
	case "import":
		if len(args) != 2 {
			return fmt.Errorf("wrong amount of arguments")
		}
		return importSetup(args[1])
	case "export":
		if len(args) != 2 {
			return fmt.Errorf("wrong amount of arguments")
		}
		return exportSetup(args[1])

	}
	return fmt.Errorf("wrong usage")
}

func getIndex(sl []string, str string) int {
	index := -1
	for i, val := range sl {
		if val == str {
			index = i
			break
		}
	}
	return index
}

func listSetup(name string) error {
	index := setupIndex(name)
	if index == -1 {
		return fmt.Errorf("a setup with the name \"%s\" could not be found", name)
	}

	setup := config.Setups[index]
	fmt.Printf("Name: %s\n", setup.ID)
	fmt.Printf("Repos: %s\n", setup.Repos)
	fmt.Printf("Apps: %s\n", setup.Apps)

	return nil
}

func setupIndex(name string) int {
	index := -1
	for i, value := range config.Setups {
		if value.ID == name {
			index = i
			break
		}
	}
	return index
}

func newSetup(name string) error {
	repos := []string{}
	for _, repo := range config.Repos {
		repos = append(repos, repo.ID)
	}
	config.Setups = append(config.Setups, setup{name, []string{}, repos})
	return writeConfig(&config)
}

func applySetup(name string) error {
	index := setupIndex(name)
	if index == -1 {
		return fmt.Errorf("a setup with the name \"%s\" could not be found", name)
	}

	setup := config.Setups[index]
	for _, repo := range setup.Repos {
		index = repoIndex(repo)
		if index == -1 {
			return fmt.Errorf("setup contains unknown repo id \"%s\" ", repo)
		}
	}
	fmt.Println("All repos work!")

	if len(setup.Apps) == 0 {
		return fmt.Errorf("setup has no apps!")
	}

	for _, app := range setup.Apps {
		err := runInstall([]string{app})
		if err != nil {
			fmt.Println(err)
		}
	}
	return nil
}

func removeSetup(name string) error {
	index := setupIndex(name)
	if index == -1 {
		return fmt.Errorf("a setup with the name \"%s\" could not be found", name)
	}
	config.Setups = append(config.Setups[:index], config.Setups[index+1:]...)
	return writeConfig(&config)
}

func addSetupApp(setupName string, appName string) error {
	index := setupIndex(setupName)
	if index == -1 {
		return fmt.Errorf("a setup with the name \"%s\" could not be found", setupName)
	}
	config.Setups[index].Apps = append(config.Setups[index].Apps, appName)
	return writeConfig(&config)
}

func removeSetupApp(setupName string, appName string) error {
	index := setupIndex(setupName)
	if index == -1 {
		return fmt.Errorf("a setup with the name \"%s\" could not be found", setupName)
	}

	apps := config.Setups[index].Apps
	appIndex := getIndex(apps, appName)
	if appIndex == -1 {
		return fmt.Errorf("a app with the name \"%s\" could not be found for the setup with the name \"%s\"", appName, setupName)
	}
	config.Setups[index].Apps = append(apps[:appIndex], apps[appIndex+1:]...)

	return writeConfig(&config)
}

func addSetupRepo(setupName string, repoName string) error {
	repoIndex := repoIndex(repoName)
	if repoIndex == -1 {
		return fmt.Errorf("a repo with the name \"%s\" could not be found", repoName)
	}

	index := setupIndex(setupName)
	if index == -1 {
		return fmt.Errorf("a setup with the name \"%s\" could not be found", setupName)
	}
	config.Setups[index].Repos = append(config.Setups[index].Repos, repoName)
	return writeConfig(&config)
}

func removeSetupRepo(setupName string, repoName string) error {
	index := setupIndex(setupName)
	if index == -1 {
		return fmt.Errorf("a setup with the name \"%s\" could not be found", setupName)
	}

	repos := config.Setups[index].Repos
	repoIndex := getIndex(repos, repoName)
	if repoIndex == -1 {
		return fmt.Errorf("a repo with the name \"%s\" could not be found for the setup with the name \"%s\"", repoName, setupName)
	}
	config.Setups[index].Repos = append(repos[:repoIndex], repos[repoIndex+1:]...)

	return writeConfig(&config)
}

func importSetup(filename string) error {
	filename, err := filepath.Abs(filename)
	if err != nil {
		return err
	}

	f, err := os.Open(filename)
	if err != nil {
		return fmt.Errorf("file \"%s\" does not exist", filename)
	}
	defer f.Close()
	fileSetup := setup{}
	err = toml.NewDecoder(f).Decode(&fileSetup)
	if err != nil {
		return err
	}

	config.Setups = append(config.Setups, fileSetup)
	return writeConfig(&config)
}

func exportSetup(name string) error {
	index := setupIndex(name)
	if index == -1 {
		return fmt.Errorf("a setup with the name \"%s\" could not be found", name)
	}
	b, err := toml.Marshal(config.Setups[index])
	if err != nil {
		return fmt.Errorf("cannot encode config: %v", err)
	}
	f, err := os.Create(config.Setups[index].ID + ".toml")
	if err != nil {
		return fmt.Errorf("cannot create config file: %v", err)
	}
	_, err = f.Write(b)
	if cerr := f.Close(); err == nil {
		err = cerr
	}
	return err
}