Skip to content

Commit

Permalink
Make store describe command more useful
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Apr 4, 2023
1 parent de0f17e commit 0b1e92b
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 33 deletions.
7 changes: 0 additions & 7 deletions commands/store_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
func init() {
// Setup flags that are used by multiple commands (variables defined in faas.go)
storeDeployCmd.Flags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL starting with http(s)://")
storeDeployCmd.Flags().StringVar(&network, "network", "", "Name of the network")
storeDeployCmd.Flags().StringVar(&functionName, "name", "", "Name of the deployed function (overriding name from the store)")
storeDeployCmd.Flags().StringVarP(&functionNamespace, "namespace", "n", "", "Namespace of the function")
// Setup flags that are used only by deploy command (variables defined above)
Expand All @@ -42,7 +41,6 @@ var storeDeployCmd = &cobra.Command{
Use: `deploy (FUNCTION_NAME|FUNCTION_TITLE)
[--name FUNCTION_NAME]
[--gateway GATEWAY_URL]
[--network NETWORK_NAME]
[--env ENVVAR=VALUE ...]
[--label LABEL=VALUE ...]
[--annotation ANNOTATION=VALUE ...]
Expand Down Expand Up @@ -117,11 +115,6 @@ func runStoreDeploy(cmd *cobra.Command, args []string) error {
}
}

// Use the network from manifest if not changed by user
if !cmd.Flag("network").Changed {
network = item.Network
}

itemName := item.Name

if functionName != "" {
Expand Down
60 changes: 38 additions & 22 deletions commands/store_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ import (
"fmt"
"text/tabwriter"

"github.com/mitchellh/go-wordwrap"
storeV2 "github.com/openfaas/faas-cli/schema/store/v2"
"github.com/spf13/cobra"
)

func init() {
// Setup flags used by store command
storeDescribeCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output for the field values")

storeCmd.AddCommand(storeDescribeCmd)
}

var storeDescribeCmd = &cobra.Command{
Use: `describe (FUNCTION_NAME|FUNCTION_TITLE) [--url STORE_URL]`,
Short: "Show details of OpenFaaS function from a store",
Example: ` faas-cli store describe NodeInfo
faas-cli store describe NodeInfo --url https://host:port/store.json`,
Example: ` faas-cli store describe nodeinfo
faas-cli store describe nodeinfo --url https://host:port/store.json
`,
Aliases: []string{"inspect"},
RunE: runStoreDescribe,
}
Expand All @@ -47,35 +46,52 @@ func runStoreDescribe(cmd *cobra.Command, args []string) error {
return fmt.Errorf("function '%s' not found for platform '%s'", functionName, targetPlatform)
}

verbose, err := cmd.Flags().GetBool("verbose")
if err != nil {
return err
}

content := storeRenderItem(item, targetPlatform, verbose)
content := storeRenderItem(item, targetPlatform)
fmt.Print(content)

return nil
}

