Skip to content

Commit

Permalink
refactor vcli
Browse files Browse the repository at this point in the history
Signed-off-by: googs1025 <googs1025@gmail.com>
  • Loading branch information
googs1025 committed May 26, 2024
1 parent 70a483b commit fdf734a
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 227 deletions.
102 changes: 51 additions & 51 deletions cmd/cli/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/spf13/cobra"

"volcano.sh/volcano/cmd/cli/util"
"volcano.sh/volcano/pkg/cli/job"
)

Expand All @@ -12,65 +13,64 @@ func buildJobCmd() *cobra.Command {
Short: "vcctl command line operation job",
}

jobRunCmd := &cobra.Command{
Use: "run",
Short: "run job by parameters from the command line",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.RunJob())
jobCommandMap := map[string]struct {
Short string
RunFunction func(cmd *cobra.Command, args []string)
InitFlags func(cmd *cobra.Command)
}{
"run": {
Short: "run job by parameters from the command line",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.RunJob())
},
InitFlags: job.InitRunFlags,
},
}
job.InitRunFlags(jobRunCmd)
jobCmd.AddCommand(jobRunCmd)

jobListCmd := &cobra.Command{
Use: "list",
Short: "list job information",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.ListJobs())
"list": {
Short: "list job information",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.ListJobs())
},
InitFlags: job.InitListFlags,
},
}
job.InitListFlags(jobListCmd)
jobCmd.AddCommand(jobListCmd)

jobViewCmd := &cobra.Command{
Use: "view",
Short: "show job information",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.ViewJob())
"view": {
Short: "show job information",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.ViewJob())
},
InitFlags: job.InitViewFlags,
},
}
job.InitViewFlags(jobViewCmd)
jobCmd.AddCommand(jobViewCmd)

jobSuspendCmd := &cobra.Command{
Use: "suspend",
Short: "abort a job",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.SuspendJob())
"suspend": {
Short: "abort a job",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.SuspendJob())
},
InitFlags: job.InitSuspendFlags,
},
}
job.InitSuspendFlags(jobSuspendCmd)
jobCmd.AddCommand(jobSuspendCmd)

jobResumeCmd := &cobra.Command{
Use: "resume",
Short: "resume a job",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.ResumeJob())
"resume": {
Short: "resume a job",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.ResumeJob())
},
InitFlags: job.InitResumeFlags,
},
"delete": {
Short: "delete a job",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, job.DeleteJob())
},
InitFlags: job.InitDeleteFlags,
},
}
job.InitResumeFlags(jobResumeCmd)
jobCmd.AddCommand(jobResumeCmd)

jobDelCmd := &cobra.Command{
Use: "delete",
Short: "delete a job",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, job.DeleteJob())
},
for command, config := range jobCommandMap {
cmd := &cobra.Command{
Use: command,
Short: config.Short,
Run: config.RunFunction,
}
config.InitFlags(cmd)
jobCmd.AddCommand(cmd)
}
job.InitDeleteFlags(jobDelCmd)
jobCmd.AddCommand(jobDelCmd)

return jobCmd
}
93 changes: 51 additions & 42 deletions cmd/cli/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"github.com/spf13/cobra"

"volcano.sh/volcano/cmd/cli/util"
"volcano.sh/volcano/pkg/cli/queue"
)

Expand All @@ -28,55 +29,63 @@ func buildQueueCmd() *cobra.Command {
Short: "Queue Operations",
}

queueCreateCmd := &cobra.Command{
Use: "create",
Short: "creates queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.CreateQueue())
commands := []struct {
Use string
Short string
RunFunction func(cmd *cobra.Command, args []string)
InitFlags func(cmd *cobra.Command)
}{
{
Use: "create",
Short: "creates queue",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, queue.CreateQueue())
},
InitFlags: queue.InitCreateFlags,
},
}
queue.InitCreateFlags(queueCreateCmd)
queueCmd.AddCommand(queueCreateCmd)

queueDeleteCmd := &cobra.Command{
Use: "delete",
Short: "delete queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.DeleteQueue())
{
Use: "delete",
Short: "delete queue",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, queue.DeleteQueue())
},
InitFlags: queue.InitDeleteFlags,
},
}
queue.InitDeleteFlags(queueDeleteCmd)
queueCmd.AddCommand(queueDeleteCmd)

queueOperateCmd := &cobra.Command{
Use: "operate queue",
Short: "operate queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.OperateQueue())
{
Use: "operate",
Short: "operate queue",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, queue.OperateQueue())
},
InitFlags: queue.InitOperateFlags,
},
}
queue.InitOperateFlags(queueOperateCmd)
queueCmd.AddCommand(queueOperateCmd)

