github.com/wawandco/oxpecker-plugins@v0.1.1/internal/info/info.go (about)

     1  package info
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     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 := ioutil.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
    33  func ModuleName() (string, error) {
    34  	content, err := ioutil.ReadFile("go.mod")
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  
    39  	path := modfile.ModulePath(content)
    40  	return path, nil
    41  }