func storeRenderItem(item *storeV2.StoreFunction, platform string, verbose bool) string {
func storeRenderItem(item *storeV2.StoreFunction, platform string) string {
var b bytes.Buffer
w := tabwriter.NewWriter(&b, 0, 0, 1, ' ', 0)
fmt.Fprintf(w, "Info for: %s\n\n", item.Title)

fmt.Fprintf(w, "%s\t%s\n", "Name", item.Name)
author := item.Author
if author == "" {
author = "unknown"
}

fmt.Fprintf(w, "%s\t%s\n", "Title:", item.Title)
fmt.Fprintf(w, "%s\t%s\n", "Author:", item.Author)
fmt.Fprintf(w, "%s\t\n%s\n\n", "Description:", wordwrap.WrapString(item.Description, 80))

fmt.Fprintf(w, "%s\t%s\n", "Image:", item.GetImageName(platform))
fmt.Fprintf(w, "%s\t%s\n", "Process:", item.Fprocess)
fmt.Fprintf(w, "%s\t%s\n", "Repo URL:", item.RepoURL)
if len(item.Environment) > 0 {
fmt.Fprintf(w, "Environment:\n")
for k, v := range item.Environment {
fmt.Fprintf(w, "- \t%s:\t%s\n", k, v)
}
fmt.Fprintln(w)
}

desc := item.Description
if !verbose {
desc = storeRenderDescription(desc)
if item.Labels != nil {
fmt.Fprintf(w, "Labels:\n")
for k, v := range item.Labels {
fmt.Fprintf(w, "- \t%s:\t%s\n", k, v)
}
fmt.Fprintln(w)
}

fmt.Fprintf(w, "%s\t%s\n", "Description", desc)
fmt.Fprintf(w, "%s\t%s\n", "Image", item.GetImageName(platform))
fmt.Fprintf(w, "%s\t%s\n", "Process", item.Fprocess)
fmt.Fprintf(w, "%s\t%s\n", "Repo URL", item.RepoURL)
if len(item.Annotations) > 0 {
fmt.Fprintf(w, "Annotations:\n")
for k, v := range item.Annotations {
fmt.Fprintf(w, "- \t%s:\t%s\n", k, v)
}
fmt.Fprintln(w)
}

fmt.Fprintln(w)
w.Flush()
return b.String()
}
9 changes: 7 additions & 2 deletions commands/store_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ func storeRenderItems(items []storeV2.StoreFunction) string {
var b bytes.Buffer
w := tabwriter.NewWriter(&b, 0, 0, 1, ' ', 0)
fmt.Fprintln(w)
fmt.Fprintln(w, "FUNCTION\tDESCRIPTION")
fmt.Fprintln(w, "FUNCTION\tAUTHOR\tDESCRIPTION")

for _, item := range items {
fmt.Fprintf(w, "%s\t%s\n", item.Title, storeRenderDescription(item.Description))
author := item.Author
if author == "" {
author = "unknown"
}

fmt.Fprintf(w, "%s\t%s\t%s\n", item.Name, author, storeRenderDescription(item.Title))
}

fmt.Fprintln(w)
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ require (
gopkg.in/yaml.v3 v3.0.1
)

require github.com/google/go-containerregistry v0.13.0
require (
github.com/google/go-containerregistry v0.13.0
github.com/mitchellh/go-wordwrap v1.0.1
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0j
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae h1:O4SWKdcHVCvYqyDV+9CJA1fcDN2L11Bule0iFy3YlAI=
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae/go.mod h1:E2VnQOmVuvZB6UYnnDB0qG5Nq/1tD9acaOpo6xmt0Kw=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
2 changes: 1 addition & 1 deletion schema/store/v2/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package v2
// StoreFunction represents a multi-arch function in the store
type StoreFunction struct {
Icon string `json:"icon"`
Author string `json:"author,omitempty"`
Title string `json:"title"`
Description string `json:"description"`
Name string `json:"name"`
Fprocess string `json:"fprocess"`
Network string `json:"network"`
RepoURL string `json:"repo_url"`
ReadOnlyRootFilesystem bool `json:"readOnlyRootFilesystem"`
Environment map[string]string `json:"environment"`
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/mitchellh/go-wordwrap/LICENSE.md

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

39 changes: 39 additions & 0 deletions vendor/github.com/mitchellh/go-wordwrap/README.md

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

83 changes: 83 additions & 0 deletions vendor/github.com/mitchellh/go-wordwrap/wordwrap.go

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

3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ github.com/mattn/go-runewidth
# github.com/mitchellh/go-homedir v1.1.0
## explicit
github.com/mitchellh/go-homedir
# github.com/mitchellh/go-wordwrap v1.0.1
## explicit; go 1.14
github.com/mitchellh/go-wordwrap
# github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae
## explicit; go 1.13
github.com/moby/term
Expand Down

0 comments on commit 0b1e92b

Please sign in to comment.