This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/master by this push: new 8dfe1f5 chore: extract indentedwriter to it's own package and make it an utility 8dfe1f5 is described below commit 8dfe1f5ca7e1d818d66f330a6fbbb1887e756fe8 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Mon Jul 1 13:16:31 2019 +0200 chore: extract indentedwriter to it's own package and make it an utility --- pkg/cmd/describe.go | 72 +++++++++------------------------------ pkg/cmd/describe_integration.go | 46 +++++++++++++------------ pkg/cmd/describe_kit.go | 30 ++++++++-------- pkg/cmd/describe_platform.go | 22 ++++++------ pkg/util/indentedwriter/writer.go | 70 +++++++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 102 deletions(-) diff --git a/pkg/cmd/describe.go b/pkg/cmd/describe.go index b726138..aa0ae52 100644 --- a/pkg/cmd/describe.go +++ b/pkg/cmd/describe.go @@ -18,92 +18,52 @@ limitations under the License. package cmd import ( - "bytes" - "fmt" - "io" "strings" - "text/tabwriter" "time" + "github.com/apache/camel-k/pkg/util/indentedwriter" + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/spf13/cobra" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -type flusher interface { - flush() -} - -type indentedWriter struct { - out io.Writer -} - -func newIndentedWriter(out io.Writer) *indentedWriter { - return &indentedWriter{out: out} -} - -func (iw *indentedWriter) write(indentLevel int, format string, i ...interface{}) { - indent := " " - prefix := "" - for i := 0; i < indentLevel; i++ { - prefix += indent - } - fmt.Fprintf(iw.out, prefix+format, i...) -} - -func (iw *indentedWriter) Flush() { - if f, ok := iw.out.(flusher); ok { - f.flush() - } -} - -func describeObjectMeta(w *indentedWriter, om metav1.ObjectMeta) { - w.write(0, "Name:\t%s\n", om.Name) - w.write(0, "Namespace:\t%s\n", om.Namespace) +func describeObjectMeta(w *indentedwriter.Writer, om metav1.ObjectMeta) { + w.Write(0, "Name:\t%s\n", om.Name) + w.Write(0, "Namespace:\t%s\n", om.Namespace) if len(om.GetLabels()) > 0 { - w.write(0, "Labels:") + w.Write(0, "Labels:") for k, v := range om.Labels { - w.write(0, "\t%s=%s\n", k, strings.TrimSpace(v)) + w.Write(0, "\t%s=%s\n", k, strings.TrimSpace(v)) } } if len(om.GetAnnotations()) > 0 { - w.write(0, "Annotations:") + w.Write(0, "Annotations:") for k, v := range om.Annotations { - w.write(0, "\t%s=%s\n", k, strings.TrimSpace(v)) + w.Write(0, "\t%s=%s\n", k, strings.TrimSpace(v)) } } - w.write(0, "Creation Timestamp:\t%s\n", om.CreationTimestamp.Format(time.RFC1123Z)) + w.Write(0, "Creation Timestamp:\t%s\n", om.CreationTimestamp.Format(time.RFC1123Z)) } -func describeTraits(w *indentedWriter, traits map[string]v1alpha1.TraitSpec) { +func describeTraits(w *indentedwriter.Writer, traits map[string]v1alpha1.TraitSpec) { if len(traits) > 0 { - w.write(0, "Traits:\n") + w.Write(0, "Traits:\n") for trait := range traits { - w.write(1, "%s:\n", strings.Title(trait)) - w.write(2, "Configuration:\n") + w.Write(1, "%s:\n", strings.Title(trait)) + w.Write(2, "Configuration:\n") for k, v := range traits[trait].Configuration { - w.write(3, "%s:\t%s\n", strings.Title(k), v) + w.Write(3, "%s:\t%s\n", strings.Title(k), v) } } } } -func indentedString(f func(io.Writer)) string { - out := new(tabwriter.Writer) - buf := &bytes.Buffer{} - out.Init(buf, 0, 8, 2, ' ', 0) - - f(out) - - out.Flush() - - return buf.String() -} - func newCmdDescribe(rootCmdOptions *RootCmdOptions) *cobra.Command { cmd := cobra.Command{ Use: "describe", diff --git a/pkg/cmd/describe_integration.go b/pkg/cmd/describe_integration.go index 393a6f7..15bc77c 100644 --- a/pkg/cmd/describe_integration.go +++ b/pkg/cmd/describe_integration.go @@ -22,6 +22,8 @@ import ( "io" "strings" + "github.com/apache/camel-k/pkg/util/indentedwriter" + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/spf13/cobra" k8sclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -85,54 +87,54 @@ func (command *describeIntegrationCommand) run(args []string) error { } func (command *describeIntegrationCommand) describeIntegration(i v1alpha1.Integration) string { - return indentedString(func(out io.Writer) { - w := newIndentedWriter(out) + return indentedwriter.IndentedString(func(out io.Writer) { + w := indentedwriter.NewWriter(out) describeObjectMeta(w, i.ObjectMeta) - w.write(0, "Phase:\t%s\n", i.Status.Phase) - w.write(0, "Camel Version:\t%s\n", i.Status.CamelVersion) - w.write(0, "Kit:\t%s\n", i.Status.Kit) - w.write(0, "Image:\t%s\n", i.Status.Image) + w.Write(0, "Phase:\t%s\n", i.Status.Phase) + w.Write(0, "Camel Version:\t%s\n", i.Status.CamelVersion) + w.Write(0, "Kit:\t%s\n", i.Status.Kit) + w.Write(0, "Image:\t%s\n", i.Status.Image) if len(i.Spec.Configuration) > 0 { - w.write(0, "Configuration:\n") + w.Write(0, "Configuration:\n") for _, config := range i.Spec.Configuration { - w.write(1, "Type:\t%s\n", config.Type) - w.write(1, "Value:\t%s\n", config.Value) + w.Write(1, "Type:\t%s\n", config.Type) + w.Write(1, "Value:\t%s\n", config.Value) } } if len(i.Status.Dependencies) > 0 { - w.write(0, "Dependencies:\n") + w.Write(0, "Dependencies:\n") for _, dependency := range i.Status.Dependencies { - w.write(1, "%s\n", dependency) + w.Write(1, "%s\n", dependency) } } if len(i.Spec.Repositories) > 0 { - w.write(0, "Repositories:\n") + w.Write(0, "Repositories:\n") for _, repository := range i.Spec.Repositories { - w.write(1, "%s\n", repository) + w.Write(1, "%s\n", repository) } } if len(i.Spec.Resources) > 0 { - w.write(0, "Resources:\n") + w.Write(0, "Resources:\n") for _, resource := range i.Spec.Resources { - w.write(1, "Content:\n") - w.write(2, "%s\n", strings.TrimSpace(resource.Content)) - w.write(1, "Name:\t%s\n", resource.Name) - w.write(1, "Type:\t%s\n", resource.Type) + w.Write(1, "Content:\n") + w.Write(2, "%s\n", strings.TrimSpace(resource.Content)) + w.Write(1, "Name:\t%s\n", resource.Name) + w.Write(1, "Type:\t%s\n", resource.Type) } } if len(i.Sources()) > 0 { - w.write(0, "Sources:\n") + w.Write(0, "Sources:\n") for _, s := range i.Sources() { - w.write(1, "Name:\t%s\n", s.Name) - w.write(1, "Content:\n") - w.write(2, "%s\n", strings.TrimSpace(s.Content)) + w.Write(1, "Name:\t%s\n", s.Name) + w.Write(1, "Content:\n") + w.Write(2, "%s\n", strings.TrimSpace(s.Content)) } } diff --git a/pkg/cmd/describe_kit.go b/pkg/cmd/describe_kit.go index 8211d79..bc55a8b 100644 --- a/pkg/cmd/describe_kit.go +++ b/pkg/cmd/describe_kit.go @@ -21,6 +21,8 @@ import ( "fmt" "io" + "github.com/apache/camel-k/pkg/util/indentedwriter" + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/spf13/cobra" k8sclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -84,41 +86,41 @@ func (command *describeKitCommand) run(args []string) error { } func (command *describeKitCommand) describeIntegrationKit(kit v1alpha1.IntegrationKit) string { - return indentedString(func(out io.Writer) { - w := newIndentedWriter(out) + return indentedwriter.IndentedString(func(out io.Writer) { + w := indentedwriter.NewWriter(out) describeObjectMeta(w, kit.ObjectMeta) - w.write(0, "Phase:\t%s\n", kit.Status.Phase) - w.write(0, "Camel Version:\t%s\n", kit.Status.CamelVersion) - w.write(0, "Image:\t%s\n", kit.Status.Image) + w.Write(0, "Phase:\t%s\n", kit.Status.Phase) + w.Write(0, "Camel Version:\t%s\n", kit.Status.CamelVersion) + w.Write(0, "Image:\t%s\n", kit.Status.Image) if len(kit.Status.Artifacts) > 0 { - w.write(0, "Artifacts:\t\n") + w.Write(0, "Artifacts:\t\n") for _, artifact := range kit.Status.Artifacts { - w.write(1, "%s\n", artifact.ID) + w.Write(1, "%s\n", artifact.ID) } } if len(kit.Spec.Configuration) > 0 { - w.write(0, "Configuration:\n") + w.Write(0, "Configuration:\n") for _, config := range kit.Spec.Configuration { - w.write(1, "Type:\t%s\n", config.Type) - w.write(1, "Value:\t%s\n", config.Value) + w.Write(1, "Type:\t%s\n", config.Type) + w.Write(1, "Value:\t%s\n", config.Value) } } if len(kit.Spec.Dependencies) > 0 { - w.write(0, "Dependencies:\t\n") + w.Write(0, "Dependencies:\t\n") for _, dependency := range kit.Spec.Dependencies { - w.write(1, "%s\n", dependency) + w.Write(1, "%s\n", dependency) } } if len(kit.Spec.Repositories) > 0 { - w.write(0, "Repositories:\n") + w.Write(0, "Repositories:\n") for _, repository := range kit.Spec.Repositories { - w.write(1, "%s\n", repository) + w.Write(1, "%s\n", repository) } } diff --git a/pkg/cmd/describe_platform.go b/pkg/cmd/describe_platform.go index 17c0d2b..b1e8f8c 100644 --- a/pkg/cmd/describe_platform.go +++ b/pkg/cmd/describe_platform.go @@ -21,6 +21,8 @@ import ( "fmt" "io" + "github.com/apache/camel-k/pkg/util/indentedwriter" + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/spf13/cobra" k8sclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -83,20 +85,20 @@ func (command *describePlatformCommand) run(args []string) error { } func (command *describePlatformCommand) describeIntegrationPlatform(platform v1alpha1.IntegrationPlatform) string { - return indentedString(func(out io.Writer) { - w := newIndentedWriter(out) + return indentedwriter.IndentedString(func(out io.Writer) { + w := indentedwriter.NewWriter(out) describeObjectMeta(w, platform.ObjectMeta) - w.write(0, "Phase:\t%s\n", platform.Status.Phase) - w.write(0, "Base Image:\t%s\n", platform.Spec.Build.BaseImage) - w.write(0, "Camel Version:\t%s\n", platform.Spec.Build.CamelVersion) - w.write(0, "Local Repository:\t%s\n", platform.Spec.Build.LocalRepository) - w.write(0, "Publish Strategy:\t%s\n", platform.Spec.Build.PublishStrategy) + w.Write(0, "Phase:\t%s\n", platform.Status.Phase) + w.Write(0, "Base Image:\t%s\n", platform.Spec.Build.BaseImage) + w.Write(0, "Camel Version:\t%s\n", platform.Spec.Build.CamelVersion) + w.Write(0, "Local Repository:\t%s\n", platform.Spec.Build.LocalRepository) + w.Write(0, "Publish Strategy:\t%s\n", platform.Spec.Build.PublishStrategy) if len(platform.Spec.Resources.Kits) > 0 { - w.write(0, "Resources:\n") - w.write(1, "Kits:\n") + w.Write(0, "Resources:\n") + w.Write(1, "Kits:\n") for _, kit := range platform.Spec.Resources.Kits { - w.write(2, "%s\n", kit) + w.Write(2, "%s\n", kit) } } }) diff --git a/pkg/util/indentedwriter/writer.go b/pkg/util/indentedwriter/writer.go new file mode 100644 index 0000000..99259be --- /dev/null +++ b/pkg/util/indentedwriter/writer.go @@ -0,0 +1,70 @@ +/* +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 indentedwriter + +import ( + "bytes" + "fmt" + "io" + "text/tabwriter" +) + +// Flusher -- +type Flusher interface { + Flush() +} + +// Writer --. +type Writer struct { + out io.Writer +} + +// NewWriter -- +func NewWriter(out io.Writer) *Writer { + return &Writer{out: out} +} + +// Write -- +func (iw *Writer) Write(indentLevel int, format string, i ...interface{}) { + indent := " " + prefix := "" + for i := 0; i < indentLevel; i++ { + prefix += indent + } + fmt.Fprintf(iw.out, prefix+format, i...) +} + +// Flush -- +func (iw *Writer) Flush() { + if f, ok := iw.out.(Flusher); ok { + f.Flush() + } +} + +// IndentedString -- +func IndentedString(f func(io.Writer)) string { + var out tabwriter.Writer + buf := &bytes.Buffer{} + out.Init(buf, 0, 8, 2, ' ', 0) + + f(&out) + + out.Flush() + + return buf.String() +}