forked from nextdns/nextdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
66 lines (63 loc) · 1.51 KB
/
config.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/nextdns/nextdns/config"
)
func cfg(args []string) error {
args = args[1:]
subCmd := "list"
if len(args) > 0 {
subCmd = args[0]
args = args[1:]
}
switch subCmd {
case "list":
var c config.Config
c.Parse("nextdns config list", args, true)
return c.Write(os.Stdout)
case "set":
var c config.Config
c.Parse("nextdns config set", args, true)
return c.Save()
case "edit":
var c config.Config
c.Parse("nextdns config edit", nil, true)
tmp, err := ioutil.TempFile("", "")
if err != nil {
return err
}
defer os.Remove(tmp.Name())
if err := c.Write(tmp); err != nil {
tmp.Close()
return err
}
tmp.Close()
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
cmd := exec.Command(editor, tmp.Name())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s: %v", editor, err)
}
c = config.Config{}
c.Parse("nextdns config edit", []string{"-config-file", tmp.Name()}, true)
c.File = ""
return c.Save()
case "wizard":
return installer("configure")
default:
return errors.New("usage: \n" +
" config list list configuration options\n" +
" config set [options] set a configuration option\n" +
" (see config set -h for list of options)\n" +
" config edit edit configuration using default editor\n" +
" config wizard run the configuration wizard")
}
}