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 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
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
}

//NewCommandCreateStudy generate create study 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"
)

//NewCommandCreate generate create cmd
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
}
164 changes: 164 additions & 0 deletions cmd/cli/get-model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
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
}

//NewCommandGetModel generate get model 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)
// Search study if Study ID or name is set
if len(opt.args) > 0 {
// Search specified study in running studies
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 {
for _, si := range r.StudyInfos {
if len(opt.args) > 0 {
if utf8.RuneCountInString(opt.args[0]) >= 7 {
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(soverviews) == 0 {
// Search specified study from ModelDB
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 {
if len(opt.args) > 0 {
if opt.args[0] == s.Name {
soverviews = append(soverviews, s)
}
} else {
soverviews = append(soverviews, s)
}
}
}
for _, si := range soverviews {
// Search Models from ModelDB
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()
}
}
}
83 changes: 83 additions & 0 deletions cmd/cli/get-study.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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"
)

//NewCommandGetStudy generate get studies cmd
func NewCommandGetStudy() *cobra.Command {
cmd := &cobra.Command{
Use: "studies",
Args: cobra.MaximumNArgs(1),
Short: "Display Study lnfo",
Long: `Display Information of a studies`,
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 sis []*api.StudyInfo
// Search study if Study ID or name is set
if len(args) > 0 {
for _, si := range r.StudyInfos {
if utf8.RuneCountInString(args[0]) >= 7 {
if strings.HasPrefix(si.StudyId, args[0]) {
sis = append(sis, si)
break
}
}
if si.Name == args[0] {
sis = append(sis, si)
break
}
}
} else {
sis = r.StudyInfos
}
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 sis {
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()
}
23 changes: 23 additions & 0 deletions cmd/cli/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

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

//NewCommandGet generate get cmd
func NewCommandGet() *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Display one or many resources",
Long: `list of resorces comannd can display includes: studies, study, trials, trial, models, model`,
}

//set local flag

//add subcommand
cmd.AddCommand(NewCommandGetStudy())
// cmd.AddCommand(NewCommandGetTrial())
cmd.AddCommand(NewCommandGetModel())

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

import (
"fmt"
"os"
)

//Entry point
func main() {
//init command
katibctl := NewRootCommand()
if err := katibctl.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}

Copy link
Member

Choose a reason for hiding this comment

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

Please remove the line here.

}
Loading