Skip to content

Commit

Permalink
Privatize presence so it doesn't get exported
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMaas committed Feb 12, 2023
1 parent 7c59b3c commit 53c4e99
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
34 changes: 17 additions & 17 deletions cmd/go-sbot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,88 +34,88 @@ func ReadEnvironmentVariables(config *config.SbotConfig) {
// go-ssb specific env flag, for peachcloud/pub compat
if val := os.Getenv("GO_SSB_REPAIR_FS"); val != "" {
config.RepairFSBeforeStart = readEnvironmentBoolean(val)
config.Presence["repair"] = true
config.SetPresence("repair", true)
}

if val := os.Getenv("SSB_DATA_DIR"); val != "" {
config.Repo = val
config.Presence["repo"] = true
config.SetPresence("repo", true)
}

if val := os.Getenv("SSB_LOG_DIR"); val != "" {
config.DebugDir = val
config.Presence["debugdir"] = true
config.SetPresence("debugdir", true)
}

if val := os.Getenv("SSB_PROMETHEUS_ADDRESS"); val != "" {
config.MetricsAddress = val
config.Presence["debuglis"] = true
config.SetPresence("debuglis", true)
}

if val := os.Getenv("SSB_PROMETHEUS_ENABLED"); val != "" {
config.Presence["debuglis"] = readEnvironmentBoolean(val)
config.SetPresence("debuglis", readEnvironmentBoolean(val))
}

if val := os.Getenv("SSB_HOPS"); val != "" {
hops, err := strconv.Atoi(val)
check(err, "parse hops from environment variable")
config.Hops = uint(hops)
config.Presence["hops"] = true
config.SetPresence("hops", true)
}

if val := os.Getenv("SSB_MUXRPC_ADDRESS"); val != "" {
config.MuxRPCAddress = val
config.Presence["lis"] = true
config.SetPresence("lis", true)
}

if val := os.Getenv("SSB_WS_ADDRESS"); val != "" {
config.WebsocketAddress = val
config.Presence["wslis"] = true
config.SetPresence("wslis", true)
}

if val := os.Getenv("SSB_WS_TLS_CERT"); val != "" {
config.WebsocketTLSCert = val
config.Presence["wstlscert"] = true
config.SetPresence("wstlscert", true)
}

if val := os.Getenv("SSB_WS_TLS_KEY"); val != "" {
config.WebsocketTLSKey = val
config.Presence["wstlskey"] = true
config.SetPresence("wstlskey", true)
}

if val := os.Getenv("SSB_EBT_ENABLED"); val != "" {
config.EnableEBT = readEnvironmentBoolean(val)
config.Presence["enable-ebt"] = true
config.SetPresence("enable-ebt", true)
}

if val := os.Getenv("SSB_CONN_FIREWALL_ENABLED"); val != "" {
config.EnableFirewall = readEnvironmentBoolean(val)
config.Presence["promisc"] = true
config.SetPresence("promisc", true)
}

if val := os.Getenv("SSB_SOCKET_ENABLED"); val != "" {
config.NoUnixSocket = !readEnvironmentBoolean(val)
config.Presence["nounixsock"] = true
config.SetPresence("nounixsock", true)
}

if val := os.Getenv("SSB_CONN_DISCOVERY_UDP_ENABLED"); val != "" {
config.EnableDiscoveryUDP = readEnvironmentBoolean(val)
config.Presence["localdiscov"] = true
config.SetPresence("localdiscov", true)
}

if val := os.Getenv("SSB_CONN_BROADCAST_UDP_ENABLED"); val != "" {
config.EnableAdvertiseUDP = readEnvironmentBoolean(val)
config.Presence["localadv"] = true
config.SetPresence("localadv", true)
}

if val := os.Getenv("SSB_CAP_SHS_KEY"); val != "" {
config.ShsCap = val
config.Presence["shscap"] = true
config.SetPresence("shscap", true)
}

if val := os.Getenv("SSB_CAP_HMAC_KEY"); val != "" {
config.Hmac = val
config.Presence["hmac"] = true
config.SetPresence("hmac", true)
}

if val := os.Getenv("SSB_NUM_PEER"); val != "" {
Expand Down
12 changes: 6 additions & 6 deletions cmd/sbotcli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,32 @@ import (
func ReadEnvironmentVariables(config *config.SbotCliConfig) {
if val := os.Getenv("SSB_CAP_SHS_KEY"); val != "" {
config.ShsCap = val
config.Presence["shscap"] = true
config.SetPresence("shscap", true)
}

if val := os.Getenv("SSB_ADDR"); val != "" {
config.Addr = val
config.Presence["addr"] = true
config.SetPresence("addr", true)
}

if val := os.Getenv("SSB_REMOTE_KEY"); val != "" {
config.RemoteKey = val
config.Presence["remotekey"] = true
config.SetPresence("remotekey", true)
}

if val := os.Getenv("SSB_KEY"); val != "" {
config.Key = val
config.Presence["key"] = true
config.SetPresence("key", true)
}

if val := os.Getenv("SSB_UNIX_SOCK"); val != "" {
config.UnixSock = val
config.Presence["unixsock"] = true
config.SetPresence("unixsock", true)
}

if val := os.Getenv("SSB_TIMEOUT"); val != "" {
config.Timeout = val
config.Presence["timeout"] = true
config.SetPresence("timeout", true)
}
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/sbotcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,32 +150,32 @@ func initClient(ctx *cli.Context) error {
config, exists := readConfigAndEnv(ctx.String("config"))
if exists {
if !ctx.IsSet("shscap") {
if config.Presence["shscap"] != nil {
if config.Has("shscap") {
ctx.Set("shscap", config.ShsCap)
}
}
if !ctx.IsSet("addr") {
if config.Presence["addr"] != nil {
if config.Has("addr") {
ctx.Set("addr", config.Addr)
}
}
if !ctx.IsSet("remotekey") {
if config.Presence["remotekey"] != nil {
if config.Has("remotekey") {
ctx.Set("remotekey", config.RemoteKey)
}
}
if !ctx.IsSet("key") {
if config.Presence["key"] != nil {
if config.Has("key") {
ctx.Set("key", config.Key)
}
}
if !ctx.IsSet("unixsock") {
if config.Presence["unixsock"] != nil {
if config.Has("unixsock") {
ctx.Set("unixsock", config.UnixSock)
}
}
if !ctx.IsSet("timeout") {
if config.Presence["timeout"] != nil {
if config.Has("timeout") {
ctx.Set("timeout", config.Timeout)
}
}
Expand Down
27 changes: 20 additions & 7 deletions internal/config-reader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type SbotConfig struct {
NumPeer uint `json:"numPeer,omitempty"`
NumRepl uint `json:"numRepl,omitempty"`

Presence map[string]interface{}
presence map[string]interface{}
}
type SbotCliConfig struct {
ShsCap string `json:"shscap,omitempty"`
Expand All @@ -54,7 +54,7 @@ type SbotCliConfig struct {
UnixSock string `json:"unixsock,omitempty"`
Timeout string `json:"timeout,omitempty"`

Presence map[string]interface{}
presence map[string]interface{}
}

type MergedConfig struct {
Expand All @@ -63,10 +63,23 @@ type MergedConfig struct {
}

func (config SbotConfig) Has(flagname string) bool {
_, ok := config.Presence[flagname]
_, ok := config.presence[flagname]
return ok
}

func (config SbotConfig) SetPresence(flagname string, val ConfigBool) {
config.presence[flagname] = val
}

func (config SbotCliConfig) Has(flagname string) bool {
_, ok := config.presence[flagname]
return ok
}

func (config SbotCliConfig) SetPresence(flagname string, val ConfigBool) {
config.presence[flagname] = val
}

func ReadConfigSbot(configPath string) (SbotConfig, bool) {
var conf MergedConfig

Expand All @@ -92,10 +105,10 @@ func ReadConfigSbot(configPath string) (SbotConfig, bool) {
err = decoder.Decode(&presence)
check(err, "decode into presence map")
if presence["go-sbot"] != nil {
conf.GoSbot.Presence = presence["go-sbot"].(map[string]interface{})
conf.GoSbot.presence = presence["go-sbot"].(map[string]interface{})
} else {
level.Warn(log).Log("event", "read config", "msg", "no [go-sbot] detected in config file - I am not reading anything from the config file", "path", configPath)
conf.GoSbot.Presence = make(map[string]interface{})
conf.GoSbot.presence = make(map[string]interface{})
}

// help repo path's default to align with common user expectations
Expand Down Expand Up @@ -129,10 +142,10 @@ func ReadConfigSbotCli(configPath string) (SbotCliConfig, bool) {
err = decoder.Decode(&presence)
check(err, "decode into presence map")
if presence["sbotcli"] != nil {
conf.SbotCli.Presence = presence["sbotcli"].(map[string]interface{})
conf.SbotCli.presence = presence["sbotcli"].(map[string]interface{})
} else {
level.Warn(log).Log("event", "read config", "msg", "no [sbotcli] detected in config file - I am not reading anything from the config file", "path", configPath)
conf.SbotCli.Presence = make(map[string]interface{})
conf.SbotCli.presence = make(map[string]interface{})
}

return conf.SbotCli, true
Expand Down

0 comments on commit 53c4e99

Please sign in to comment.