aboutsummaryrefslogtreecommitdiff
path: root/environment.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-04-16 01:06:38 +0700
committerShulhan <ms@kilabit.info>2022-04-16 18:32:36 +0700
commitdfc441b32458d204dae6498867f08e7da483c815 (patch)
tree5348dbd28424e24719fdc582f8881551c390f3d7 /environment.go
parentf9338965943247d124174c79392033518dc06a5b (diff)
downloadrescached-dfc441b32458d204dae6498867f08e7da483c815.tar.xz
cmd/resolver: implement command to fetch and print server environment
The "env" command fetch the current server environment and print as ini format to stdout.
Diffstat (limited to 'environment.go')
-rw-r--r--environment.go65
1 files changed, 55 insertions, 10 deletions
diff --git a/environment.go b/environment.go
index 43ce214..b8f2729 100644
--- a/environment.go
+++ b/environment.go
@@ -5,6 +5,7 @@ package rescached
import (
"fmt"
+ "io"
"strconv"
"strings"
@@ -214,13 +215,21 @@ func (env *Environment) loadResolvConf() (ok bool, err error) {
return true, nil
}
-//
-// write the options values back to file.
-//
-func (env *Environment) write(file string) (err error) {
- in, err := ini.Open(file)
- if err != nil {
- return fmt.Errorf("write: %w", err)
+func (env *Environment) save(file string) (in *ini.Ini, err error) {
+ var (
+ logp = "save"
+
+ hb *hostsBlock
+ ns string
+ )
+
+ if len(file) == 0 {
+ in = &ini.Ini{}
+ } else {
+ in, err = ini.Open(file)
+ if err != nil {
+ return nil, fmt.Errorf("%s: %w", logp, err)
+ }
}
in.Set(sectionNameRescached, "", keyFileResolvConf, env.FileResolvConf)
@@ -229,14 +238,14 @@ func (env *Environment) write(file string) (err error) {
in.UnsetAll(sectionNameRescached, "", keyHostsBlock)
- for _, hb := range env.HostsBlocks {
+ for _, hb = range env.HostsBlocks {
if hb.IsEnabled {
in.Add(sectionNameRescached, "", keyHostsBlock, hb.URL)
}
}
in.UnsetAll(sectionNameDNS, subNameServer, keyParent)
- for _, ns := range env.NameServers {
+ for _, ns = range env.NameServers {
in.Add(sectionNameDNS, subNameServer, keyParent, ns)
}
@@ -262,5 +271,41 @@ func (env *Environment) write(file string) (err error) {
in.Set(sectionNameDNS, subNameServer, keyCachePruneThreshold,
fmt.Sprintf("%s", env.ServerOptions.PruneThreshold))
- return in.Save(file)
+ return in, nil
+}
+
+// Write the configuration as ini format to Writer w.
+func (env *Environment) Write(w io.Writer) (err error) {
+ if w == nil {
+ return nil
+ }
+
+ var (
+ logp = "Environment.Write"
+ in *ini.Ini
+ )
+
+ in, err = env.save(env.fileConfig)
+ if err != nil {
+ return fmt.Errorf("%s: %w", logp, err)
+ }
+
+ return in.Write(w)
+}
+
+//
+// write the options values back to file.
+//
+func (env *Environment) write(file string) (err error) {
+ var (
+ in *ini.Ini
+ )
+ in, err = env.save(file)
+ if err != nil {
+ return err
+ }
+ if len(file) > 0 {
+ return in.Save(file)
+ }
+ return nil
}