github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/util/util.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package util
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"runtime"
    25  	"syscall"
    26  
    27  	"strconv"
    28  	"strings"
    29  
    30  	"github.com/getgauge/common"
    31  	"github.com/getgauge/gauge/logger"
    32  )
    33  
    34  // NumberOfCores returns the number of CPU cores on the system
    35  func NumberOfCores() int {
    36  	return runtime.NumCPU()
    37  }
    38  
    39  // IsWindows returns if Gauge is running on Windows
    40  func IsWindows() bool {
    41  	return runtime.GOOS == "windows"
    42  }
    43  
    44  // DownloadAndUnzip downloads the zip file from given download link and unzips it.
    45  // Returns the unzipped file path.
    46  func DownloadAndUnzip(downloadLink string, tempDir string) (string, error) {
    47  	logger.Infof("Downloading %s", filepath.Base(downloadLink))
    48  	logger.Debugf("Download URL %s", downloadLink)
    49  	downloadedFile, err := Download(downloadLink, tempDir, "", false)
    50  	if err != nil {
    51  		return "", err
    52  	}
    53  	logger.Debugf("Downloaded to %s", downloadedFile)
    54  
    55  	unzippedPluginDir, err := common.UnzipArchive(downloadedFile, tempDir)
    56  	if err != nil {
    57  		return "", fmt.Errorf("Failed to Unzip file %s: %s", downloadedFile, err.Error())
    58  	}
    59  	logger.Debugf("Unzipped to => %s\n", unzippedPluginDir)
    60  
    61  	return unzippedPluginDir, nil
    62  }
    63  
    64  // IsProcessRunning checks if the process with the given process id is still running
    65  func IsProcessRunning(pid int) bool {
    66  	process, err := os.FindProcess(pid)
    67  	if err != nil {
    68  		return false
    69  	}
    70  
    71  	if !IsWindows() {
    72  		return process.Signal(syscall.Signal(0)) == nil
    73  	}
    74  
    75  	processState, err := process.Wait()
    76  	if err != nil {
    77  		return false
    78  	}
    79  	if processState.Exited() {
    80  		return false
    81  	}
    82  
    83  	return true
    84  }
    85  
    86  // SetWorkingDir sets the current working directory to specified location
    87  func SetWorkingDir(workingDir string) {
    88  	targetDir, err := filepath.Abs(workingDir)
    89  	if err != nil {
    90  		logger.Fatalf("Unable to set working directory : %s", err.Error())
    91  	}
    92  
    93  	if !common.DirExists(targetDir) {
    94  		err = os.Mkdir(targetDir, 0777)
    95  		if err != nil {
    96  			logger.Fatalf("Unable to set working directory : %s", err.Error())
    97  		}
    98  	}
    99  
   100  	err = os.Chdir(targetDir)
   101  	if err != nil {
   102  		logger.Fatalf("Unable to set working directory : %s", err.Error())
   103  	}
   104  
   105  	_, err = os.Getwd()
   106  	if err != nil {
   107  		logger.Fatalf("Unable to set working directory : %s", err.Error())
   108  	}
   109  }
   110  
   111  func ConvertToBool(value, property string, defaultValue bool) bool {
   112  	boolValue, err := strconv.ParseBool(strings.TrimSpace(value))
   113  	if err != nil {
   114  		logger.Warningf("Incorrect value for %s in property file. Cannot convert %s to boolean.", property, value)
   115  		logger.Warningf("Using default value %v for property %s.", defaultValue, property)
   116  		return defaultValue
   117  	}
   118  	return boolValue
   119  }