github.com/mhlo/force@v0.22.28-0.20150915022417-6d05ecfb0b47/config.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/mhlo/config"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	//"fmt"
     9  ) 
    10  
    11  var Config = config.NewConfig("force")
    12  
    13  func GetSourceDir() (src string, err error) {
    14  	// Last element is default
    15  	var sourceDirs = []string{
    16  		//"src",
    17  		"metadata",
    18  	}
    19  
    20  	wd, err := os.Getwd()
    21  
    22  	err = nil
    23  	for _, src = range sourceDirs {
    24  		if strings.Contains(wd, src) {
    25  			// our working directory contains a src dir above us, we need to move up the file syste
    26  			nsrc := wd
    27  			for {
    28  				nsrc = filepath.Dir(nsrc)
    29  				if filepath.Base(nsrc) == src {
    30  					src = nsrc
    31  					return
    32  				}
    33  			}
    34  		} else {
    35  			_, err = os.Stat(filepath.Join(wd, src)) //, "package.xml"))
    36  			// Found a real source dir
    37  			if err == nil {
    38  				return
    39  			}
    40  		}
    41  	}
    42  
    43  	return
    44  }
    45  
    46  func ExitIfNoSourceDir(err error) {
    47  	if err != nil {
    48  		if os.IsNotExist(err) {
    49  			ErrorAndExit("Current directory does not contain a metadata or src directory")
    50  		}
    51  
    52  		ErrorAndExit(err.Error())
    53  	}
    54  }