Improving func environment command (#1935)

* improvising func environment command

Signed-off-by: ntishchauhan0022 <nitishchauhan0022@gmail.com>

* running goimport

Signed-off-by: ntishchauhan0022 <nitishchauhan0022@gmail.com>

* resolving describing non-running function

Signed-off-by: ntishchauhan0022 <nitishchauhan0022@gmail.com>

---------

Signed-off-by: ntishchauhan0022 <nitishchauhan0022@gmail.com>
This commit is contained in:
Nitish Chauhan 2023-08-19 03:03:18 +05:30 committed by GitHub
parent 5135a9205b
commit e04c1392c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package cmd
import (
"context"
"encoding/json"
"fmt"
"os"
@ -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
@ -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)
},
@ -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
@ -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) {
@ -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":
@ -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
}

View File

@ -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
@ -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)
```