Skip to content

Commit

Permalink
feat(arguments): Arguments can now be passed as environment variables.
Browse files Browse the repository at this point in the history
The following flags have an equivalent environment variable:
- a : FALCOSIDEKICK_UI_ADDR
- p : FALCOSIDEKICK_UI_PORT
- r : FALCOSIDEKICK_UI_REDIS_URL
- x : FALCOSIDEKICK_UI_DEV

The precedence order is:
- Command line flag
- Environment
- Default Value

Signed-off-by: Lyonel Martinez <[email protected]>
  • Loading branch information
Lowaiz authored and poiana committed Jun 27, 2022
1 parent e3ef80a commit f2a3ed4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ Events are stored in a `Redis` server with [`Redisearch`](https://github.com/Red
## Usage

### Options
#### Precedence: flag value -> environment variable value -> default value

```shell
-a string
Listen Address (default "0.0.0.0")
Listen Address (default "0.0.0.0", environment "FALCOSIDEKICK_UI_ADDR")
-d Enable dark mode as default
-p int
Listen Port (default 2802)
Listen Port (default 2802, environment "FALCOSIDEKICK_UI_PORT")
-r string
Redis server address (default "localhost:6379")
-x Allow CORS for development
Redis server address (default "localhost:6379", environment "FALCOSIDEKICK_UI_REDIS_URL")
-x Allow CORS for development (environment "FALCOSIDEKICK_UI_DEV")
```

### Run with docker
Expand Down
15 changes: 15 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package utils

import (
"flag"
"fmt"
"log"
"os"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -77,3 +79,16 @@ func RemoveDuplicate(input []string) []string {
sort.Strings(singleKeys)
return singleKeys
}

func GetFlagOrEnvParam(flagString string, envVar string, defaultValue string, usage string) *string {
res := flag.String(flagString, "NON-SET", usage)
if *res == "NON-SET" {
str, present := os.LookupEnv(envVar)
if present {
*res = str
} else {
*res = defaultValue
}
}
return res
}
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/http"
"os"
"strconv"

"github.com/falcosecurity/falcosidekick-ui/configuration"
"github.com/falcosecurity/falcosidekick-ui/internal/api"
Expand Down Expand Up @@ -34,12 +35,20 @@ type CustomValidator struct {
}

func init() {
addr := flag.String("a", "0.0.0.0", "Listen Address")
port := flag.Int("p", 2802, "Listen Port")
addr := utils.GetFlagOrEnvParam("a", "FALCOSIDEKICK_UI_ADDR", "0.0.0.0", "Listen Address")
portString := utils.GetFlagOrEnvParam("p", "FALCOSIDEKICK_UI_PORT", "2802", "Listen Port")
port, err := strconv.Atoi(*portString)
if err != nil {
utils.WriteLog("error", "Failed to parse Listen Port", true)
}
redisserver := utils.GetFlagOrEnvParam("r", "FALCOSIDEKICK_UI_REDIS_URL", "localhost:6379", "Redis server address")
dev := flag.Bool("x", false, "Allow CORS for development")
if !*dev {
_, *dev = os.LookupEnv("FALCOSIDEKICK_UI_DEV")
}

// darkmod := flag.Bool("d", false, "Enable dark mode as default")
version := flag.Bool("v", false, "Print version")
dev := flag.Bool("x", false, "Allow CORS for development")
redisserver := flag.String("r", "localhost:6379", "Redis server address")
flag.Parse()

if *version {
Expand All @@ -54,7 +63,7 @@ func init() {
utils.WriteLog("error", "Failed to parse Listen Address", true)
}
config.ListenAddress = *addr
config.ListenPort = *port
config.ListenPort = port
// config.DisplayMode = light
// if *darkmod {
// config.DisplayMode = dark
Expand Down

0 comments on commit f2a3ed4

Please sign in to comment.