From 48e162fa051c19ec12b99e1930a924bae3a57f9f Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 8 Apr 2021 16:11:21 -0500 Subject: [PATCH] share workflow content getting code --- pkg/cmd/workflow/run/run.go | 25 +------------------------ pkg/cmd/workflow/shared/shared.go | 27 +++++++++++++++++++++++++++ pkg/cmd/workflow/view/http.go | 27 --------------------------- pkg/cmd/workflow/view/view.go | 4 +++- 4 files changed, 31 insertions(+), 52 deletions(-) diff --git a/pkg/cmd/workflow/run/run.go b/pkg/cmd/workflow/run/run.go index a981b08e3..15d241516 100644 --- a/pkg/cmd/workflow/run/run.go +++ b/pkg/cmd/workflow/run/run.go @@ -2,13 +2,11 @@ package run import ( "bytes" - "encoding/base64" "encoding/json" "errors" "fmt" "io/ioutil" "net/http" - "net/url" "reflect" "sort" "strings" @@ -285,7 +283,7 @@ func runRun(opts *RunOptions) error { } if opts.Prompt { - yamlContent, err := getWorkflowContent(client, repo, workflow, ref) + yamlContent, err := shared.GetWorkflowContent(client, repo, *workflow, ref) if err != nil { return fmt.Errorf("unable to fetch workflow file content: %w", err) } @@ -408,24 +406,3 @@ func findInputs(yamlContent []byte) (map[string]WorkflowInput, error) { return out, nil } - -func getWorkflowContent(client *api.Client, repo ghrepo.Interface, workflow *shared.Workflow, ref string) ([]byte, error) { - path := fmt.Sprintf("repos/%s/contents/%s?ref=%s", ghrepo.FullName(repo), workflow.Path, url.QueryEscape(ref)) - - type Result struct { - Content string - } - - var result Result - err := client.REST(repo.RepoHost(), "GET", path, nil, &result) - if err != nil { - return nil, err - } - - decoded, err := base64.StdEncoding.DecodeString(result.Content) - if err != nil { - return nil, fmt.Errorf("failed to decode workflow file: %w", err) - } - - return decoded, nil -} diff --git a/pkg/cmd/workflow/shared/shared.go b/pkg/cmd/workflow/shared/shared.go index 6b4aa348d..14c1c1417 100644 --- a/pkg/cmd/workflow/shared/shared.go +++ b/pkg/cmd/workflow/shared/shared.go @@ -1,8 +1,10 @@ package shared import ( + "encoding/base64" "errors" "fmt" + "net/url" "path" "strings" @@ -216,3 +218,28 @@ func ResolveWorkflow(io *iostreams.IOStreams, client *api.Client, repo ghrepo.In return SelectWorkflow(workflows, "Which workflow do you mean?", states) } + +func GetWorkflowContent(client *api.Client, repo ghrepo.Interface, workflow Workflow, ref string) ([]byte, error) { + path := fmt.Sprintf("repos/%s/contents/%s", ghrepo.FullName(repo), workflow.Path) + if ref != "" { + q := fmt.Sprintf("?ref=%s", url.QueryEscape(ref)) + path = path + q + } + + type Result struct { + Content string + } + + var result Result + err := client.REST(repo.RepoHost(), "GET", path, nil, &result) + if err != nil { + return nil, err + } + + decoded, err := base64.StdEncoding.DecodeString(result.Content) + if err != nil { + return nil, fmt.Errorf("failed to decode workflow file: %w", err) + } + + return decoded, nil +} diff --git a/pkg/cmd/workflow/view/http.go b/pkg/cmd/workflow/view/http.go index bee32b3b8..9565047a3 100644 --- a/pkg/cmd/workflow/view/http.go +++ b/pkg/cmd/workflow/view/http.go @@ -1,9 +1,7 @@ package view import ( - "encoding/base64" "fmt" - "net/url" "github.com/cli/cli/api" "github.com/cli/cli/internal/ghrepo" @@ -16,31 +14,6 @@ type workflowRuns struct { Runs []runShared.Run } -func getWorkflowContent(client *api.Client, repo ghrepo.Interface, ref string, workflow *shared.Workflow) (string, error) { - path := fmt.Sprintf("repos/%s/contents/%s", ghrepo.FullName(repo), workflow.Path) - if ref != "" { - q := fmt.Sprintf("?ref=%s", url.QueryEscape(ref)) - path = path + q - } - - type Result struct { - Content string - } - - var result Result - err := client.REST(repo.RepoHost(), "GET", path, nil, &result) - if err != nil { - return "", err - } - - decoded, err := base64.StdEncoding.DecodeString(result.Content) - if err != nil { - return "", fmt.Errorf("failed to decode workflow file: %w", err) - } - - return string(decoded), nil -} - func getWorkflowRuns(client *api.Client, repo ghrepo.Interface, workflow *shared.Workflow) (workflowRuns, error) { var wr workflowRuns var result runShared.RunsPayload diff --git a/pkg/cmd/workflow/view/view.go b/pkg/cmd/workflow/view/view.go index dbcabcb81..4cb49ac26 100644 --- a/pkg/cmd/workflow/view/view.go +++ b/pkg/cmd/workflow/view/view.go @@ -143,7 +143,7 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared } opts.IO.StartProgressIndicator() - yaml, err := getWorkflowContent(client, repo, opts.Ref, workflow) + yamlBytes, err := shared.GetWorkflowContent(client, repo, *workflow, opts.Ref) opts.IO.StopProgressIndicator() if err != nil { if s, ok := err.(api.HTTPError); ok && s.StatusCode == 404 { @@ -155,6 +155,8 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared return fmt.Errorf("could not get workflow file content: %w", err) } + yaml := string(yamlBytes) + theme := opts.IO.DetectTerminalTheme() markdownStyle := markdown.GetStyle(theme) if err := opts.IO.StartPager(); err != nil {