github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/common/checks.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package common 21 22 import ( 23 "context" 24 "fmt" 25 "os" 26 "path/filepath" 27 "regexp" 28 "strconv" 29 "strings" 30 31 "github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata" 32 "github.com/docker/docker/api/types" 33 "github.com/docker/docker/client" 34 ) 35 36 func CheckJavaDependencies() error { 37 fmt.Println("✅ Checking dependencies...") 38 if err := checkJava(); err != nil { 39 return fmt.Errorf("%w", err) 40 } 41 if err := checkMaven(); err != nil { 42 return fmt.Errorf("%w", err) 43 } 44 return nil 45 } 46 47 func checkJava() error { 48 javaCheck := ExecCommand("java", "-version") 49 version, err := javaCheck.CombinedOutput() 50 if err != nil { 51 fmt.Println("ERROR: Java not found") 52 fmt.Printf("At least Java %.2d is required to use this command\n", metadata.JavaVersion) 53 return err 54 } 55 userJavaVersion, err := parseJavaVersion(string(version)) 56 if err != nil { 57 return fmt.Errorf("error while parsing Java version: %w", err) 58 } 59 60 if userJavaVersion < metadata.JavaVersion { 61 fmt.Printf("ERROR: Please make sure you are using Java version %.2d or later", metadata.JavaVersion) 62 fmt.Println("Installation stopped. Please upgrade Java and run again") 63 os.Exit(1) 64 } else { 65 fmt.Println(" - Java version check.") 66 } 67 return nil 68 } 69 70 func checkMaven() error { 71 mavenCheck := ExecCommand("mvn", "--version") 72 version, err := mavenCheck.CombinedOutput() 73 if err != nil { 74 fmt.Println("ERROR: Maven not found") 75 fmt.Printf("At least Maven %.2d.%.2d.1 is required to use this command\n", metadata.MavenMajorVersion, metadata.MavenMinorVersion) 76 return err 77 } 78 major, minor, err := parseMavenVersion(string(version)) 79 if err != nil { 80 return fmt.Errorf("error while parsing Maven version: %w", err) 81 } 82 83 if major < metadata.MavenMajorVersion && minor < metadata.MavenMinorVersion { 84 fmt.Printf("ERROR: Please make sure you are using Maven version %d.%d.1 or later", major, minor) 85 fmt.Println("Installation stopped. Please upgrade Maven and run again") 86 os.Exit(1) 87 } else { 88 fmt.Println(" - Maven version check.") 89 } 90 91 return nil 92 } 93 94 func CheckDocker() error { 95 fmt.Println("✅ Checking if Docker is available...") 96 cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) 97 if err != nil { 98 fmt.Println("Error creating docker client") 99 return err 100 } 101 _, err = cli.ContainerList(context.Background(), types.ContainerListOptions{}) 102 if err != nil { 103 fmt.Println("ERROR: Docker not found.") 104 fmt.Println("Download from https://docs.docker.com/get-docker/") 105 fmt.Println("If it's already installed, check if the docker daemon is running") 106 return err 107 } 108 fmt.Println(" - Docker is running") 109 return nil 110 } 111 112 func CheckPodman() error { 113 fmt.Println("✅ Checking if Podman is available...") 114 dockerCheck := ExecCommand("podman", "stats", "--no-stream") 115 if err := dockerCheck.Run(); err != nil { 116 fmt.Println("ERROR: Podman not found.") 117 fmt.Println("Download from https://docs.podman.io/en/latest/") 118 fmt.Println("If it's already installed, check if the podman daemon is running") 119 return err 120 } 121 122 fmt.Println(" - Podman is running") 123 return nil 124 } 125 126 func parseJavaVersion(version string) (int64, error) { 127 dotVersion := strings.Split(strings.Split(version, "\"")[1], ".") 128 intVersion, err := strconv.ParseInt(dotVersion[0], 10, 8) 129 if err != nil { 130 return 0, err 131 } 132 return intVersion, nil 133 } 134 135 func parseMavenVersion(version string) (int64, int64, error) { 136 stringVersion := strings.Split(version, " ")[2] 137 dotVersion := strings.Split(stringVersion, ".") 138 majorVersion, err := strconv.ParseInt(dotVersion[0], 10, 8) 139 if err != nil { 140 return 0, 0, err 141 } 142 minorVersion, err := strconv.ParseInt(dotVersion[1], 10, 8) 143 if err != nil { 144 return 0, 0, err 145 } 146 return majorVersion, minorVersion, nil 147 } 148 149 func CheckIfDirExists(dirName string) (bool, error) { 150 _, err := FS.Stat(fmt.Sprintf("./%s", dirName)) 151 if err == nil { 152 return true, nil 153 } 154 if os.IsNotExist(err) { 155 return false, nil 156 } 157 return false, err 158 } 159 160 func IsQuarkusSonataFlowProject() bool { 161 if fileExists("pom.xml") { 162 return true 163 } 164 return false 165 } 166 167 func IsSonataFlowProject() bool { 168 if anyFileExists("*.sw.*") { 169 return true 170 } 171 return false 172 } 173 174 func fileExists(filename string) bool { 175 _, err := os.Stat(filename) 176 return !os.IsNotExist(err) 177 } 178 179 func anyFileExists(extension string) bool { 180 matches, err := filepath.Glob(extension) 181 if err != nil { 182 return false 183 } 184 185 if len(matches) > 0 { 186 return true 187 } 188 return false 189 } 190 191 func CheckProjectName(name string) (err error) { 192 matched, err := regexp.MatchString(`^([_\-\.a-zA-Z0-9]+)$`, name) 193 if !matched { 194 fmt.Printf("The project name (\"%s\") contains invalid characters. Valid characters are alphanumeric (A-Za-z), underscore, dash and dot.", name) 195 err = fmt.Errorf("invalid project name") 196 197 } 198 return 199 }