Skip to content

Commit

Permalink
feat: add a URL output type for func describe (#389)
Browse files Browse the repository at this point in the history
* Added a URL output type for 'func describe'

Fixes #387

Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com>
  • Loading branch information
jcrossley3 and zroubalik authored Jun 18, 2021
1 parent 76b5800 commit 947fcaa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func init() {
root.AddCommand(describeCmd)
describeCmd.Flags().StringP("namespace", "n", "", "Namespace of the function. By default, the namespace in func.yaml is used or the actual active namespace if not set in the configuration. (Env: $FUNC_NAMESPACE)")
describeCmd.Flags().StringP("output", "o", "human", "Output format (human|plain|json|xml|yaml) (Env: $FUNC_OUTPUT)")
describeCmd.Flags().StringP("output", "o", "human", "Output format (human|plain|json|xml|yaml|url) (Env: $FUNC_OUTPUT)")
describeCmd.Flags().StringP("path", "p", cwd(), "Path to the project directory (Env: $FUNC_PATH)")

err := describeCmd.RegisterFlagCompletionFunc("output", CompleteOutputFormatList)
Expand Down Expand Up @@ -161,3 +161,10 @@ func (d description) XML(w io.Writer) error {
func (d description) YAML(w io.Writer) error {
return yaml.NewEncoder(w).Encode(d)
}

func (d description) URL(w io.Writer) error {
if len(d.Routes) > 0 {
fmt.Fprintf(w, "%s\n", d.Routes[0])
}
return nil
}
4 changes: 4 additions & 0 deletions cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
JSON = "json" // Technically a ⊆ yaml, but no one likes yaml.
XML = "xml"
YAML = "yaml"
URL = "url"
)

// formatter is any structure which has methods for serialization.
Expand All @@ -23,6 +24,7 @@ type Formatter interface {
JSON(io.Writer) error
XML(io.Writer) error
YAML(io.Writer) error
URL(io.Writer) error
}

// write to the output the output of the formatter's appropriate serilization function.
Expand All @@ -40,6 +42,8 @@ func write(out io.Writer, s Formatter, formatName string) {
err = s.XML(out)
case YAML:
err = s.YAML(out)
case URL:
err = s.URL(out)
default:
err = fmt.Errorf("format not recognized: %v\n", formatName)
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,10 @@ func (items listItems) XML(w io.Writer) error {
func (items listItems) YAML(w io.Writer) error {
return yaml.NewEncoder(w).Encode(items)
}

func (items listItems) URL(w io.Writer) error {
for _, item := range items {
fmt.Fprintf(w, "%s\n", item.URL)
}
return nil
}

0 comments on commit 947fcaa

Please sign in to comment.