Skip to content

Commit

Permalink
Improving func environment command (#1935)
Browse files Browse the repository at this point in the history
* improvising func environment command

Signed-off-by: ntishchauhan0022 <[email protected]>

* running goimport

Signed-off-by: ntishchauhan0022 <[email protected]>

* resolving describing non-running function

Signed-off-by: ntishchauhan0022 <[email protected]>

---------

Signed-off-by: ntishchauhan0022 <[email protected]>
  • Loading branch information
nitishchauhan0022 authored Aug 18, 2023
1 parent 5135a92 commit e04c139
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
35 changes: 32 additions & 3 deletions cmd/environment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -29,7 +30,7 @@ NAME
{{rootCmdUse}} environment - display function execution environment information
SYNOPSIS
{{rootCmdUse}} environment [-e|--env-format] [-v|--verbose]
{{rootCmdUse}} environment [-f|--format] [-v|--verbose] [-p|--path]
DESCRIPTION
Expand All @@ -38,7 +39,7 @@ DESCRIPTION
available runtimes, and available templates.
`,
SuggestFor: []string{"env", "environemtn", "enviroment", "enviornment", "enviroment"},
PreRunE: bindEnv("verbose", "format"),
PreRunE: bindEnv("verbose", "format", "path"),
RunE: func(cmd *cobra.Command, args []string) error {
return runEnvironment(cmd, newClient, version)
},
Expand All @@ -49,6 +50,7 @@ DESCRIPTION
}

cmd.Flags().StringP("format", "f", format, "Format of output environment information, 'json' or 'yaml'. ($FUNC_FORMAT)")
addPathFlag(cmd)
addVerboseFlag(cmd, cfg.Verbose)

return cmd
Expand All @@ -67,6 +69,8 @@ type Environment struct {
Cluster string
TektonTasks map[string]string
Defaults config.Global
Function *functions.Function `json:",omitempty" yaml:",omitempty"`
Instance *functions.Instance `json:",omitempty" yaml:",omitempty"`
}

func runEnvironment(cmd *cobra.Command, newClient ClientFactory, v *Version) (err error) {
Expand Down Expand Up @@ -140,6 +144,14 @@ func runEnvironment(cmd *cobra.Command, newClient ClientFactory, v *Version) (er
},
}

function, instance := describeFuncInformation(cmd.Context(), newClient, cfg)
if function != nil {
environment.Function = function
}
if instance != nil {
environment.Instance = instance
}

var s []byte
switch cfg.Format {
case "json":
Expand Down Expand Up @@ -177,16 +189,33 @@ func getTemplates(client *functions.Client, runtimes []string) (map[string][]str
return templateMap, nil
}

func describeFuncInformation(context context.Context, newClient ClientFactory, cfg environmentConfig) (*functions.Function, *functions.Instance) {
function, err := functions.NewFunction(cfg.Path)
if err != nil || !function.Initialized() {
return nil, nil
}

client, done := newClient(ClientConfig{Namespace: function.Deploy.Namespace, Verbose: cfg.Verbose})
defer done()

instance, err := client.Describe(context, function.Name, function)
if err != nil {
return &function, nil
}
return &function, &instance
}

type environmentConfig struct {
Verbose bool
Format string
Path string
}

func newEnvironmentConfig() (cfg environmentConfig, err error) {
cfg = environmentConfig{
Verbose: viper.GetBool("verbose"),
Format: viper.GetString("format"),
Path: viper.GetString("path"),
}

return
}
3 changes: 2 additions & 1 deletion docs/reference/func_environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ NAME
func environment - display function execution environment information

SYNOPSIS
func environment [-e|--env-format] [-v|--verbose]
func environment [-f|--format] [-v|--verbose] [-p|--path]


DESCRIPTION
Expand All @@ -27,6 +27,7 @@ func environment
```
-f, --format string Format of output environment information, 'json' or 'yaml'. ($FUNC_FORMAT) (default "json")
-h, --help help for environment
-p, --path string Path to the function. Default is current directory ($FUNC_PATH)
-v, --verbose Print verbose logs ($FUNC_VERBOSE)
```

Expand Down

0 comments on commit e04c139

Please sign in to comment.