-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Context Sharing POC #1855
Context Sharing POC #1855
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ import ( | |
|
||
"github.com/spf13/cobra" | ||
"knative.dev/client/pkg/kn/config" | ||
"knative.dev/client/pkg/kn/plugin" | ||
pluginpkg "knative.dev/client/pkg/kn/plugin" | ||
"knative.dev/client/pkg/kn/root" | ||
) | ||
|
||
|
@@ -60,7 +60,7 @@ func run(args []string) error { | |
return err | ||
} | ||
|
||
pluginManager := plugin.NewManager(config.GlobalConfig.PluginsDir(), config.GlobalConfig.LookupPluginsInPath()) | ||
pluginManager := pluginpkg.NewManager(config.GlobalConfig.PluginsDir(), config.GlobalConfig.LookupPluginsInPath()) | ||
|
||
// Create kn root command and all sub-commands | ||
rootCmd, err := root.NewRootCommand(pluginManager.HelpTemplateFuncs()) | ||
|
@@ -84,13 +84,37 @@ func run(args []string) error { | |
return err | ||
} | ||
|
||
// FT: Context Sharing | ||
var ctxManager *pluginpkg.ContextDataManager | ||
if config.GlobalConfig.ContextSharing() { | ||
ctxManager, err = pluginpkg.NewContextManager(pluginManager) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func(ctxManager *pluginpkg.ContextDataManager) { | ||
if err := ctxManager.WriteCache(); err != nil { | ||
println("error during write") | ||
} | ||
}(ctxManager) | ||
} | ||
|
||
if plugin != nil { | ||
// Validate & Execute plugin | ||
err = validatePlugin(rootCmd, plugin) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if config.GlobalConfig.ContextSharing() { | ||
if pwm, ok := plugin.(pluginpkg.PluginWithManifest); ok { | ||
data, _ := ctxManager.FetchContextData() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably should not ignore the error here. Either remove the error part of the FectContextData() signature or return the error (I hope, the second arg is an error ;-), nevertheless ignore this here has some smell) |
||
err := pwm.ExecuteWithContext(data, argsWithoutCommands(args, plugin.CommandParts())) | ||
if err != nil { | ||
return &runError{err: err} | ||
} | ||
} | ||
return nil | ||
} | ||
err := plugin.Execute(argsWithoutCommands(args, plugin.CommandParts())) | ||
if err != nil { | ||
return &runError{err: err} | ||
|
@@ -140,7 +164,7 @@ func filterHelpOptions(args []string) []string { | |
} | ||
|
||
// Check if the plugin collides with any command specified in the root command | ||
func validatePlugin(root *cobra.Command, plugin plugin.Plugin) error { | ||
func validatePlugin(root *cobra.Command, plugin pluginpkg.Plugin) error { | ||
// Check if a given plugin can be identified as a command | ||
cmd, args, err := root.Find(plugin.CommandParts()) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
"knative.dev/serving/pkg/apis/serving" | ||
|
||
"knative.dev/client/pkg/kn/commands/revision" | ||
"knative.dev/client/pkg/kn/plugin" | ||
"knative.dev/client/pkg/printers" | ||
clientservingv1 "knative.dev/client/pkg/serving/v1" | ||
|
||
|
@@ -99,10 +100,21 @@ func NewServiceDescribeCommand(p *commands.KnParams) *cobra.Command { | |
Example: describe_example, | ||
ValidArgsFunction: commands.ResourceNameCompletionFunc(p), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var serviceName string | ||
if len(args) != 1 { | ||
return errors.New("'service describe' requires the service name given as single argument") | ||
if plugin.CtxManager != nil { | ||
data, err := plugin.CtxManager.FetchContextData() | ||
if err != nil { | ||
return err | ||
} | ||
serviceName = data["service"] | ||
} | ||
if serviceName == "" { | ||
return errors.New("'service describe' requires the service name given as single argument") | ||
} | ||
} else { | ||
serviceName = args[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure, but I have the feeling that his access pattern will be repeated in several commands (all that require a service name), so maybe we could extract this handling in a utility method. Usually, I wouldn't opt for this, but this looks complex enough to avoid a C&P approach. |
||
} | ||
serviceName := args[0] | ||
|
||
namespace, err := p.GetNamespace(cmd) | ||
if err != nil { | ||
|
@@ -182,7 +194,7 @@ func describe(w io.Writer, service *servingv1.Service, revisions []*revisionDesc | |
return nil | ||
} | ||
|
||
// Write out main service information. Use colors for major items. | ||
// WriteCache out main service information. Use colors for major items. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems to be a wrong search/replace thingy. Same as in the comment below. |
||
func writeService(dw printers.PrefixWriter, service *servingv1.Service) { | ||
commands.WriteMetadata(dw, &service.ObjectMeta, printDetails) | ||
dw.WriteAttribute("URL", extractURL(service)) | ||
|
@@ -200,7 +212,7 @@ func writeService(dw printers.PrefixWriter, service *servingv1.Service) { | |
} | ||
} | ||
|
||
// Write out revisions associated with this service. By default only active | ||
// WriteCache out revisions associated with this service. By default only active | ||
// target revisions are printed, but with --verbose also inactive revisions | ||
// created by this services are shown | ||
func writeRevisions(dw printers.PrefixWriter, revisions []*revisionDesc, printDetails bool) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,7 +117,7 @@ func setupConfig(t *testing.T, configContent string) (string, func()) { | |
// Save old args | ||
backupArgs := os.Args | ||
|
||
// Write out a temporary configContent file | ||
// WriteCache out a temporary configContent file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong comment |
||
var cfgFile string | ||
if configContent != "" { | ||
cfgFile = filepath.Join(tmpDir, "config.yaml") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error message should contain more context (pun intended :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, you propbably want to write this to standard error.