-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.go
233 lines (204 loc) · 4.75 KB
/
main.go
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
// Copyright (c) 2015, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
const cmdName = "fdroidcl"
const version = "v0.7.0"
func subdir(dir, name string) string {
p := filepath.Join(dir, name)
if err := os.MkdirAll(p, 0o755); err != nil {
fmt.Fprintf(os.Stderr, "Could not create dir '%s': %v\n", p, err)
}
return p
}
func mustCache() string {
dir, err := os.UserCacheDir()
if err != nil {
fmt.Fprintln(os.Stderr, err)
panic("TODO: return an error")
}
return subdir(dir, cmdName)
}
func mustData() string {
dir, err := os.UserConfigDir()
if err != nil {
fmt.Fprintln(os.Stderr, err)
panic("TODO: return an error")
}
return subdir(dir, cmdName)
}
func configPath() string {
return filepath.Join(mustData(), "config.json")
}
type repo struct {
ID string `json:"id"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
}
type userConfig struct {
Repos []repo `json:"repos"`
}
var config = userConfig{
Repos: []repo{
{
ID: "f-droid",
URL: "https://f-droid.org/repo",
Enabled: true,
},
{
ID: "f-droid-archive",
URL: "https://f-droid.org/archive",
Enabled: false,
},
},
}
func readConfig() error {
f, err := os.Open(configPath())
if err != nil {
// ignore error, if file does not exist
return nil
}
defer f.Close()
fileConfig := userConfig{}
err = json.NewDecoder(f).Decode(&fileConfig)
if err != nil {
return err
}
config = fileConfig
return nil
}
// A Command is an implementation of a go command
// like go build or go fix.
type Command struct {
// Run runs the command.
// The args are the arguments after the command name.
Run func(args []string) error
// UsageLine is the one-line usage message.
// The first word in the line is taken to be the command name.
UsageLine string
// Short is the short, single-line description.
Short string
// Long is an optional longer version of the Short description.
Long string
Fset flag.FlagSet
}
// Name returns the command's name: the first word in the usage line.
func (c *Command) Name() string {
name := c.UsageLine
i := strings.Index(name, " ")
if i >= 0 {
name = name[:i]
}
return name
}
func (c *Command) usage() {
fmt.Fprintf(os.Stderr, "usage: %s %s\n\n", cmdName, c.UsageLine)
if c.Long == "" {
fmt.Fprintf(os.Stderr, "%s.\n", c.Short)
} else {
fmt.Fprint(os.Stderr, c.Long)
}
anyFlags := false
c.Fset.VisitAll(func(f *flag.Flag) { anyFlags = true })
if anyFlags {
fmt.Fprintf(os.Stderr, "\nAvailable options:\n")
c.Fset.PrintDefaults()
}
}
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [-h] <command> [<args>]\n\n", cmdName)
fmt.Fprintf(os.Stderr, "Available commands:\n")
maxUsageLen := 0
for _, c := range commands {
if len(c.UsageLine) > maxUsageLen {
maxUsageLen = len(c.UsageLine)
}
}
for _, c := range commands {
fmt.Fprintf(os.Stderr, " %s%s %s\n", c.UsageLine,
strings.Repeat(" ", maxUsageLen-len(c.UsageLine)), c.Short)
}
fmt.Fprintf(os.Stderr, `
An appid is just an app's unique package name. A specific version of an app can
be selected by following the appid with a colon and the version code. The
'search' and 'show' commands can be used to find these strings. For example:
$ fdroidcl search redreader
$ fdroidcl show org.quantumbadger.redreader
$ fdroidcl install org.quantumbadger.redreader:85
`)
fmt.Fprintf(os.Stderr, "\nUse %s <command> -h for more information.\n", cmdName)
}
}
// Commands lists the available commands.
var commands = []*Command{
cmdUpdate,
cmdSearch,
cmdShow,
cmdInstall,
cmdUninstall,
cmdDownload,
cmdDevices,
cmdList,
cmdRepo,
cmdClean,
cmdDefaults,
cmdVersion,
}
var cmdVersion = &Command{
UsageLine: "version",
Short: "Print version information",
Run: func(args []string) error {
if len(args) > 0 {
return fmt.Errorf("no arguments allowed")
}
fmt.Println(version)
return nil
},
}
func main() {
os.Exit(main1())
}
func main1() int {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
flag.Usage()
return 2
}
cmdName := args[0]
for _, cmd := range commands {
if cmd.Name() != cmdName {
continue
}
cmd.Fset.Init(cmdName, flag.ContinueOnError)
cmd.Fset.Usage = cmd.usage
if err := cmd.Fset.Parse(args[1:]); err != nil {
if err != flag.ErrHelp {
fmt.Fprintf(os.Stderr, "flag: %v\n", err)
cmd.Fset.Usage()
}
return 2
}
err := readConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "config %s: %v\n", configPath(), err)
return 1
}
if err := cmd.Run(cmd.Fset.Args()); err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", cmdName, err)
return 1
}
return 0
}
fmt.Fprintf(os.Stderr, "Unrecognised command '%s'\n\n", cmdName)
flag.Usage()
return 2
}