Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vkctl job list --all-namespaces show ns #336

Merged
merged 1 commit into from
Jul 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions pkg/cli/job/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const (
RetryCount string = "RetryCount"
// JobType job type
JobType string = "JobType"
// Namespace job namespace
Namespace string = "Namespace"
)

var listJobFlags = &listFlags{}
Expand Down Expand Up @@ -111,9 +113,19 @@ func ListJobs() error {

// PrintJobs prints all jobs details
func PrintJobs(jobs *v1alpha1.JobList, writer io.Writer) {
maxNameLen := getMaxNameLen(jobs)
_, err := fmt.Fprintf(writer, fmt.Sprintf("%%-%ds%%-25s%%-12s%%-12s%%-12s%%-6s%%-10s%%-10s%%-12s%%-10s%%-12s%%-10s\n", maxNameLen),
Name, Creation, Phase, JobType, Replicas, Min, Pending, Running, Succeeded, Failed, Unknown, RetryCount)
maxLenInfo := getMaxLen(jobs)

titleFormat := "%%-%ds%%-25s%%-12s%%-12s%%-12s%%-6s%%-10s%%-10s%%-12s%%-10s%%-12s%%-10s\n"
contentFormat := "%%-%ds%%-25s%%-12s%%-12s%%-12d%%-6d%%-10d%%-10d%%-12d%%-10d%%-12d%%-10d\n"

var err error
if listJobFlags.allNamespace {
_, err = fmt.Fprintf(writer, fmt.Sprintf("%%-%ds"+titleFormat, maxLenInfo[1], maxLenInfo[0]),
Namespace, Name, Creation, Phase, JobType, Replicas, Min, Pending, Running, Succeeded, Failed, Unknown, RetryCount)
} else {
_, err = fmt.Fprintf(writer, fmt.Sprintf(titleFormat, maxLenInfo[0]),
Name, Creation, Phase, JobType, Replicas, Min, Pending, Running, Succeeded, Failed, Unknown, RetryCount)
}
if err != nil {
fmt.Printf("Failed to print list command result: %s.\n", err)
}
Expand All @@ -133,22 +145,33 @@ func PrintJobs(jobs *v1alpha1.JobList, writer io.Writer) {
if jobType == "" {
jobType = "Batch"
}
_, err = fmt.Fprintf(writer, fmt.Sprintf("%%-%ds%%-25s%%-12s%%-12s%%-12d%%-6d%%-10d%%-10d%%-12d%%-10d%%-12d%%-10d\n", maxNameLen),
job.Name, job.CreationTimestamp.Format("2006-01-02 15:04:05"), job.Status.State.Phase, jobType, replicas,
job.Status.MinAvailable, job.Status.Pending, job.Status.Running, job.Status.Succeeded, job.Status.Failed, job.Status.Unknown, job.Status.RetryCount)

if listJobFlags.allNamespace {
_, err = fmt.Fprintf(writer, fmt.Sprintf("%%-%ds"+contentFormat, maxLenInfo[1], maxLenInfo[0]),
job.Namespace, job.Name, job.CreationTimestamp.Format("2006-01-02 15:04:05"), job.Status.State.Phase, jobType, replicas,
job.Status.MinAvailable, job.Status.Pending, job.Status.Running, job.Status.Succeeded, job.Status.Failed, job.Status.Unknown, job.Status.RetryCount)
} else {
_, err = fmt.Fprintf(writer, fmt.Sprintf(contentFormat, maxLenInfo[0]),
job.Name, job.CreationTimestamp.Format("2006-01-02 15:04:05"), job.Status.State.Phase, jobType, replicas,
job.Status.MinAvailable, job.Status.Pending, job.Status.Running, job.Status.Succeeded, job.Status.Failed, job.Status.Unknown, job.Status.RetryCount)
}
if err != nil {
fmt.Printf("Failed to print list command result: %s.\n", err)
}
}
}

func getMaxNameLen(jobs *v1alpha1.JobList) int {
maxLen := len(Name)
func getMaxLen(jobs *v1alpha1.JobList) []int {
maxNameLen := len(Name)
maxNamespaceLen := len(Namespace)
for _, job := range jobs.Items {
if len(job.Name) > maxLen {
maxLen = len(job.Name)
if len(job.Name) > maxNameLen {
maxNameLen = len(job.Name)
}
if len(job.Namespace) > maxNamespaceLen {
maxNamespaceLen = len(job.Namespace)
}
}

return maxLen + 3
return []int{maxNameLen + 3, maxNamespaceLen + 3}
}