Skip to content

Commit

Permalink
Feature/get workflow,Task,Domain,Project (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
yindia authored Sep 22, 2020
1 parent 8774f00 commit 2811f53
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 598 deletions.
30 changes: 30 additions & 0 deletions flytectl/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# WARNING: THIS FILE IS MANAGED IN THE 'BOILERPLATE' REPO AND COPIED TO OTHER REPOSITORIES.
# ONLY EDIT THIS FILE FROM WITHIN THE 'LYFT/BOILERPLATE' REPOSITORY:
#
# TO OPT OUT OF UPDATES, SEE https://github.com/lyft/boilerplate/blob/master/Readme.rst

run:
skip-dirs:
- pkg/client

linters:
disable-all: true
enable:
- deadcode
- errcheck
- gas
- goconst
- goimports
- golint
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
1 change: 1 addition & 0 deletions flytectl/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
type Config struct {
Project string `json:"project" pflag:",Specifies the project to work on."`
Domain string `json:"domain" pflag:",Specified the domain to work on."`
Output string `json:"output" pflag:",Specified the output type."`
}

func GetConfig() *Config {
Expand Down
1 change: 1 addition & 0 deletions flytectl/cmd/config/config_flags.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions flytectl/cmd/core/cmd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package cmdcore

import (
"context"
Expand Down Expand Up @@ -27,7 +27,6 @@ func generateCommandFunc(cmdFunc CommandFunc) func(cmd *cobra.Command, args []st
if err != nil {
return err
}

return cmdFunc(ctx, args, CommandContext{
out: cmd.OutOrStdout(),
adminClient: adminClient,
Expand Down
2 changes: 1 addition & 1 deletion flytectl/cmd/core/cmd_ctx.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package cmdcore

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion flytectl/cmd/core/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package cmdcore

import "context"

Expand Down
45 changes: 5 additions & 40 deletions flytectl/cmd/get/get.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package get

import (
"context"

"github.com/lyft/flytectl/cmd/core"

"github.com/lyft/flytestdlib/logger"

"github.com/landoop/tableprinter"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/spf13/cobra"
)

Expand All @@ -18,42 +12,13 @@ func CreateGetCommand() *cobra.Command {
Short: "Retrieve various resource.",
}

getResourcesFuncs := map[string]core.CommandFunc{
"projects": getProjectsFunc,
getResourcesFuncs := map[string]cmdcore.CommandFunc{
"projects": getProjectsFunc,
"tasks": getTaskFunc,
"workflows": getWorkflowFunc,
}

core.AddCommands(getCmd, getResourcesFuncs)
cmdcore.AddCommands(getCmd, getResourcesFuncs)

return getCmd
}

func getProjectsFunc(ctx context.Context, args []string, cmdCtx core.CommandContext) error {
projects, err := cmdCtx.AdminClient().ListProjects(ctx, &admin.ProjectListRequest{})
if err != nil {
return err
}

logger.Debugf(ctx, "Retrieved %v projects", len(projects.Projects))
printer := tableprinter.New(cmdCtx.OutputPipe())
printer.Print(toPrintableProjects(projects.Projects))
return nil
}

func toPrintableProjects(projects []*admin.Project) []interface{} {
type printableProject struct {
Id string `header:"Id"`
Name string `header:"Name"`
Description string `header:"Description"`
}

res := make([]interface{}, 0, len(projects))
for _, p := range projects {
res = append(res, printableProject{
Id: p.Id,
Name: p.Name,
Description: p.Description,
})
}

return res
}
56 changes: 56 additions & 0 deletions flytectl/cmd/get/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package get

import (
"context"
"github.com/lyft/flytectl/cmd/config"
"encoding/json"
cmdCore "github.com/lyft/flytectl/cmd/core"
"github.com/lyft/flytectl/pkg/printer"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/lyft/flytestdlib/logger"
)

type PrintableProject struct {
Id string `header:"Id"`
Name string `header:"Name"`
Description string `header:"Description"`
}

var tableStructure = map[string]string{
"Id" : "$.id",
"Name" : "$.name",
"Description" : "$.description",
}


func getProjectsFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
adminPrinter := printer.Printer{}

transformProject := func(jsonbody [] byte)(interface{},error){
results := PrintableProject{}
if err := json.Unmarshal(jsonbody, &results); err != nil {
return results,err
}
return results,nil
}
if len(args) == 1 {
projects, err := cmdCtx.AdminClient().ListProjects(ctx, &admin.ProjectListRequest{})
if err != nil {
return err
}
logger.Debugf(ctx, "Retrieved %v projects", len(projects.Projects))
for _, v := range projects.Projects {
if v.Name == args[0] {
adminPrinter.Print(config.GetConfig().Output, projects.Projects,tableStructure,transformProject)
}
}
return nil
}
projects, err := cmdCtx.AdminClient().ListProjects(ctx, &admin.ProjectListRequest{})
if err != nil {
return err
}
logger.Debugf(ctx, "Retrieved %v projects", len(projects.Projects))
adminPrinter.Print(config.GetConfig().Output, projects.Projects,tableStructure,transformProject)
return nil
}
78 changes: 78 additions & 0 deletions flytectl/cmd/get/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package get

import (
"context"
"encoding/json"
"fmt"
"github.com/lyft/flytectl/cmd/config"
cmdCore "github.com/lyft/flytectl/cmd/core"
"github.com/lyft/flytectl/pkg/printer"
"github.com/lyft/flytestdlib/logger"

"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin"
)

type PrintableTask struct {
Version string `header:"Version"`
Name string `header:"Name"`
Type string `header:"Type"`
Discoverable bool `header:"Discoverable"`
DiscoveryVersion string `header:"DiscoveryVersion"`
}

var taskStructure = map[string]string{
"Version" : "$.id.version",
"Name" : "$.id.name",
"Type" : "$.closure.compiledTask.template.type",
"Discoverable" : "$.closure.compiledTask.template.metadata.discoverable",
"DiscoveryVersion" : "$.closure.compiledTask.template.metadata.discovery_version",
}

var transformTask = func(jsonbody [] byte)(interface{},error){
results := PrintableTask{}
if err := json.Unmarshal(jsonbody, &results); err != nil {
return results,err
}
return results,nil
}

func getTaskFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
if config.GetConfig().Project == "" {
return fmt.Errorf("Please set project name to get domain")
}
if config.GetConfig().Domain == "" {
return fmt.Errorf("Please set project name to get workflow")
}
taskPrinter := printer.Printer{
}

if len(args) == 1 {
task, err := cmdCtx.AdminClient().ListTasks(ctx, &admin.ResourceListRequest{
Id: &admin.NamedEntityIdentifier{
Project: config.GetConfig().Project,
Domain: config.GetConfig().Domain,
Name: args[0],
},
Limit: 10,
})
if err != nil {
return err
}
logger.Debugf(ctx, "Retrieved Task", task.Tasks)

taskPrinter.Print(config.GetConfig().Output, task.Tasks,taskStructure,transformTask)
return nil
}

tasks, err := cmdCtx.AdminClient().ListTaskIds(ctx, &admin.NamedEntityIdentifierListRequest{
Project: config.GetConfig().Project,
Domain: config.GetConfig().Domain,
Limit: 3,
})
if err != nil {
return err
}
logger.Debugf(ctx, "Retrieved %v Task", len(tasks.Entities))
taskPrinter.Print(config.GetConfig().Output, tasks.Entities,entityStructure,transformTaskEntity)
return nil
}
23 changes: 23 additions & 0 deletions flytectl/cmd/get/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package get

import "encoding/json"

type PrintableNamedEntityIdentifier struct {
Name string `header:"Name"`
Project string `header:"Project"`
Domain string `header:"Domain"`
}

var entityStructure = map[string]string{
"Domain" : "$.domain",
"Name" : "$.name",
"Project" : "$.project",
}

var transformTaskEntity = func(jsonbody [] byte)(interface{},error){
results := PrintableNamedEntityIdentifier{}
if err := json.Unmarshal(jsonbody, &results); err != nil {
return results,err
}
return results,nil
}
72 changes: 72 additions & 0 deletions flytectl/cmd/get/workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package get

import (
"context"
"encoding/json"
"fmt"
"github.com/lyft/flytectl/cmd/config"
cmdCore "github.com/lyft/flytectl/cmd/core"
"github.com/lyft/flytectl/pkg/printer"
"github.com/lyft/flytestdlib/logger"

"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin"
)

var workflowStructure = map[string]string{
"Version" : "$.id.version",
"Name" : "$.id.name",
}

type PrintableWorkflow struct {
Name string `header:"Name"`
Version string `header:"Version"`
}

var transformWorkflow = func(jsonbody [] byte)(interface{},error){
results := PrintableWorkflow{}
if err := json.Unmarshal(jsonbody, &results); err != nil {
return results,err
}
return results,nil
}


func getWorkflowFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
if config.GetConfig().Project == "" {
return fmt.Errorf("Please set project name to get domain")
}
if config.GetConfig().Domain == "" {
return fmt.Errorf("Please set project name to get workflow")
}
adminPrinter := printer.Printer{
}
if len(args) > 0 {
workflows, err := cmdCtx.AdminClient().ListWorkflows(ctx, &admin.ResourceListRequest{
Id: &admin.NamedEntityIdentifier{
Project: config.GetConfig().Project,
Domain: config.GetConfig().Domain,
Name: args[0],
},
Limit: 10,
})
if err != nil {
return err
}
logger.Debugf(ctx, "Retrieved %v workflows", len(workflows.Workflows))

adminPrinter.Print(config.GetConfig().Output, workflows.Workflows,workflowStructure,transformWorkflow)
return nil
}
workflows, err := cmdCtx.AdminClient().ListWorkflowIds(ctx, &admin.NamedEntityIdentifierListRequest{
Project: config.GetConfig().Project,
Domain: config.GetConfig().Domain,
Limit: 10,
})
if err != nil {
return err
}
logger.Debugf(ctx, "Retrieved %v workflows", len(workflows.Entities))

adminPrinter.Print(config.GetConfig().Output, workflows.Entities,entityStructure,transformTaskEntity)
return nil
}
2 changes: 1 addition & 1 deletion flytectl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var (
configAccessor = viper.NewAccessor(stdConfig.Options{StrictMode: true})
)


func newRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
PersistentPreRunE: initConfig,
Expand All @@ -31,6 +30,7 @@ func newRootCmd() *cobra.Command {
// --root.project, this adds a convenience on top to allow --project to be used
rootCmd.PersistentFlags().StringVarP(&(config.GetConfig().Project), "project", "p", "", "Specifies the Flyte project.")
rootCmd.PersistentFlags().StringVarP(&(config.GetConfig().Domain), "domain", "d", "", "Specifies the Flyte project's domain.")
rootCmd.PersistentFlags().StringVarP(&(config.GetConfig().Output), "output", "o", "table", "Specifies the output type")

rootCmd.AddCommand(viper.GetConfigCommand())
rootCmd.AddCommand(versionCmd)
Expand Down
Loading

0 comments on commit 2811f53

Please sign in to comment.