From bcd92b87eea624f8cb2d78e5b2fb66adba66a9b2 Mon Sep 17 00:00:00 2001 From: VIKAS MATHUR <97077039+vmathur12@users.noreply.github.com> Date: Thu, 26 Jan 2023 19:32:19 +0530 Subject: [PATCH] Added db2level check before installing clidriver (#193) * Added db2level check before installing clidriver * Upated with db2cli validate check * Updated setup.go with include and lib folder check Co-authored-by: Vikas Mathur --- README.md | 2 +- installer/setup.go | 80 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 63992ff..da51628 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Interface for GoLang to DB2 for z/OS, DB2 for LUW, DB2 for i. ## Prerequisite -Golang should be installed(Golang version should be >=1.12.x and <= 1.18.X) +Golang should be installed(Golang version should be >=1.12.x and <= 1.19.X) Git should be installed in your system. diff --git a/installer/setup.go b/installer/setup.go index 199d729..261dc83 100644 --- a/installer/setup.go +++ b/installer/setup.go @@ -10,6 +10,7 @@ import ( "path/filepath" "runtime" "strings" + "bufio" ) func downloadFile(filepath string, url string) error { @@ -101,34 +102,93 @@ func aix_untar(clidriver string, targetDirectory string) error { return nil } +func getinstalledpath(validateout string) { + var line string + + scanner := bufio.NewScanner(strings.NewReader(validateout)) + + for scanner.Scan() { + line = scanner.Text() + + if(strings.Contains(line, "Install")) { + fields := strings.Split(line, " ") + fmt.Println(fields[7]) + input1 := fields[7][0:len(fields[7])] + fmt.Println("Clidriver is already present") + fmt.Println("Please set IBM_DB_HOME to ", input1) + } + } +} + +func checkincludepath( includepath string) bool { + + if _, err1 := os.Stat(includepath + "/include"); !os.IsNotExist(err1) { + //fmt.Println("clidriver/include folder exists in the path") + if _, err2 := os.Stat(includepath + "/lib"); !os.IsNotExist(err2) { + //fmt.Println("clidriver/lib folder exists in the path") + return true + } + } + + return false +} + func main() { var target, cliFileName string var unpackageType int + var err11 error + var out []byte fmt.Println("NOTE: Environment variable DB2HOME name is changed to IBM_DB_HOME.") - value, ok := os.LookupEnv("IBM_DB_HOME") - if !ok { - if runtime.GOOS == "windows" { - fmt.Println("Please set IBM_DB_HOME and add this path to PATH environment variable") - os.Exit(1) - } else { - fmt.Println("Please set IBM_DB_HOME, CGO_CFLAGS, CGO_LDFLAGS and LD_LIBRARY_PATH or DYLD_LIBRARY_PATH environment variables", value) + + out, err11 = exec.Command("db2cli", "validate").Output() + if err11 != nil { + _, ok := os.LookupEnv("IBM_DB_HOME") + if !ok { + if runtime.GOOS == "windows" { + fmt.Println("Please set IBM_DB_HOME add this path to PATH environment variable after clidriver installed") + } else { + fmt.Println("Please set IBM_DB_HOME, CGO_CFLAGS, CGO_LDFLAGS and LD_LIBRARY_PATH or DYLD_LIBRARY_PATH environment variables after clidriver installed") + } + } + }else { + path, ok := os.LookupEnv("IBM_DB_HOME") + if !ok { + //set IBM_DB_HOME + getinstalledpath(string(out)) os.Exit(1) + }else{ + fmt.Println("clidriver folder exists in the path....", path) + if checkincludepath(path) { + os.Exit(1) + } } } - if len(os.Args) == 2 { target = os.Args[1] } else { target = "./../.." } - if _, err := os.Stat(target + "/clidriver"); !os.IsNotExist(err) { + if _, err1 := os.Stat(target + "/clidriver"); !os.IsNotExist(err1) { fmt.Println("clidriver folder exists in the path") - os.Exit(2) + + if _, err2 := os.Stat(target + "/clidriver/include"); !os.IsNotExist(err2) { + //fmt.Println("clidriver/include folder exists in the path") + + if _, err3 := os.Stat(target + "/clidriver/lib"); !os.IsNotExist(err3) { + //fmt.Println("clidriver/lib folder exists in the path") + os.Exit(2) + } else { + fmt.Println("clidriver/lib folder is not exist, installing clidriver ....") + } + } else { + fmt.Println("clidriver/include folder is not exist, installing clidriver ....") + } } + if runtime.GOOS == "windows" { unpackageType = 1 const wordsize = 32 << (^uint(0) >> 32 & 1)