mirror of https://github.com/knative/func.git
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:
parent
5135a9205b
commit
e04c1392c1
|
@ -1,6 +1,7 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
@ -29,7 +30,7 @@ NAME
|
||||||
{{rootCmdUse}} environment - display function execution environment information
|
{{rootCmdUse}} environment - display function execution environment information
|
||||||
|
|
||||||
SYNOPSIS
|
SYNOPSIS
|
||||||
{{rootCmdUse}} environment [-e|--env-format] [-v|--verbose]
|
{{rootCmdUse}} environment [-f|--format] [-v|--verbose] [-p|--path]
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
|
@ -38,7 +39,7 @@ DESCRIPTION
|
||||||
available runtimes, and available templates.
|
available runtimes, and available templates.
|
||||||
`,
|
`,
|
||||||
SuggestFor: []string{"env", "environemtn", "enviroment", "enviornment", "enviroment"},
|
SuggestFor: []string{"env", "environemtn", "enviroment", "enviornment", "enviroment"},
|
||||||
PreRunE: bindEnv("verbose", "format"),
|
PreRunE: bindEnv("verbose", "format", "path"),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runEnvironment(cmd, newClient, version)
|
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)")
|
cmd.Flags().StringP("format", "f", format, "Format of output environment information, 'json' or 'yaml'. ($FUNC_FORMAT)")
|
||||||
|
addPathFlag(cmd)
|
||||||
addVerboseFlag(cmd, cfg.Verbose)
|
addVerboseFlag(cmd, cfg.Verbose)
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
@ -67,6 +69,8 @@ type Environment struct {
|
||||||
Cluster string
|
Cluster string
|
||||||
TektonTasks map[string]string
|
TektonTasks map[string]string
|
||||||
Defaults config.Global
|
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) {
|
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
|
var s []byte
|
||||||
switch cfg.Format {
|
switch cfg.Format {
|
||||||
case "json":
|
case "json":
|
||||||
|
@ -177,16 +189,33 @@ func getTemplates(client *functions.Client, runtimes []string) (map[string][]str
|
||||||
return templateMap, nil
|
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 {
|
type environmentConfig struct {
|
||||||
Verbose bool
|
Verbose bool
|
||||||
Format string
|
Format string
|
||||||
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newEnvironmentConfig() (cfg environmentConfig, err error) {
|
func newEnvironmentConfig() (cfg environmentConfig, err error) {
|
||||||
cfg = environmentConfig{
|
cfg = environmentConfig{
|
||||||
Verbose: viper.GetBool("verbose"),
|
Verbose: viper.GetBool("verbose"),
|
||||||
Format: viper.GetString("format"),
|
Format: viper.GetString("format"),
|
||||||
|
Path: viper.GetString("path"),
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ NAME
|
||||||
func environment - display function execution environment information
|
func environment - display function execution environment information
|
||||||
|
|
||||||
SYNOPSIS
|
SYNOPSIS
|
||||||
func environment [-e|--env-format] [-v|--verbose]
|
func environment [-f|--format] [-v|--verbose] [-p|--path]
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
|
@ -27,6 +27,7 @@ func environment
|
||||||
```
|
```
|
||||||
-f, --format string Format of output environment information, 'json' or 'yaml'. ($FUNC_FORMAT) (default "json")
|
-f, --format string Format of output environment information, 'json' or 'yaml'. ($FUNC_FORMAT) (default "json")
|
||||||
-h, --help help for environment
|
-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)
|
-v, --verbose Print verbose logs ($FUNC_VERBOSE)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue