Skip to content

Commit

Permalink
internal/mod: init
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Jul 16, 2023
1 parent 76f8a37 commit e769a4a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions internal/mod/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mod

import (
"os/exec"
"strings"
)

// Root contains the absolute path of the module root. Its value is empty if
// used outside of a module.
var Root string

func init() {
stdout, _ := exec.Command("go", "env", "GOMOD").Output()

var ok bool
Root, ok = strings.CutSuffix(strings.TrimSpace(string(stdout)), "/go.mod")
if !ok {
Root = ""
}
}
14 changes: 14 additions & 0 deletions internal/mod/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mod_test

import (
"strings"
"testing"

"github.com/lmittmann/w3/internal/mod"
)

func TestModRoot(t *testing.T) {
if !strings.HasSuffix(mod.Root, "w3") {
t.Fatalf("Unexpected module root: %q", mod.Root)
}
}

0 comments on commit e769a4a

Please sign in to comment.