mirror of
https://github.com/safing/portbase
synced 2025-04-12 05:29:08 +00:00
Add config export via flag and API
This commit is contained in:
parent
11e8271d41
commit
40c2849bab
3 changed files with 62 additions and 0 deletions
24
api/endpoints_config.go
Normal file
24
api/endpoints_config.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/safing/portbase/config"
|
||||
)
|
||||
|
||||
func registerConfigEndpoints() error {
|
||||
if err := RegisterEndpoint(Endpoint{
|
||||
Path: "config/options",
|
||||
Read: PermitAnyone,
|
||||
MimeType: MimeTypeJSON,
|
||||
StructFunc: listConfig,
|
||||
Name: "Export Configuration Options",
|
||||
Description: "Returns a list of all registered configuration options and their metadata. This does not include the current active or default settings.",
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func listConfig(ar *Request) (i interface{}, err error) {
|
||||
return config.ExportOptions(), nil
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
|
@ -17,6 +19,8 @@ const (
|
|||
var (
|
||||
module *modules.Module
|
||||
dataRoot *utils.DirStructure
|
||||
|
||||
exportConfig bool
|
||||
)
|
||||
|
||||
// SetDataRoot sets the data root from which the updates module derives its paths.
|
||||
|
@ -29,6 +33,8 @@ func SetDataRoot(root *utils.DirStructure) {
|
|||
func init() {
|
||||
module = modules.Register("config", prep, start, nil, "database")
|
||||
module.RegisterEvent(configChangeEvent)
|
||||
|
||||
flag.BoolVar(&exportConfig, "export-config-options", false, "export configuration registry and exit")
|
||||
}
|
||||
|
||||
func prep() error {
|
||||
|
@ -37,6 +43,10 @@ func prep() error {
|
|||
return errors.New("data root is not set")
|
||||
}
|
||||
|
||||
if exportConfig {
|
||||
modules.SetCmdLineOperation(exportConfigCmd)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -54,3 +64,13 @@ func start() error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func exportConfigCmd() error {
|
||||
data, err := json.MarshalIndent(ExportOptions(), "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = os.Stdout.Write(data)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package config
|
|||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
@ -29,6 +30,23 @@ func ForEachOption(fn func(opt *Option) error) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// ExportOptions exports the registered options. The returned data must be
|
||||
// treated as immutable.
|
||||
// The data does not include the current active or default settings.
|
||||
func ExportOptions() []*Option {
|
||||
optionsLock.RLock()
|
||||
defer optionsLock.RUnlock()
|
||||
|
||||
// Copy the map into a slice.
|
||||
opts := make([]*Option, 0, len(options))
|
||||
for _, opt := range options {
|
||||
opts = append(opts, opt)
|
||||
}
|
||||
|
||||
sort.Sort(sortByKey(opts))
|
||||
return opts
|
||||
}
|
||||
|
||||
// GetOption returns the option with name or an error
|
||||
// if the option does not exist. The caller should lock
|
||||
// the returned option itself for further processing.
|
||||
|
|
Loading…
Add table
Reference in a new issue