DEV Community

Cover image for New method of application configuration (occson)
Tomasz Kowalewski
Tomasz Kowalewski

Posted on

New method of application configuration (occson)

Are you irritated by having to configure applications with multiple environmental variables?

If so – you’ve come to the right place – we have a tool that will make configuration management a cakewalk :)

Take a look at occson to learn how you can easily manage configuration via environmental variables.

And now, let’s create a simple environment variable provider in golang.

To start, let’s familiarize ourselves with the occson library API

Let’s import occson

package main

import {
  occson "github.com/occson/go-occson"
}
Enter fullscreen mode Exit fullscreen mode

and download the configuration using its API

func main() {
  uri := os.Args[1]
  access_token := os.Getenv("OCCSON_ACCESS_TOKEN")
  passphrase := os.Getenv("OCCSON_PASSPHRASE")

  doc := occson.NewDocument(uri, access_token, passphrase)

  decrypted, err := doc.Download()

  if err != nil {
    panic(err)
  }
}
Enter fullscreen mode Exit fullscreen mode

i.e., using the command line interface call OCCSON_ACCESS_TOKEN=[ACCESS_TOKEN] OCCSON_PASSPHRASE=[PASSPHRASE] go run occson.go occson://test to download the config.

Now, it’s a good idea to parse the resulting document and embed the environmental variables.

splitted := strings.Split(string(decrypted), "\n")

for _, element := range splitted {
  env := strings.Split(element, "=")
  os.Setenv(env[0], env[1])
}
Enter fullscreen mode Exit fullscreen mode

Then, we intercept the target command from the assigned arguments and deploy it.

cmd := exec.Command(os.Args[3], os.Args[4:]...)
stdout, _ := cmd.StdoutPipe()
cmd.Start()

scanner := bufio.NewScanner(stdout)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
  m := scanner.Text()
  fmt.Println(m)
}
cmd.Wait()
Enter fullscreen mode Exit fullscreen mode

Deploying OCCSON_ACCESS_TOKEN=[ACCESS_TOKEN] OCCSON_PASSPHRASE=[PASSPHRASE] go run occson.go occson://test -- printenv will result in downloading the configuration from test and embedding it in the environmental variables, after which printenv will give us the defined environmental variables.

Register an account on occson and add your first config. Then prepare (go build occson.go) a configuration provider and start using this breakthrough method of configuration downloading (OCCSON_ACCESS_TOKEN=[ACCESS_TOKEN] OCCSON_PASSPHRASE=[PASSPHRASE] occson occson://test -- printenv)

# occson.go
package main

import (
  "bufio"
  "fmt"
  "os"
  "os/exec"
  "strings"

  occson "github.com/occson/go-occson"
)

func main() {
  uri := os.Args[1]
  access_token := os.Getenv("OCCSON_ACCESS_TOKEN")
  passphrase := os.Getenv("OCCSON_PASSPHRASE")

  doc := occson.NewDocument(uri, access_token, passphrase)

  decrypted, err := doc.Download()

  if err != nil {
    panic(err)
  }

  splitted := strings.Split(string(decrypted), "\n")

  for _, element := range splitted {
    env := strings.Split(element, "=")
    os.Setenv(env[0], env[1])
  }

  cmd := exec.Command(os.Args[3], os.Args[4:]...)
  stdout, _ := cmd.StdoutPipe()
  cmd.Start()

  scanner := bufio.NewScanner(stdout)
  scanner.Split(bufio.ScanLines)
  for scanner.Scan() {
    m := scanner.Text()
    fmt.Println(m)
  }
  cmd.Wait()
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)