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

Cobra cli #69

Merged
merged 5 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
65 changes: 65 additions & 0 deletions cmd/cli/create-study.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"context"
"fmt"
"io/ioutil"
"log"

"github.com/kubeflow/katib/pkg/api"
"github.com/spf13/cobra"
"google.golang.org/grpc"
yaml "gopkg.in/yaml.v2"
)

type createStudyOpt struct {
conf string
args []string
}

//NewCommandGetStudy generate run cmd
func NewCommandCreateStudy() *cobra.Command {
var opt createStudyOpt
cmd := &cobra.Command{
Use: "study",
Args: cobra.NoArgs,
Short: "Create a study from a file",
Long: "YAML formats are accepted.",
Aliases: []string{"st"},
Run: func(cmd *cobra.Command, args []string) {
opt.args = args
createStudy(cmd, &opt)
},
}
cmd.Flags().StringVarP(&opt.conf, "config", "f", "", "File path of study config(required)")
cmd.MarkFlagRequired("config")
return cmd
}

func createStudy(cmd *cobra.Command, opt *createStudyOpt) {
//check and get persistent flag volume
var pf *PersistentFlags
pf, err := CheckPersistentFlags()
if err != nil {
log.Fatalf("Fail to Check Flags: %v", err)
return
}

var sc api.StudyConfig
buf, _ := ioutil.ReadFile(opt.conf)
err = yaml.Unmarshal(buf, &sc)

conn, err := grpc.Dial(pf.server, grpc.WithInsecure())
if err != nil {
log.Fatalf("could not connect: %v", err)
return
}
defer conn.Close()
req := &api.CreateStudyRequest{StudyConfig: &sc}
c := api.NewManagerClient(conn)
r, err := c.CreateStudy(context.Background(), req)
if err != nil {
log.Fatalf("CreateStudy failed: %v", err)
}
fmt.Printf("Study %v is created.", r.StudyId)
}
18 changes: 18 additions & 0 deletions cmd/cli/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/spf13/cobra"
)

//NewCommandGet generate run cmd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name is NewCommandCreate. Maybe there is a typo

func NewCommandCreate() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create a resource from a file",
Long: `YAML formats are accepted.`,
}

cmd.AddCommand(NewCommandCreateStudy())

return cmd
}
159 changes: 159 additions & 0 deletions cmd/cli/get-model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package main

