Git Product home page Git Product logo

goe's Introduction

Overview

It provide some common helpers to load environment variables.

  • No struct tag magic, only composable functions, better IDE autocomplete.
  • .env file encryption and decryption.
  • Use default value as return type (with go generics).
  • Recursively search for the .env file, easier sub package unit testing.
  • Auto expand the environment variables in the .env file.
  • Auto parse base64 encoded value.
  • Auto read file content if the value is a file path.
  • Customizable parser.
  • Composable functions, easy to extend.

For usage check the example.

About the format of .env: link

Safely share .env file with team members

.env file usually contains sensitive information, like database password, API key, etc. It's not recommended to commit it to the version control system, but it's usually required to run the project.

We can use the whisper tool to encrypt the .env file and commit it to the version control system. Then only selected team members can access it.

First create a whisper batch config file whisper.json beside the .env file:

{
  "files": {
    ".env": ["@alice", "@bob"]
  }
}

Here alice and bob are the github user ids.

Then run the command to encrypt the .env file:

go run github.com/ysmood/whisper@latest -be whisper.json

Then you can safely commit generated .env.wsp file to the version control system.

To decrypt the .env file:

go run github.com/ysmood/whisper@latest -bd whisper.json

If you have several teams to manage, you might want to create a dedicated git repo like https://github.com/your-org/vault to hold all the .env files for different services. Then you can use command like this to decrypt the web.env.wsp file to your local:

go run github.com/ysmood/whisper@latest -d https://github.com/your-org/vault/blob/main/web.env.wsp > .env

Decrypt .env int Github Actions

Generate a key pair for the CI:

whisper -gen-key id_ci

It will generate two files id_ci and id_ci.pub. When we encrypt .env we add the id_ci.pub as a recipient.

Add the id_ci to the Github Action secret.

Assume the Github secret name is ID_CI, then we can decrypt the file in the Github Action:

name: Test

on: [push]

env:
  WHISPER_DEFAULT_KEY: ${{ secrets.ID_CI }}

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/setup-go@v4
        with:
          go-version: 1.21

      - name: setup .env
        run: go run github.com/ysmood/whisper@latest https://github.com/your-org/vault/blob/main/web.env.wsp > .env

      - name: test
        run: go test ./...

Usually you will use whisper to encrypt the id_ci file to the team's vault repo.

goe's People

Contributors

ysmood avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

zeals-co-ltd

goe's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.