diff --git a/internal/munit/load_file.go b/internal/munit/load_file.go index d6072f6..259e009 100644 --- a/internal/munit/load_file.go +++ b/internal/munit/load_file.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "sort" + "strings" "gopkg.in/yaml.v3" ) @@ -56,3 +57,36 @@ func LoadDir(dir string) (units []Unit, err error) { } return } + +const ( + unitDirNone = "none" +) + +func ParseUnitDirPattern(pattern string) (dirs []string) { +outerLoop: + for _, dir := range strings.Split(pattern, ":") { + dir = strings.TrimSpace(dir) + + if dir == "" { + continue + } + + if dir == unitDirNone { + continue + } + + if err := os.MkdirAll(dir, 0755); err != nil { + continue + } + + for _, existed := range dirs { + if existed == dir { + continue outerLoop + } + } + + dirs = append(dirs, dir) + } + + return +} diff --git a/internal/munit/load_test.go b/internal/munit/load_test.go index 8c11fbb..fe183db 100644 --- a/internal/munit/load_test.go +++ b/internal/munit/load_test.go @@ -15,8 +15,8 @@ func TestLoad(t *testing.T) { } units, skipped, err := Load(LoadOptions{ - Dir: "testdata", - Env: m, + Dirs: []string{"testdata"}, + Env: m, }) require.NoError(t, err) @@ -134,3 +134,8 @@ func TestDupOrMakeMap(t *testing.T) { require.Equal(t, "d", m1a["c"]) require.Equal(t, "", m1b["c"]) } + +func TestParseUnitDirPattern(t *testing.T) { + require.Equal(t, []string{"testdata/a", "testdata/b", "testdata/c"}, ParseUnitDirPattern("testdata/a:testdata/b:testdata/c")) + require.Equal(t, []string{"testdata/a", "testdata/b", "testdata/c"}, ParseUnitDirPattern("::none: testdata/a:testdata/b :testdata/c:/sys/not-possible")) +} diff --git a/main.go b/main.go index dd9cbab..a2acade 100644 --- a/main.go +++ b/main.go @@ -83,18 +83,6 @@ func main() { // unit dir, be careful, empty string should be treated as current directory envStr("MINIT_UNIT_DIR", &optUnitDir) - var unitDirs []string - - for _, _dir := range strings.Split(optUnitDir, ":") { - dir := strings.TrimSpace(_dir) - - if mkdirUnlessNone(&dir); dir == dirNone { - continue - } - - unitDirs = append(unitDirs, dir) - } - // log dir envStr("MINIT_LOG_DIR", &optLogDir) mkdirUnlessNone(&optLogDir) @@ -131,7 +119,7 @@ func main() { munit.LoadOptions{ Args: os.Args[1:], Env: menv.Environ(), - Dirs: unitDirs, + Dirs: munit.ParseUnitDirPattern(optUnitDir), }, ), )