oscerd closed pull request #179: Add log command URL: https://github.com/apache/camel-k/pull/179
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/pkg/client/cmd/completion_bash.go b/pkg/client/cmd/completion_bash.go index d6f093a..df76a1e 100644 --- a/pkg/client/cmd/completion_bash.go +++ b/pkg/client/cmd/completion_bash.go @@ -145,6 +145,10 @@ __custom_func() { __kamel_kubectl_get_integrations return ;; + kamel_log) + __kamel_kubectl_get_integrations + return + ;; kamel_context_delete) __kamel_kubectl_get_user_integrationcontexts return @@ -225,7 +229,6 @@ func configureKnownBashCompletions(command *cobra.Command) { } func configureBashAnnotationForFlag(command *cobra.Command, flagName string, annotations map[string][]string) { - flag := command.Flag(flagName) if flag != nil { flag.Annotations = annotations diff --git a/pkg/client/cmd/log.go b/pkg/client/cmd/log.go new file mode 100644 index 0000000..d727718 --- /dev/null +++ b/pkg/client/cmd/log.go @@ -0,0 +1,85 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "fmt" + + "github.com/operator-framework/operator-sdk/pkg/sdk" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" + "github.com/apache/camel-k/pkg/util/log" + "github.com/spf13/cobra" +) + +func newCmdLog(rootCmdOptions *RootCmdOptions) *cobra.Command { + options := logCmdOptions{ + RootCmdOptions: rootCmdOptions, + } + + cmd := cobra.Command{ + Use: "log integration", + Short: "Print the logs of an integration", + Long: `Print the logs of an integration.`, + Args: options.validate, + RunE: options.run, + } + + // completion support + configureKnownCompletions(&cmd) + + return &cmd +} + +type logCmdOptions struct { + *RootCmdOptions +} + +func (o *logCmdOptions) validate(cmd *cobra.Command, args []string) error { + if len(args) != 1 { + return fmt.Errorf("accepts 1 arg, received %d", len(args)) + } + + return nil +} + +func (o *logCmdOptions) run(cmd *cobra.Command, args []string) error { + integration := v1alpha1.Integration{ + TypeMeta: metav1.TypeMeta{ + Kind: v1alpha1.IntegrationKind, + APIVersion: v1alpha1.SchemeGroupVersion.String(), + }, + ObjectMeta: metav1.ObjectMeta{ + Namespace: o.Namespace, + Name: args[0], + }, + } + + if err := sdk.Get(&integration); err != nil { + return err + } + if err := log.Print(o.Context, &integration); err != nil { + return err + } + + // Let's add a wait point, otherwise the script terminates + <-o.Context.Done() + + return nil +} diff --git a/pkg/client/cmd/root.go b/pkg/client/cmd/root.go index 4c3e574..69920e7 100644 --- a/pkg/client/cmd/root.go +++ b/pkg/client/cmd/root.go @@ -46,9 +46,9 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, error) { Context: ctx, } var cmd = cobra.Command{ - Use: "kamel", - Short: "Kamel is a awesome client tool for running Apache Camel integrations natively on Kubernetes", - Long: kamelCommandLongDescription, + Use: "kamel", + Short: "Kamel is a awesome client tool for running Apache Camel integrations natively on Kubernetes", + Long: kamelCommandLongDescription, BashCompletionFunction: bashCompletionFunction, } @@ -78,6 +78,7 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, error) { cmd.AddCommand(newCmdGet(&options)) cmd.AddCommand(newCmdDelete(&options)) cmd.AddCommand(newCmdInstall(&options)) + cmd.AddCommand(newCmdLog(&options)) cmd.AddCommand(newCmdContext(&options)) return &cmd, nil diff --git a/pkg/client/cmd/run.go b/pkg/client/cmd/run.go index 558244e..1d030d5 100644 --- a/pkg/client/cmd/run.go +++ b/pkg/client/cmd/run.go @@ -19,7 +19,6 @@ package cmd import ( "fmt" - "io" "io/ioutil" "net/http" "os" @@ -28,8 +27,6 @@ import ( "strings" "github.com/apache/camel-k/pkg/trait" - "github.com/arsham/blush/blush" - "github.com/apache/camel-k/pkg/util" "github.com/apache/camel-k/pkg/util/sync" @@ -153,7 +150,7 @@ func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error { } } if o.Logs || o.Dev { - err = o.printLogs(integration) + err = log.Print(o.Context, integration) if err != nil { return err } @@ -191,35 +188,6 @@ func (o *runCmdOptions) waitForIntegrationReady(integration *v1alpha1.Integratio return watch.HandleStateChanges(o.Context, integration, handler) } -func (o *runCmdOptions) printLogs(integration *v1alpha1.Integration) error { - scraper := log.NewSelectorScraper(integration.Namespace, "camel.apache.org/integration="+integration.Name) - reader := scraper.Start(o.Context) - - b := &blush.Blush{ - Finders: []blush.Finder{ - blush.NewExact("FATAL", blush.Red), - blush.NewExact("ERROR", blush.Red), - blush.NewExact("WARN", blush.Yellow), - blush.NewExact("INFO", blush.Green), - blush.NewExact("DEBUG", blush.Colour{ - Foreground: blush.RGB{R: 170, G: 170, B: 170}, - Background: blush.NoRGB, - }), - blush.NewExact("TRACE", blush.Colour{ - Foreground: blush.RGB{R: 170, G: 170, B: 170}, - Background: blush.NoRGB, - }), - }, - Reader: ioutil.NopCloser(reader), - } - - if _, err := io.Copy(os.Stdout, b); err != nil { - fmt.Println(err.Error()) - } - - return nil -} - func (o *runCmdOptions) syncIntegration(file string) error { changes, err := sync.File(o.Context, file) if err != nil { diff --git a/pkg/util/log/util.go b/pkg/util/log/util.go new file mode 100644 index 0000000..05a83db --- /dev/null +++ b/pkg/util/log/util.go @@ -0,0 +1,59 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package log + +import ( + "context" + "fmt" + "io" + "io/ioutil" + "os" + + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" + "github.com/arsham/blush/blush" +) + +// Print prints integrations logs to the stdout +func Print(ctx context.Context, integration *v1alpha1.Integration) error { + scraper := NewSelectorScraper(integration.Namespace, "camel.apache.org/integration="+integration.Name) + reader := scraper.Start(ctx) + + b := &blush.Blush{ + Finders: []blush.Finder{ + blush.NewExact("FATAL", blush.Red), + blush.NewExact("ERROR", blush.Red), + blush.NewExact("WARN", blush.Yellow), + blush.NewExact("INFO", blush.Green), + blush.NewExact("DEBUG", blush.Colour{ + Foreground: blush.RGB{R: 170, G: 170, B: 170}, + Background: blush.NoRGB, + }), + blush.NewExact("TRACE", blush.Colour{ + Foreground: blush.RGB{R: 170, G: 170, B: 170}, + Background: blush.NoRGB, + }), + }, + Reader: ioutil.NopCloser(reader), + } + + if _, err := io.Copy(os.Stdout, b); err != nil { + fmt.Println(err.Error()) + } + + return nil +} ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services