Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Added output option for dot and doturl (#101)
Browse files Browse the repository at this point in the history
Signed-off-by: Prafulla Mahindrakar <[email protected]>
  • Loading branch information
pmahindrakar-oss authored Jun 17, 2021
1 parent 60fb355 commit d938e10
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 350 deletions.
43 changes: 13 additions & 30 deletions cmd/get/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ package get

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

"github.com/flyteorg/flytestdlib/errors"

"github.com/flyteorg/flytectl/pkg/visualize"

workflowconfig "github.com/flyteorg/flytectl/cmd/config/subcommand/workflow"
"github.com/flyteorg/flytectl/pkg/ext"
"github.com/flyteorg/flytestdlib/logger"
Expand Down Expand Up @@ -75,6 +67,18 @@ Retrieves all the workflow within project and domain in json format.
flytectl get workflow -p flytesnacks -d development -o json
Visualize the graph for a workflow within project and domain in dot format.
::
flytectl get workflow -p flytesnacks -d development -o dot
Visualize the graph for a workflow within project and domain in a dot content render.
::
flytectl get workflow -p flytesnacks -d development -o dot-url
Usage
`
)
Expand Down Expand Up @@ -103,28 +107,7 @@ func getWorkflowFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandC
return err
}
logger.Debugf(ctx, "Retrieved %v workflow", len(workflows))
err = adminPrinter.Print(config.GetConfig().MustOutputFormat(), workflowColumns, WorkflowToProtoMessages(workflows)...)
if err != nil {
return err
}
if len(workflows) > 0 && workflowconfig.DefaultConfig.Visualize != "" {
//f, err := workflowconfig.DefaultConfig.GraphvizFormat()
//if err != nil {
// return err
//}
if workflowconfig.DefaultConfig.OutputFile == "" {
return fmt.Errorf("--visualize should be accompanied with a file-name using --output_file option")
}
b, err := visualize.RenderWorkflow(workflows[0].Closure.CompiledWorkflow)
if err != nil {
return errors.Wrapf("VisualizationError", err, "failed to visualize workflow")
}
if err := ioutil.WriteFile(workflowconfig.DefaultConfig.OutputFile, b, fs.ModePerm); err != nil {
return errors.Wrapf("FileWriteError", err, "failed to write visualization file")
}
fmt.Printf("File [%s] written", workflowconfig.DefaultConfig.OutputFile)
}
return nil
return adminPrinter.Print(config.GetConfig().MustOutputFormat(), workflowColumns, WorkflowToProtoMessages(workflows)...)
}

workflows, err = cmdCtx.AdminFetcherExt().FetchAllVerOfWorkflow(ctx, "", config.GetConfig().Project, config.GetConfig().Domain, workflowconfig.DefaultConfig.Filter)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/sirupsen/logrus v1.8.0
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
Expand Down
14 changes: 8 additions & 6 deletions pkg/printer/outputformat_enumer.go

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

32 changes: 32 additions & 0 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/browser"
"net/url"
"os"

"github.com/flyteorg/flytectl/pkg/visualize"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flytestdlib/errors"

"github.com/ghodss/yaml"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
Expand All @@ -22,8 +27,12 @@ const (
OutputFormatTABLE OutputFormat = iota
OutputFormatJSON
OutputFormatYAML
OutputFormatDOT
OutputFormatDOTURL
)

const GraphVisualizationServiceUrl = "http://graph.flyte.org/#"

func OutputFormats() []string {
var v []string
for _, o := range OutputFormatValues() {
Expand Down Expand Up @@ -137,6 +146,29 @@ func (p Printer) Print(format OutputFormat, columns []Column, messages ...proto.
}
fmt.Println(string(v))
}
case OutputFormatDOT, OutputFormatDOTURL:
var workflows []*admin.Workflow
for _, m := range messages {
if w, ok := m.(*admin.Workflow); ok {
workflows = append(workflows, w)
} else {
return fmt.Errorf("visualization is only supported on workflows")
}
}
if len(workflows) == 0 {
return fmt.Errorf("atleast one workflow required for visualization")
}
workflow := workflows[0]
graphStr, err := visualize.RenderWorkflow(workflow.Closure.CompiledWorkflow)
if err != nil {
return errors.Wrapf("VisualizationError", err, "failed to visualize workflow")
}
if format == OutputFormatDOTURL {
urlToOpen := GraphVisualizationServiceUrl + url.PathEscape(graphStr)
fmt.Println("Opening the browser at "+ urlToOpen)
return browser.OpenURL(urlToOpen)
}
fmt.Println(graphStr)
default: // Print table
rows, err := json.Marshal(printableMessages)
if err != nil {
Expand Down
Loading

0 comments on commit d938e10

Please sign in to comment.