diff options
| author | Shulhan <ms@kilabit.info> | 2022-04-16 01:06:38 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-04-16 18:32:36 +0700 |
| commit | dfc441b32458d204dae6498867f08e7da483c815 (patch) | |
| tree | 5348dbd28424e24719fdc582f8881551c390f3d7 /environment.go | |
| parent | f9338965943247d124174c79392033518dc06a5b (diff) | |
| download | rescached-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.go | 65 |
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 } |