queueListCmd := &cobra.Command{
Use: "list",
Short: "lists all the queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.ListQueue())
{
Use: "list",
Short: "lists all the queue",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, queue.ListQueue())
},
InitFlags: queue.InitListFlags,
},
{
Use: "get",
Short: "get a queue",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, queue.GetQueue())
},
InitFlags: queue.InitGetFlags,
},
}
queue.InitListFlags(queueListCmd)
queueCmd.AddCommand(queueListCmd)

queueGetCmd := &cobra.Command{
Use: "get",
Short: "get a queue",
Run: func(cmd *cobra.Command, args []string) {
checkError(cmd, queue.GetQueue())
},
for _, command := range commands {
cmd := &cobra.Command{
Use: command.Use,
Short: command.Short,
Run: command.RunFunction,
}
command.InitFlags(cmd)
queueCmd.AddCommand(cmd)
}
queue.InitGetFlags(queueGetCmd)
queueCmd.AddCommand(queueGetCmd)

return queueCmd
}
15 changes: 0 additions & 15 deletions cmd/cli/vcctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package main

import (
"fmt"
"os"

"github.com/spf13/cobra"
Expand All @@ -42,20 +41,6 @@ func main() {
os.Exit(code)
}

func checkError(cmd *cobra.Command, err error) {
if err != nil {
msg := "Failed to"

// Ignore the root command.
for cur := cmd; cur.Parent() != nil; cur = cur.Parent() {
msg += fmt.Sprintf(" %s", cur.Name())
}

fmt.Printf("%s: %v\n", msg, err)
os.Exit(2)
}
}

var versionExample = `vcctl version`

func versionCommand() *cobra.Command {
Expand Down
41 changes: 0 additions & 41 deletions pkg/cli/job/common.go

This file was deleted.

5 changes: 2 additions & 3 deletions pkg/cli/job/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

type deleteFlags struct {
commonFlags
util.CommonFlags

Namespace string
JobName string
Expand All @@ -39,8 +39,7 @@ var deleteJobFlags = &deleteFlags{}

// InitDeleteFlags init the delete command flags.
func InitDeleteFlags(cmd *cobra.Command) {
initFlags(cmd, &deleteJobFlags.commonFlags)

util.InitFlags(cmd, &deleteJobFlags.CommonFlags)
cmd.Flags().StringVarP(&deleteJobFlags.Namespace, "namespace", "n", "default", "the namespace of job")
cmd.Flags().StringVarP(&deleteJobFlags.JobName, "name", "N", "", "the name of job")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/job/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

type listFlags struct {
commonFlags
util.CommonFlags

Namespace string
SchedulerName string
Expand Down Expand Up @@ -81,7 +81,7 @@ var listJobFlags = &listFlags{}

// InitListFlags init list command flags.
func InitListFlags(cmd *cobra.Command) {
initFlags(cmd, &listJobFlags.commonFlags)
util.InitFlags(cmd, &listJobFlags.CommonFlags)

cmd.Flags().StringVarP(&listJobFlags.Namespace, "namespace", "n", "default", "the namespace of job")
cmd.Flags().StringVarP(&listJobFlags.SchedulerName, "scheduler", "S", "", "list job with specified scheduler name")
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/job/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"volcano.sh/volcano/pkg/cli/util"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -62,7 +63,7 @@ func TestListJob(t *testing.T) {

for i, testcase := range testCases {
listJobFlags = &listFlags{
commonFlags: commonFlags{
CommonFlags: util.CommonFlags{
Master: server.URL,
},
Namespace: "test",
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/job/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

type resumeFlags struct {
commonFlags
util.CommonFlags

Namespace string
JobName string
Expand All @@ -36,7 +36,7 @@ var resumeJobFlags = &resumeFlags{}

// InitResumeFlags init resume command flags.
func InitResumeFlags(cmd *cobra.Command) {
initFlags(cmd, &resumeJobFlags.commonFlags)
util.InitFlags(cmd, &resumeJobFlags.CommonFlags)

cmd.Flags().StringVarP(&resumeJobFlags.Namespace, "namespace", "n", "default", "the namespace of job")
cmd.Flags().StringVarP(&resumeJobFlags.JobName, "name", "N", "", "the name of job")
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/job/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

type runFlags struct {
commonFlags
util.CommonFlags

Name string
Namespace string
Expand All @@ -52,7 +52,7 @@ var launchJobFlags = &runFlags{}

// InitRunFlags init the run flags.
func InitRunFlags(cmd *cobra.Command) {
initFlags(cmd, &launchJobFlags.commonFlags)
util.InitFlags(cmd, &launchJobFlags.CommonFlags)

cmd.Flags().StringVarP(&launchJobFlags.Image, "image", "i", "busybox", "the container image of job")
cmd.Flags().StringVarP(&launchJobFlags.Namespace, "namespace", "n", "default", "the namespace of job")
Expand Down
Loading

0 comments on commit fdf734a

Please sign in to comment.