-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdup.go
71 lines (64 loc) · 1.51 KB
/
dup.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/kurin/dup/update"
)
var config = filepath.Join(os.Getenv("HOME"), ".dup")
var (
remote = flag.String("remote", "https://domains.google.com/nic/update", "The remote API endpoint.")
host = flag.String("host", "", "The hostname to update.")
ip = flag.String("ip", "", "The IP to set. Leave blank to use the connection IP.")
offline = flag.Bool("offline", false, "Set the host 'offline' status.")
username = flag.String("username", "", "The API username.")
password = flag.String("password", "", "The API password.")
save = flag.Bool("save", false, "Save these settings.")
)
func loadJSON(file string) (*update.Config, error) {
var c update.Config
b, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
if err := json.Unmarshal(b, &c); err != nil {
return nil, err
}
return &c, nil
}
func saveJSON(file string) error {
c := &update.Config{
Remote: *remote,
Host: *host,
IP: *ip,
Offline: *offline,
Username: *username,
Password: *password,
}
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(file, b, 0600)
}
func main() {
flag.Parse()
if *save {
if err := saveJSON(config); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
c, err := loadJSON(config)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := c.Update(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}