github.com/getgauge/gauge@v1.6.9/util/util.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package util 8 9 import ( 10 "fmt" 11 "os" 12 "path/filepath" 13 "runtime" 14 "syscall" 15 16 "strings" 17 18 "github.com/getgauge/common" 19 "github.com/getgauge/gauge/env" 20 "github.com/getgauge/gauge/logger" 21 ) 22 23 // NumberOfCores returns the number of CPU cores on the system 24 func NumberOfCores() int { 25 return runtime.NumCPU() 26 } 27 28 // IsWindows returns if Gauge is running on Windows 29 func IsWindows() bool { 30 return runtime.GOOS == "windows" 31 } 32 33 // DownloadAndUnzip downloads the zip file from given download link and unzips it. 34 // Returns the unzipped file path. 35 func DownloadAndUnzip(downloadLink, tempDir string) (string, error) { 36 logger.Debugf(true, "Download URL %s", downloadLink) 37 downloadedFile, err := Download(downloadLink, tempDir, "", false) 38 if err != nil { 39 return "", err 40 } 41 logger.Debugf(true, "Downloaded to %s", downloadedFile) 42 43 unzippedPluginDir, err := common.UnzipArchive(downloadedFile, tempDir) 44 if err != nil { 45 return "", fmt.Errorf("failed to unzip file %s: %s", downloadedFile, err.Error()) 46 } 47 logger.Debugf(true, "Unzipped to => %s", unzippedPluginDir) 48 return unzippedPluginDir, nil 49 } 50 51 // IsProcessRunning checks if the process with the given process id is still running 52 func IsProcessRunning(pid int) bool { 53 process, err := os.FindProcess(pid) 54 if err != nil { 55 return false 56 } 57 58 if !IsWindows() { 59 return process.Signal(syscall.Signal(0)) == nil 60 } 61 62 processState, err := process.Wait() 63 if err != nil { 64 return false 65 } 66 if processState.Exited() { 67 return false 68 } 69 70 return true 71 } 72 73 // SetWorkingDir sets the current working directory to specified location 74 func SetWorkingDir(workingDir string) { 75 targetDir, err := filepath.Abs(workingDir) 76 if err != nil { 77 logger.Fatalf(true, "Unable to set working directory : %s", err.Error()) 78 } 79 80 if !common.DirExists(targetDir) { 81 err = os.Mkdir(targetDir, 0750) 82 if err != nil { 83 logger.Fatalf(true, "Unable to set working directory : %s", err.Error()) 84 } 85 } 86 87 err = os.Chdir(targetDir) 88 if err != nil { 89 logger.Fatalf(true, "Unable to set working directory : %s", err.Error()) 90 } 91 92 _, err = os.Getwd() 93 if err != nil { 94 logger.Fatalf(true, "Unable to set working directory : %s", err.Error()) 95 } 96 } 97 98 // GetSpecDirs returns the specification directory. 99 // It checks whether the environment variable for gauge_specs_dir is set. 100 // It returns 'specs' otherwise 101 func GetSpecDirs() []string { 102 var specFromProperties = os.Getenv(env.SpecsDir) 103 if specFromProperties != "" { 104 var specDirectories = strings.Split(specFromProperties, ",") 105 for index, ele := range specDirectories { 106 specDirectories[index] = strings.TrimSpace(ele) 107 } 108 return specDirectories 109 } 110 return []string{common.SpecsDirectoryName} 111 } 112 113 // GetConceptsPaths returns the concepts directory. 114 // It checks whether the environment variable for gauge_concepts_dir is set. 115 // It returns an empty list (of directories) otherwise 116 func GetConceptsPaths() []string { 117 var conceptDirs []string 118 var conceptsDirFromProperties = os.Getenv(env.ConceptsDir) 119 if conceptsDirFromProperties != "" { 120 conceptDirs = strings.Split(conceptsDirFromProperties, ",") 121 for index, ele := range conceptDirs { 122 conceptDirs[index] = strings.TrimSpace(ele) 123 } 124 } 125 return conceptDirs 126 } 127 128 func ListContains(list []string, val string) bool { 129 for _, s := range list { 130 if s == val { 131 return true 132 } 133 } 134 return false 135 } 136 137 func GetFileContents(filepath string) (string, error) { 138 return common.ReadFileContents(GetPathToFile(filepath)) 139 }