-
Notifications
You must be signed in to change notification settings - Fork 448
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 all 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 | ||
} | ||
|
||
//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) | ||
} |
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" | ||
) | ||
|
||
//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 | ||
} |
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,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() | ||
} | ||
} | ||
} |
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,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() | ||
} |
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,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 | ||
} |
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,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) | ||
} | ||
|
||
} |
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.
Please remove the line here.