import (
"context"
"fmt"
"log"
"os"
"strings"
"text/tabwriter"
"unicode/utf8"

"github.com/kubeflow/katib/pkg/api"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

type getModelOpt struct {
detail bool
args []string
}

//NewCommandGetTrials generate run cmd
func NewCommandGetModel() *cobra.Command {
var opt getModelOpt
cmd := &cobra.Command{
Use: "model",
Args: cobra.MaximumNArgs(2),
Short: "Display Model Info",
Long: `Display Information of saved model`,
Aliases: []string{"md"},
Run: func(cmd *cobra.Command, args []string) {
opt.args = args
getModel(cmd, &opt)
},
}
cmd.Flags().BoolVarP(&opt.detail, "detail", "d", false, "Display detail information of Model")
return cmd
}

func getModel(cmd *cobra.Command, opt *getModelOpt) {
//check and get persistent flag volume
var pf *PersistentFlags
pf, err := CheckPersistentFlags()
if err != nil {
log.Fatalf("Fail to Check Flags: %v", err)
return
}

conn, err := grpc.Dial(pf.server, grpc.WithInsecure())
if err != nil {
log.Fatalf("could not connect: %v", err)
return
}
defer conn.Close()
var soverviews []*api.StudyOverview
c := api.NewManagerClient(conn)
if len(opt.args) > 0 {
req := &api.GetStudiesRequest{}
r, err := c.GetStudies(context.Background(), req)
if err != nil {
log.Fatalf("GetStudy failed: %v", err)
return
}
if len(r.StudyInfos) > 0 {
var sInfo []*api.StudyInfo
for _, si := range r.StudyInfos {
if len(opt.args) > 0 {
if utf8.RuneCountInString(opt.args[0]) >= 7 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add some comments here? I can not understand the code well.

if strings.HasPrefix(si.StudyId, opt.args[0]) {
soverviews = append(soverviews, &api.StudyOverview{
Name: si.Name,
Owner: si.Owner,
})
break
}
}
if si.Name == opt.args[0] {
soverviews = append(soverviews, &api.StudyOverview{
Name: si.Name,
Owner: si.Owner,
})
break
}
} else {
soverviews = append(soverviews, &api.StudyOverview{
Name: si.Name,
Owner: si.Owner,
})
}
}
if len(sInfo) == 0 {
log.Fatalf("No Study %v is not saved.", opt.args[0])
return
}
}
}
if len(soverviews) == 0 {
sreq := &api.GetSavedStudiesRequest{}
sr, err := c.GetSavedStudies(context.Background(), sreq)
if err != nil {
log.Fatalf("GetStudy failed: %v", err)
return
}
if len(sr.Studies) == 0 {
log.Fatalf("No Studies are saved.")
return
}
for _, s := range sr.Studies {
soverviews = append(soverviews, s)
}
}
for _, si := range soverviews {
mreq := &api.GetSavedModelsRequest{StudyName: si.Name}
mr, err := c.GetSavedModels(context.Background(), mreq)
if err != nil {
log.Fatalf("GetModels failed: %v", err)
return
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', tabwriter.TabIndent)
fmt.Printf("Study %v Owner %v Saved Model Num %v:\n", si.Name, si.Owner, len(mr.Models))
if opt.detail {
for _, m := range mr.Models {
if len(opt.args) > 1 {
if !strings.HasPrefix(m.TrialId, opt.args[1]) {
continue
}
}
fmt.Printf("TrialID :%v\n", m.TrialId)
fmt.Printf("Model Path: %s\n", m.ModelPath)
fmt.Println("Parameters:")
for _, p := range m.Parameters {
fmt.Fprintf(w, " %s:\t%v\n", p.Name, p.Value)
}
w.Flush()
fmt.Println("Metrics:")
for _, m := range m.Metrics {
fmt.Fprintf(w, " %s:\t%v\n", m.Name, m.Value)
}
w.Flush()
}
} else {
fmt.Fprintln(w, "TrialID\tParamNum\tMetricsNum")
for _, m := range mr.Models {
if len(opt.args) > 1 {
if !strings.HasPrefix(m.TrialId, opt.args[1]) {
continue
}
}
fmt.Fprintf(w, "%s\t%d\t%d\n",
string([]rune(m.TrialId)[:7]),
len(m.Parameters),
len(m.Metrics),
)
}
w.Flush()
}
}
}
63 changes: 63 additions & 0 deletions cmd/cli/get-studies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"context"
"fmt"
"log"
"os"
"text/tabwriter"

"github.com/kubeflow/katib/pkg/api"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

//NewCommandGetStudies generate run cmd
func NewCommandGetStudies() *cobra.Command {
cmd := &cobra.Command{
Use: "studies",
Args: cobra.NoArgs,
Short: "Display Study list",
Long: `list of studies and their overview`,
Aliases: []string{"sts"},
Run: getStudies,
}
return cmd
}

func getStudies(cmd *cobra.Command, args []string) {
//check and get persistent flag volume
var pf *PersistentFlags
pf, err := CheckPersistentFlags()
if err != nil {
log.Fatalf("Fail to Check Flags: %v", err)
return
}

conn, err := grpc.Dial(pf.server, grpc.WithInsecure())
if err != nil {
log.Fatalf("could not connect: %v", err)
return
}
defer conn.Close()

c := api.NewManagerClient(conn)
req := &api.GetStudiesRequest{}
r, err := c.GetStudies(context.Background(), req)
if err != nil {
log.Fatalf("GetStudy failed: %v", err)
return
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', tabwriter.TabIndent)
fmt.Fprintln(w, "StudyID\tName\tOwner\tRunning\tCompleted")
for _, si := range r.StudyInfos {
fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%d\n",
string([]rune(si.StudyId)[:7]),
si.Name,
si.Owner,
si.RunningTrialNum,
si.CompletedTrialNum)
}
w.Flush()
}
73 changes: 73 additions & 0 deletions cmd/cli/get-study.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"context"
"fmt"
"log"
"strings"
"unicode/utf8"

"github.com/kubeflow/katib/pkg/api"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

//NewCommandGetStudy generate run cmd
func NewCommandGetStudy() *cobra.Command {
cmd := &cobra.Command{
Use: "study",
Args: cobra.ExactArgs(1),
Short: "Display Study Info",
Long: `Display Information of a study`,
Aliases: []string{"st"},
Run: getStudy,
}
return cmd
}

func getStudy(cmd *cobra.Command, args []string) {
//check and get persistent flag volume
var pf *PersistentFlags
pf, err := CheckPersistentFlags()
if err != nil {
log.Fatalf("Fail to Check Flags: %v", err)
return
}

conn, err := grpc.Dial(pf.server, grpc.WithInsecure())
if err != nil {
log.Fatalf("could not connect: %v", err)
return
}
defer conn.Close()

c := api.NewManagerClient(conn)
req := &api.GetStudiesRequest{}
r, err := c.GetStudies(context.Background(), req)
if err != nil {
log.Fatalf("GetStudy failed: %v", err)
return
}
var sInfo *api.StudyInfo
for _, si := range r.StudyInfos {
if utf8.RuneCountInString(args[0]) >= 7 {
if strings.HasPrefix(si.StudyId, args[0]) {
sInfo = si
break
}
}
if si.Name == args[0] {
sInfo = si
break
}
}
if sInfo == nil {
log.Fatalf("Study %s is not found.", args[0])
return
}
fmt.Printf("Study ID: %s\n", sInfo.StudyId)
fmt.Printf("Study Name: %s\n", sInfo.Name)
fmt.Printf("Study Owner: %s\n", sInfo.Owner)
fmt.Printf("Runinng Trial: %d\n", sInfo.RunningTrialNum)
fmt.Printf("Complete Trial: %d\n", sInfo.CompletedTrialNum)
}
Loading