Skip to content

Commit

Permalink
refactor: treat os environment as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeguo committed Jul 30, 2024
1 parent d65aee6 commit af8298c
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 186 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cSpell.words": [
"charsets",
"exem",
"injectenv",
"menv",
"merrs",
"mexec",
Expand Down
14 changes: 3 additions & 11 deletions internal/menv/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,12 @@ const (
)

// Construct create the env map with current system environ, extra and rendering MINIT_ENV_ prefixed keys
func Construct(extra map[string]string) (envs map[string]string, err error) {
func Construct(sys map[string]string, extra map[string]string) (envs map[string]string, err error) {
envs = make(map[string]string)

// system env
for _, item := range osEnviron() {
splits := strings.SplitN(item, "=", 2)
var key, val string
if len(splits) > 0 {
key = splits[0]
if len(splits) > 1 {
val = splits[1]
}
envs[key] = val
}
for key, val := range sys {
envs[key] = val
}

// merge extra env
Expand Down
28 changes: 3 additions & 25 deletions internal/menv/construct_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
package menv

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

func replaceTestEnv(m map[string]string) {
osGetenv = func(key string) string {
return m[key]
}
osEnviron = func() []string {
var env []string
for k, v := range m {
env = append(env, k+"="+v)
}
return env
}
}

func restoreTestEnv() {
osGetenv = os.Getenv
osEnviron = os.Environ
}

func TestBuild(t *testing.T) {
replaceTestEnv(map[string]string{
"HOME": "/home/minit",
})
defer restoreTestEnv()

func TestConstruct(t *testing.T) {
envs, err := Construct(map[string]string{
"HOME": "/home/minit",
}, map[string]string{
"HOME-": "NONE",
"MINIT_ENV_BUF": "{{stringsToUpper \"bbb\"}}",
})
Expand Down
23 changes: 23 additions & 0 deletions internal/menv/environ.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package menv

import (
"os"
"strings"
)

// Environ returns the system environment variables as a map
func Environ() (m map[string]string) {
m = make(map[string]string)
for _, item := range os.Environ() {
splits := strings.SplitN(item, "=", 2)
var key, val string
if len(splits) > 0 {
key = splits[0]
if len(splits) > 1 {
val = splits[1]
}
m[key] = val
}
}
return
}
12 changes: 12 additions & 0 deletions internal/menv/environ_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package menv

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestEnviron(t *testing.T) {
m := Environ()
require.NotEmpty(t, m["SHELL"])
}
8 changes: 0 additions & 8 deletions internal/menv/os_inject.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/mexec/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (m *manager) Execute(opts ExecuteOptions) (err error) {

// build env
var env map[string]string
if env, err = menv.Construct(opts.Env); err != nil {
if env, err = menv.Construct(menv.Environ(), opts.Env); err != nil {
err = errors.New("failed constructing environment variables: " + err.Error())
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/mrunners/runner_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (r *actionRender) Do(ctx context.Context) (err error) {

var env map[string]string

if env, err = menv.Construct(r.Unit.Env); err != nil {
if env, err = menv.Construct(menv.Environ(), r.Unit.Env); err != nil {
err = r.PanicOnCritical("failed constructing environments variables", err)
return
}
Expand Down
45 changes: 22 additions & 23 deletions internal/munit/loader.go → internal/munit/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package munit

import (
"errors"
"os"
"regexp"
"strconv"
"strings"
)

var (
Expand All @@ -18,26 +18,13 @@ const (
PrefixKind = "&"
)

type Loader struct {
filter *Filter
}

func NewLoader() (ld *Loader) {
return &Loader{
filter: NewFilter(
strings.TrimSpace(osGetenv("MINIT_ENABLE")),
strings.TrimSpace(osGetenv("MINIT_DISABLE")),
),
}
}

type LoadOptions struct {
Args []string
Env bool
Env map[string]string
Dir string
}

func (ld *Loader) Load(opts LoadOptions) (output []Unit, skipped []Unit, err error) {
func Load(opts LoadOptions) (output []Unit, skipped []Unit, err error) {
var units []Unit

// load units
Expand All @@ -48,6 +35,7 @@ func (ld *Loader) Load(opts LoadOptions) (output []Unit, skipped []Unit, err err
}
units = append(units, dUnits...)
}

if len(opts.Args) > 0 {
var unit Unit
var ok bool
Expand All @@ -58,26 +46,35 @@ func (ld *Loader) Load(opts LoadOptions) (output []Unit, skipped []Unit, err err
units = append(units, unit)
}
}
if opts.Env {

filter := NewFilter("", "")

if opts.Env != nil {

filter = NewFilter(
opts.Env["MINIT_ENABLE"],
opts.Env["MINIT_DISABLE"],
)

{
// legacy minit main
var (
unit Unit
ok bool
)
if unit, ok, err = LoadEnv(); err != nil {
if unit, ok, err = LoadEnv(opts.Env); err != nil {
return
} else if ok {
units = append(units, unit)
}
}

for _, infix := range DetectEnvInfixes() {
for _, infix := range DetectEnvInfixes(opts.Env) {
var (
unit Unit
ok bool
)
if unit, ok, err = LoadEnvWithInfix(infix); err != nil {
if unit, ok, err = LoadEnvWithInfix(opts.Env, infix); err != nil {
return
}
if ok {
Expand Down Expand Up @@ -120,14 +117,16 @@ func (ld *Loader) Load(opts LoadOptions) (output []Unit, skipped []Unit, err err
}

// skip if needed
if !ld.filter.Match(unit) {
if !filter.Match(unit) {
skipped = append(skipped, unit)
continue
}

// eval cron
if unit.Cron != "" {
unit.Cron = osExpandEnv(unit.Cron)
if unit.Cron != "" && opts.Env != nil {
unit.Cron = os.Expand(unit.Cron, func(s string) string {
return opts.Env[s]
})
}

// replicas
Expand Down
Loading

0 comments on commit af8298c

Please sign in to comment.