Skip to content

Commit

Permalink
[RFE] close #173
Browse files Browse the repository at this point in the history
  • Loading branch information
gmeghnag committed Jan 5, 2025
1 parent 91bfe3c commit 2a41c2a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
55 changes: 55 additions & 0 deletions cmd/use/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package use
import (
"archive/tar"
"archive/zip"
"bufio"
"compress/gzip"
"fmt"
"io"
Expand Down Expand Up @@ -437,3 +438,57 @@ func extractTarXZ(xzFile string, destinationdir string) (string, error) {
}
return ExtractTarStream(xzReader, destinationdir)
}

func extractClientVersion(mustGatherLogsFilePath string) string {
filePath := mustGatherLogsFilePath
clientVersion := ""
// Open the file
file, err := os.Open(filePath)
if err != nil {
return ""
}
defer file.Close()

// Initialize a scanner to read the file line by line
scanner := bufio.NewScanner(file)

// Variable to store the matching line
var clientVersionLine string

// Counter for the first 20 lines
lineCount := 0

// Read the file line by line
for scanner.Scan() {
line := scanner.Text()
lineCount++

// Check if the line starts with "ClientVersion: "
if strings.HasPrefix(line, "ClientVersion: ") {
clientVersionLine = line
break // Exit the loop as we found the line
}

// Stop checking after 20 lines as it should be at line 4
if lineCount >= 20 {
break
}
}

// Handle potential scanning error
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
return ""
}

// Check if we found the line and print the result
if clientVersionLine != "" {
parts := strings.Split(clientVersionLine, ":")
if len(parts) == 2 {
// Trim spaces and get the version part
clientVersion = strings.TrimSpace(parts[1])
return clientVersion
}
}
return ""
}
41 changes: 37 additions & 4 deletions cmd/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"sigs.k8s.io/yaml"
)

var singleNamespaceInMustGather bool

func useContext(path string, omcConfigFile string, idFlag string) error {
if path != "" {
_path, err := findMustGatherIn(path)
Expand Down Expand Up @@ -82,6 +84,7 @@ func useContext(path string, omcConfigFile string, idFlag string) error {
if len(namespaces) == 1 {
NewContexts = append(NewContexts, types.Context{Id: ctxId, Path: path, Current: "*", Project: namespaces[0]})
vars.Namespace = namespaces[0]
singleNamespaceInMustGather = true
} else {
NewContexts = append(NewContexts, types.Context{Id: ctxId, Path: path, Current: "*", Project: defaultProject})
vars.Namespace = defaultProject
Expand Down Expand Up @@ -149,7 +152,12 @@ func findMustGatherIn(path string) (string, error) {
}

func MustGatherInfo() {
fmt.Printf("Must-Gather : %s\nProject : %s\n", vars.MustGatherRootPath, vars.Namespace)
fmt.Printf("Must-Gather : %s\n", vars.MustGatherRootPath)
if singleNamespaceInMustGather {
fmt.Printf("Project : %s (single project)\n", vars.Namespace)
} else {
fmt.Printf("Project : %s\n", vars.Namespace)
}
InfrastrctureFilePathExists, _ := helpers.Exists(vars.MustGatherRootPath + "/cluster-scoped-resources/config.openshift.io/infrastructures.yaml")
if InfrastrctureFilePathExists {
_file, _ := os.ReadFile(vars.MustGatherRootPath + "/cluster-scoped-resources/config.openshift.io/infrastructures.yaml")
Expand All @@ -158,8 +166,8 @@ func MustGatherInfo() {
fmt.Println("Error when trying to unmarshal file: " + vars.MustGatherRootPath + "/cluster-scoped-resources/config.openshift.io/infrastructures.yaml")
os.Exit(1)
} else {
fmt.Printf("ApiServerURL : %s\n", infrastructureList.Items[0].Status.APIServerURL)
fmt.Printf("Platform : %s\n", infrastructureList.Items[0].Status.PlatformStatus.Type)
fmt.Printf("ApiServerURL : %s\n", infrastructureList.Items[0].Status.APIServerURL)
fmt.Printf("Platform : %s\n", infrastructureList.Items[0].Status.PlatformStatus.Type)
}
}
clusterversionFilePathExists, _ := helpers.Exists(vars.MustGatherRootPath + "/cluster-scoped-resources/config.openshift.io/clusterversions/version.yaml")
Expand All @@ -170,9 +178,34 @@ func MustGatherInfo() {
fmt.Println("Error when trying to unmarshal file: " + vars.MustGatherRootPath + "/cluster-scoped-resources/config.openshift.io/clusterversions/version.yaml")
os.Exit(1)
} else {
fmt.Printf("ClusterID : %s\n", ClusterVersion.Spec.ClusterID)
clusterversion := ""
versionHistory := ClusterVersion.Status.History
for _, version := range versionHistory {
if version.State == "Completed" {
clusterversion = version.Version
break
}
}

fmt.Printf("ClusterID : %s\n", ClusterVersion.Spec.ClusterID)
fmt.Printf("ClusterVersion : %s\n", clusterversion)
}
}
mustGatherSplitPath := strings.Split(vars.MustGatherRootPath, "/")
mustGatherParentPath := strings.Join(mustGatherSplitPath[0:(len(mustGatherSplitPath)-1)], "/")
clientVersion := extractClientVersion(mustGatherParentPath + "/must-gather.logs")
if clientVersion != "" {
fmt.Printf("ClientVersion : %s\n", clientVersion)
}
parts := strings.Split(vars.MustGatherRootPath, "/")
if len(parts) > 0 {
lastPart := parts[len(parts)-1]
if strings.Contains(lastPart, "-sha256") {
mustGatherImage := strings.Split(lastPart, "-sha256")[0]
fmt.Printf("Image : %s\n", mustGatherImage)
}
}

}

// useCmd represents the use command
Expand Down

0 comments on commit 2a41c2a

Please sign in to comment.