github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/internal/info/info.go (about)

     1  package info
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"golang.org/x/mod/modfile"
     9  )
    10  
    11  var ErrModuleNameNotFound = errors.New("module name not found")
    12  
    13  // BuildName extracts the last part of the module by splitting on `/`
    14  // this last part is useful for name of the binary and other things.
    15  func BuildName() (string, error) {
    16  	content, err := os.ReadFile("go.mod")
    17  	if err != nil {
    18  		return "", err
    19  	}
    20  
    21  	path := modfile.ModulePath(content)
    22  	name := filepath.Base(path)
    23  
    24  	if name == "." {
    25  		return "", ErrModuleNameNotFound
    26  	}
    27  
    28  	return name, nil
    29  }
    30  
    31  // ModuleName returns the full module name
    32  // from go.mod, it returns empty if there is
    33  // an issue reading the go.mod
    34  func ModuleName() string {
    35  	content, err := os.ReadFile("go.mod")
    36  	if err != nil {
    37  		return ""
    38  	}
    39  
    40  	path := modfile.ModulePath(content)
    41  	return path
    42  }