-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use get resource format to define commands
- Loading branch information
1 parent
d6bf47f
commit 1f187a1
Showing
10 changed files
with
141 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cmd | ||
|
||
import ( | ||
"cli/cmd/pod" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var getCmd = &cobra.Command{ | ||
Use: "get [command]", | ||
Short: "get resource", | ||
Long: "get resources for pods", | ||
} | ||
|
||
func init() { | ||
getCmd.AddCommand(pod.GetPodCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package pod | ||
|
||
import ( | ||
"cli/api" | ||
"cli/table" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/olekukonko/tablewriter" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var AllFields bool | ||
|
||
var GetPodCmd = &cobra.Command{ | ||
Use: "pod [podId]", | ||
Aliases: []string{"pods"}, | ||
Args: cobra.MaximumNArgs(1), | ||
Short: "get all pods", | ||
Long: "get all pods or specify pod id", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
pods, err := api.QueryPods() | ||
cobra.CheckErr(err) | ||
|
||
fmt.Println(AllFields) | ||
|
||
data := make([][]string, len(pods)) | ||
for i, p := range pods { | ||
if len(args) == 1 && p.Id != strings.ToLower(args[0]) { | ||
continue | ||
} | ||
row := []string{p.Id, p.Name, fmt.Sprintf("%d %s", p.GpuCount, p.Machine.GpuDisplayName), p.ImageName, p.DesiredStatus} | ||
if AllFields { | ||
row = append( | ||
row, | ||
p.PodType, | ||
fmt.Sprintf("%d", p.VcpuCount), | ||
fmt.Sprintf("%d", p.MemoryInGb), | ||
fmt.Sprintf("%d", p.ContainerDiskInGb), | ||
fmt.Sprintf("%d", p.VolumeInGb), | ||
fmt.Sprintf("%.3f", p.CostPerHr), | ||
) | ||
} | ||
data[i] = row | ||
} | ||
|
||
header := []string{"ID", "Name", "GPU", "Image Name", "Status"} | ||
if AllFields { | ||
header = append(header, "Pod Type", "vCPU", "Mem", "Container Disk", "Volume Disk", "$/hr") | ||
} | ||
|
||
tb := tablewriter.NewWriter(os.Stdout) | ||
tb.SetHeader(header) | ||
tb.AppendBulk(data) | ||
table.Defaults(tb) | ||
tb.Render() | ||
}, | ||
} | ||
|
||
func init() { | ||
GetPodCmd.Flags().BoolVarP(&AllFields, "allfields", "a", false, "include all fields in output") | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package table | ||
|
||
import ( | ||
"github.com/olekukonko/tablewriter" | ||
) | ||
|
||
func Defaults(table *tablewriter.Table) { | ||
table.SetAutoWrapText(false) | ||
table.SetAutoFormatHeaders(true) | ||
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) | ||
table.SetAlignment(tablewriter.ALIGN_LEFT) | ||
table.SetCenterSeparator("") | ||
table.SetColumnSeparator("") | ||
table.SetRowSeparator("") | ||
table.SetHeaderLine(false) | ||
table.SetBorder(false) | ||
table.SetTablePadding("\t") | ||
table.SetNoWhiteSpace(true) | ||
} |