github.com/angryronald/go-kit@v0.0.0-20240505173814-ff2bd9c79dbf/file/helper.go (about)

     1  package file
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  )
     7  
     8  var ErrProjectDirectoryNotFound = errors.New("project directory not found")
     9  
    10  func firstIndexOfString(source string, value string) int {
    11  	for i := 0; i+len(value) <= len(source); i++ {
    12  		if source[i:i+len(value)] == value {
    13  			return i
    14  		}
    15  	}
    16  	return -1
    17  }
    18  
    19  func GetProjectDir(projectName string) (string, error) {
    20  	currentWorkingDir, err := os.Getwd()
    21  	if err != nil {
    22  		return "", err
    23  	}
    24  
    25  	index := firstIndexOfString(currentWorkingDir, projectName)
    26  	if index == -1 {
    27  		return "", ErrProjectDirectoryNotFound
    28  	}
    29  
    30  	return currentWorkingDir[:index+len(projectName)], nil
    31  }