github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/cli/oc_version.go (about) 1 package cli 2 3 import ( 4 "strings" 5 ) 6 7 // openshiftVersion represents the client/server version pair. 8 type openshiftVersion struct { 9 client string 10 server string 11 } 12 13 // ExactMatch is true when client and server version are known and equal. 14 func (ov openshiftVersion) ExactMatch() bool { 15 return !ov.Incomplete() && ov.client == ov.server 16 } 17 18 // Incomplete returns true if at least one version could not be detected properly. 19 func (ov openshiftVersion) Incomplete() bool { 20 return ov.client == "?" || ov.server == "?" 21 } 22 23 // Get OC client and server version. See tests for example output of "oc version". 24 func ocVersion(ocClient OcClientVersioner) openshiftVersion { 25 ov := openshiftVersion{"?", "?"} 26 outBytes, errBytes, err := ocClient.Version() 27 if err != nil { 28 VerboseMsg("Failed to query client and server version, got:", string(errBytes)) 29 return ov 30 } 31 output := string(outBytes) 32 33 ocClientVersion := "" 34 ocServerVersion := "" 35 extractVersion := func(versionPart string) string { 36 ocVersionParts := strings.SplitN(versionPart, ".", 3) 37 return strings.Join(ocVersionParts[:len(ocVersionParts)-1], ".") 38 } 39 40 lines := strings.Split(strings.TrimSuffix(output, "\n"), "\n") 41 for _, line := range lines { 42 if len(line) > 0 { 43 parts := strings.SplitN(line, " ", 2) 44 if parts[0] == "oc" { 45 ocClientVersion = extractVersion(parts[1]) 46 } 47 if parts[0] == "openshift" { 48 ocServerVersion = extractVersion(parts[1]) 49 } 50 } 51 } 52 53 if len(ocClientVersion) > 0 && strings.Contains(ocClientVersion, ".") { 54 ov.client = ocClientVersion 55 } 56 if len(ocServerVersion) > 0 && strings.Contains(ocServerVersion, ".") { 57 ov.server = ocServerVersion 58 } 59 60 if ov.Incomplete() { 61 VerboseMsg("Client and server version could not be detected properly, got:", output) 62 return ov 63 } 64 return openshiftVersion{client: ocClientVersion, server: ocServerVersion} 65 }