flemay / envvars

Give your environment variables the love they deserve.
MIT License
20 stars 4 forks source link

Envvar 'Allowed Values' Enum? #15

Open andyt10 opened 2 years ago

andyt10 commented 2 years ago

I don't know if this is trying to solve a problem in the wrong place, but I have reached a conclusion it might be useful to specify a list of allowed values for env vars when using this tool. A validator of sorts:

Example:

envvars:
  - name: ENV
    desc: Application stage (dev, qa, preprod, prod)
    tags:
      - deploy
    optional: true
    example: dev
    values:
        - dev
        - qa
        - preprod
        - prod

I'd be happy to flex my go muscles and contribute this, just want to get some input before I do :)

flemay commented 2 years ago

Hi @andyt10 ! Thanks for creating a new issue. I did think about it before and I am not sure what is the best solution. We can start with string values but I think eventually, one will want types and different validations. A bit like AWS parameters. Is it where envvars should be headed?

I also wouldn't mind having value validation based on regex. Introducing this feature would probably be simple as well as providing more options to the user.

Lately, I have been playing with CUE. You can see my spike in _cuelang.

andyt10 commented 2 years ago

Hey @flemay I think regex validation could also be a useful feature, for similar reasons I'd like to specify a list of allowed.

I also think you might be right in saying that extending the functionality of envvars might be helpdful/desired using something like CUE . For something 'next version' like that I'd perhaps picture myself packaging up a standard library of envvar/config type definitions for use in multiple projects....somehow...but that's a bit beyond what I'm considering right now.😄

For me regex/string enum validation would suit the majority of use cases that come to mind for making sure a value conforms. I sort of pictured something like above:

      - name: SERVER_ENV
      .....
      .....
      values:
          - dev
          - qa
          - preprod
          - prod

which I think would also work similarly for regexes:

      - name: SERVER_ENV
      .....
      .....
      matchRegex: ^some[0-9]{1,}value$

where both options would be mutually exclusive (defining both would be invalid)

flemay commented 2 years ago

What I meant is that values list can be written using regex :)

package main

import (
    "fmt"
    "log"
    "regexp"
)

func main() {
    r, err := regexp.Compile("(^dev$)|(^qa$)|(^preprod$)|(^prod$)")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(r.MatchString("qa"))         // true
    fmt.Println(r.MatchString("preprod"))    // true
    fmt.Println(r.MatchString("production")) // false
}