This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
/
cmd.go
59 lines (51 loc) · 1.66 KB
/
cmd.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
package cmd
import (
"flag"
"os"
"github.com/18F/e-QIP-prototype/api"
"github.com/18F/e-QIP-prototype/api/cloudfoundry"
"github.com/18F/e-QIP-prototype/api/env"
"github.com/18F/e-QIP-prototype/api/postgresql"
)
var (
flagAll = flag.Bool("all", false, "apply to all accounts")
)
// Command represents a basic utility command.
func Command(log api.LogService, action func(api.DatabaseService, *api.Account)) {
cloudfoundry.Configure()
settings := &env.Native{}
settings.Configure()
database := &postgresql.Service{Log: log, Env: settings}
database.Configure()
flag.Parse()
if *flagAll {
// Retrieve all accounts within the system from the database so we
// may iterate through them.
var accounts []*api.Account
if err := database.FindAll(&accounts); err != nil {
log.WarnError(api.NoAccount, err, api.LogFields{})
return
}
// Iterate through the accounts with a given database context.
for _, account := range accounts {
if _, err := account.Get(database, 0); err != nil {
log.WarnError(api.NoAccount, err, api.LogFields{"account": account.Username})
continue
}
// Perform the provided actions on the given account.
action(database, account)
}
} else {
// Assume all arguments are a username delimited by white space.
for _, username := range os.Args[1:] {
// Retrieve the account with the provided username and database context.
account := &api.Account{Username: username}
if _, err := account.Get(database, 0); err != nil {
log.WarnError(api.NoAccount, err, api.LogFields{"account": account.Username})
continue
}
// Perform the provided actions on the given account.
action(database, account)
}
}
}