Skip to content

Commit

Permalink
feat: use get resource format to define commands
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-singh committed Apr 9, 2022
1 parent d6bf47f commit 1f187a1
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 71 deletions.
45 changes: 28 additions & 17 deletions api/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ type MySelfData struct {
Pods []*Pod
}
type Pod struct {
Id string
Name string
ImageName string
DesiredStatus string
Machine *Machine
Id string
ContainerDiskInGb int
CostPerHr float32
DesiredStatus string
DockerArgs string
Env []string
GpuCount int
ImageName string
MemoryInGb int
Name string
PodType string
Ports string
VcpuCount int
VolumeInGb int
VolumeMountPath string
Machine *Machine
}
type Machine struct {
GpuDisplayName string
Expand All @@ -37,25 +48,25 @@ func QueryPods() (pods []*Pod, err error) {
myself {
pods {
id
machineId
name
dockerId
containerDiskInGb
costPerHr
desiredStatus
dockerArgs
dockerId
env
gpuCount
imageName
lastStatusChange
machineId
memoryInGb
name
podType
port
ports
podType
gpuCount
uptimeSeconds
vcpuCount
containerDiskInGb
memoryInGb
volumeInGb
volumeMountPath
desiredStatus
uptimeSeconds
costPerHr
env
lastStatusChange
machine {
gpuDisplayName
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import (
)

var ConfigFile string
var ApiKey string
var apiKey string

// ConfigCmd represents the config command
var ConfigCmd = &cobra.Command{
Use: "config",
Short: "CLI Config",
Long: "RunPod CLI Config Settings",
Run: func(c *cobra.Command, args []string) {
ApiKey = strings.TrimSpace(ApiKey)
if len(ApiKey) == 0 {
apiKey = strings.TrimSpace(apiKey)
if len(apiKey) == 0 {
cobra.CheckErr(errors.New("apiKey cannot be empty"))
}
err := viper.WriteConfig()
Expand All @@ -30,7 +29,7 @@ var ConfigCmd = &cobra.Command{
}

func init() {
ConfigCmd.Flags().StringVar(&ApiKey, "apiKey", "", "runpod api key")
ConfigCmd.Flags().StringVar(&apiKey, "apiKey", "", "runpod api key")
ConfigCmd.MarkFlagRequired("apiKey")
viper.BindPFlag("apiKey", ConfigCmd.Flags().Lookup("apiKey"))
viper.SetDefault("apiKey", "")
Expand Down
17 changes: 17 additions & 0 deletions cmd/get.go
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)
}
63 changes: 63 additions & 0 deletions cmd/pod/getPod.go
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")
}
23 changes: 0 additions & 23 deletions cmd/pod/ls.go

This file was deleted.

21 changes: 0 additions & 21 deletions cmd/pod/pod.go

This file was deleted.

9 changes: 4 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"os"

"cli/cmd/config"
"cli/cmd/pod"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "runpod-cli",
Short: "RunPod CLI",
Long: "RUNPOD CLI",
Use: "runpodctl",
Short: "runpodctl for runpod.io",
Long: "runpodctl is a CLI tool to manage your pods for runpod.io",
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -28,7 +27,7 @@ func Execute() {

func init() {
cobra.OnInitialize(initConfig)
rootCmd.AddCommand(pod.PodCmd)
rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(config.ConfigCmd)
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
19 changes: 19 additions & 0 deletions table/table.go
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)
}

0 comments on commit 1f187a1

Please sign in to comment.