-
Notifications
You must be signed in to change notification settings - Fork 454
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
Cobra cli #69
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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) | ||
} |
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,18 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
//NewCommandGet generate run 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 | ||
} |
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,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
} | ||
} | ||
} |
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 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() | ||
} |
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,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) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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