Skip to content

Commit f0f04c7

Browse files
committed
Update options and logic
1 parent baa5b59 commit f0f04c7

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/coverage.txt
1010
# Mac OS X files
1111
.DS_Store
12+
mem_backup.json
1213

1314
# Binaries for programs and plugins
1415
*.exe

cmd/util/main.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ import (
1111
)
1212

1313
var (
14-
client *memClient
15-
host *string
16-
port *string
17-
path *string
18-
backup *bool
19-
restore *bool
14+
client *memClient
15+
addr *string
16+
path *string
17+
operation *string
2018

2119
format = logging.MustStringFormatter(
2220
`%{color}%{time:2006-01-02T15:04:05.999999} %{shortfunc} ▶ %{level:.8s} %{id:03x}%{color:reset} %{message}`,
@@ -37,11 +35,9 @@ func init() {
3735
// Set the backend to be used.
3836
logging.SetBackend(backendLevelFormatted)
3937

40-
host = flag.String("host", `0.0.0.0`, "Memcached hostname")
41-
port = flag.String("port", "11211", "Memcached port")
42-
path = flag.String("name", "mem_backup.json", "Path to store the output file at")
43-
backup = flag.Bool("backup", false, "Whether to backup the cache")
44-
restore = flag.Bool("restore", false, "Whether to restore the cache")
38+
addr = flag.String("addr", `localhost:11211`, "Address to memcached server")
39+
path = flag.String("filename", "mem_backup.json", "Path to store the output file at")
40+
operation = flag.String("op", "", "Whether to backup the cache")
4541

4642
// If the given filename does not have the suffix, add to it
4743
*path = strings.Trim(*path, "/")
@@ -51,7 +47,7 @@ func init() {
5147

5248
flag.Parse()
5349

54-
client = createClient(host, port)
50+
client = createClient(addr)
5551
}
5652

5753
// backupCache: Exports the cache into file at the given path
@@ -110,18 +106,14 @@ func restoreCache() {
110106

111107
// main: Validates the arguments and processes backup or restore
112108
func main() {
113-
Logger.Infof("host %s", *host)
114-
Logger.Infof("port %s", *port)
109+
Logger.Infof("address %s", *addr)
115110

116-
// If both the options are given or none of the options are given
117-
if (*backup && *restore) || (!*backup && !*restore) {
118-
Logger.Error("Exactly one option --backup or --restore is required")
119-
os.Exit(1)
120-
}
121-
122-
if *backup {
111+
if *operation == "backup" {
123112
backupCache()
124-
} else if *restore {
113+
} else if *operation == "restore" {
125114
restoreCache()
115+
} else {
116+
Logger.Error("--op is required with either 'backup' or 'restore'")
117+
os.Exit(1)
126118
}
127119
}

cmd/util/memcached.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ func (client *memClient) Stat(statName string) (Stat, bool) {
196196

197197
// Creates a memClient and deals with any errors
198198
// that might occur (e.g. unable to connect to server).
199-
func createClient(host, port *string) (*memClient) {
200-
server := *host + ":" + *port
199+
func createClient(addr *string) (*memClient) {
200+
server := *addr
201201
client, err := MemClient(server)
202202
if err != nil {
203203
fmt.Fprintln(os.Stderr, "Unable to connect to", server)

0 commit comments

Comments
 (0)