github.com/xyproto/orbiton/v2@v2.65.12-0.20240516144430-e10a419274ec/platforms.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  	"strings"
     7  
     8  	"github.com/xyproto/env/v2"
     9  	"github.com/xyproto/files"
    10  )
    11  
    12  var (
    13  	isDarwinCache *bool
    14  	isLinuxCache  *bool
    15  )
    16  
    17  // isDarwin checks if the current OS is Darwin, and caches the result
    18  func isDarwin() bool {
    19  	if isDarwinCache != nil {
    20  		return *isDarwinCache
    21  	}
    22  	b := runtime.GOOS == "darwin"
    23  	isDarwinCache = &b
    24  	return b
    25  }
    26  
    27  // isLinux checks if the current OS is Linux, and caches the result
    28  func isLinux() bool {
    29  	if isLinuxCache != nil {
    30  		return *isLinuxCache
    31  	}
    32  	b := runtime.GOOS == "linux"
    33  	isLinuxCache = &b
    34  	return b
    35  }
    36  
    37  // getFullName tries to find the full name of the current user
    38  func getFullName() (fullName string) {
    39  	// Start out with whatever is in $LOGNAME, then capitalize the words
    40  	fullName = capitalizeWords(env.Str("LOGNAME", "name"))
    41  	// Then look for ~/.gitconfig
    42  	gitConfigFilename := env.ExpandUser("~/.gitconfig")
    43  	if files.Exists(gitConfigFilename) {
    44  		data, err := os.ReadFile(gitConfigFilename)
    45  		if err != nil {
    46  			return fullName
    47  		}
    48  		// Look for a line starting with "name =", in the "[user]" section
    49  		inUserSection := false
    50  		for _, line := range strings.Split(string(data), "\n") {
    51  			trimmedLine := strings.TrimSpace(line)
    52  			if trimmedLine == "[user]" {
    53  				inUserSection = true
    54  				continue
    55  			} else if strings.HasPrefix(trimmedLine, "[") {
    56  				inUserSection = false
    57  				continue
    58  			}
    59  			if inUserSection && strings.HasPrefix(trimmedLine, "name =") {
    60  				foundName := strings.TrimSpace(strings.SplitN(trimmedLine, "name =", 2)[1])
    61  				if len(foundName) > len(fullName) {
    62  					fullName = foundName
    63  				}
    64  			}
    65  		}
    66  	}
    67  	return fullName
    68  }