Browse Source
Ditched JSON configuration file. Replaced with simple key = value format. Added indented_output option to format output with indent.master
7 changed files with 127 additions and 91 deletions
@ -0,0 +1,108 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"bufio" |
||||
"io/ioutil" |
||||
"log" |
||||
"os" |
||||
"strconv" |
||||
"strings" |
||||
) |
||||
|
||||
// Configuration holds a list of process names to be tracked and a listen address.
|
||||
type Configuration struct { |
||||
ListenAddress string |
||||
IndentedOutput bool |
||||
Processes []string |
||||
} |
||||
|
||||
// LoadConfiguration loads configuration from a file.
|
||||
func LoadConfiguration(path string) (conf *Configuration, err error) { |
||||
conf = &Configuration{} |
||||
|
||||
file, err := os.Open(path) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
defer file.Close() |
||||
|
||||
s := bufio.NewScanner(file) |
||||
s.Split(bufio.ScanLines) |
||||
|
||||
for s.Scan() { |
||||
kv := strings.Split(s.Text(), " = ") |
||||
switch kv[0] { |
||||
case "listen_address": |
||||
conf.ListenAddress = kv[1] |
||||
case "indented_output": |
||||
v, err := strconv.ParseBool(kv[1]) |
||||
if err != nil { |
||||
log.Printf("WARN: could not parse \"indented_output\", valid values are true or false. Defaulted to false.\n") |
||||
} |
||||
conf.IndentedOutput = v |
||||
case "processes": |
||||
if kv[1] != "" { |
||||
conf.Processes = strings.Split(kv[1], " ") |
||||
} else { |
||||
log.Printf("WARN: \"processes\" list is empty.\n") |
||||
conf.Processes = []string{} |
||||
} |
||||
} |
||||
} |
||||
|
||||
return conf, nil |
||||
} |
||||
|
||||
// StoreConfiguration writes Configuration into a file.
|
||||
func (conf *Configuration) StoreConfiguration(path string) (err error) { |
||||
var config strings.Builder |
||||
|
||||
config.WriteString("listen_address = ") |
||||
config.WriteString(conf.ListenAddress) |
||||
config.WriteByte('\n') |
||||
config.WriteString("indented_output = ") |
||||
config.WriteString(strconv.FormatBool(conf.IndentedOutput)) |
||||
config.WriteByte('\n') |
||||
config.WriteString("processes = ") |
||||
config.WriteString(strings.Join(conf.Processes, " ")) |
||||
|
||||
if err := ioutil.WriteFile(path, []byte(config.String()), 0644); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// AddProcessToList appends a new given process into a configuration file.
|
||||
func (conf *Configuration) AddProcessToList(process string, configPath string) error { |
||||
for _, v := range conf.Processes { |
||||
if v == process { |
||||
return ErrIsOnList |
||||
} |
||||
} |
||||
|
||||
conf.Processes = append(conf.Processes, process) |
||||
if err := conf.StoreConfiguration(configPath); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// RemoveProcessFromList removes a given process from a configuration file.
|
||||
func (conf *Configuration) RemoveProcessFromList(process string, configPath string) error { |
||||
for k, v := range conf.Processes { |
||||
if v == process { |
||||
newlist := make([]string, len(conf.Processes)-1) |
||||
newlist = append(conf.Processes[:k], conf.Processes[k+1:]...) |
||||
conf.Processes = newlist |
||||
if err := conf.StoreConfiguration(configPath); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
} |
||||
|
||||
return ErrNotFound |
||||
} |
@ -1,4 +1,4 @@
|
||||
package prog |
||||
package main |
||||
|
||||
import "errors" |
||||
|
@ -1,4 +1,4 @@
|
||||
package prog |
||||
package main |
||||
|
||||
import ( |
||||
"encoding/xml" |
@ -1,77 +0,0 @@
|
||||
package prog |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"io/ioutil" |
||||
) |
||||
|
||||
// Configuration holds a list of process names to be tracked and a listen address.
|
||||
type Configuration struct { |
||||
ListenAddress string `json:"listen_address"` |
||||
Processes []string `json:"processes"` |
||||
} |
||||
|
||||
// LoadConfiguration loads configuration from a JSON file.
|
||||
func LoadConfiguration(path string) (conf *Configuration, err error) { |
||||
conf = &Configuration{} |
||||
|
||||
file, err := ioutil.ReadFile(path) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
if err := json.Unmarshal(file, conf); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return conf, nil |
||||
} |
||||
|
||||
// StoreConfiguration writes Configuration into a JSON file.
|
||||
func StoreConfiguration(path string, conf *Configuration) (err error) { |
||||
config, err := json.Marshal(*conf) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
err = ioutil.WriteFile(path, config, 0644) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// AddProcessToList appends a new given process into a configuration file.
|
||||
func AddProcessToList(process string, conf *Configuration, configPath string) error { |
||||
for _, v := range conf.Processes { |
||||
if v == process { |
||||
return ErrIsOnList |
||||
} |
||||
} |
||||
|
||||
conf.Processes = append(conf.Processes, process) |
||||
if err := StoreConfiguration(configPath, conf); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// RemoveProcessFromList removes a given process from a configuration file.
|
||||
func RemoveProcessFromList(process string, conf *Configuration, configPath string) error { |
||||
for k, v := range conf.Processes { |
||||
if v == process { |
||||
newlist := make([]string, len(conf.Processes)-1) |
||||
newlist = append(conf.Processes[:k], conf.Processes[k+1:]...) |
||||
conf.Processes = newlist |
||||
if err := StoreConfiguration(configPath, conf); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
} |
||||
|
||||
return ErrNotFound |
||||
} |
Loading…
Reference in new issue