Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Feb 4, 2025
1 parent b6d6e7a commit 26ad5bc
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 102 deletions.
5 changes: 5 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ issues:
- staticcheck
text: "SA1019:"

# Ignore the copyright header checks in following files
- path: component/tabwriter/internal/tabwriter.go
linters:
- goheader

include:
- EXC0011 # disable excluding of issues about missing package comments from stylecheck

Expand Down
54 changes: 21 additions & 33 deletions command/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import (
"github.com/spf13/cobra"
)

const (
NameArgumentName = "name"
NamesArgumentName = "name(s)"
)

var ErrIgnoreArg = fmt.Errorf("ignore argument")

type Arg struct {
Expand Down Expand Up @@ -58,34 +53,6 @@ func Args(cmd *cobra.Command, argDefs ...Arg) {
addArgsToUseString(cmd, argDefs)
}

func NameArg(name *string) Arg {
return Arg{
Name: NameArgumentName,
Arity: 1,
Set: func(_ *cobra.Command, args []string, offset int) error {
*name = args[offset]
return nil
},
}
}

func OptionalNameArg(name *string) Arg {
arg := NameArg(name)
arg.Optional = true
return arg
}

func NamesArg(names *[]string) Arg {
return Arg{
Name: NamesArgumentName,
Arity: -1,
Set: func(_ *cobra.Command, args []string, offset int) error {
*names = args[offset:]
return nil
},
}
}

func Argument(name string, val *string) Arg {
return Arg{
Name: name,
Expand Down Expand Up @@ -150,3 +117,24 @@ func addArgsToUseString(cmd *cobra.Command, argDefs []Arg) {
cmd.Use += " " + name
}
}

// Name argument specific helpers

const (
NameArgumentName = "name"
NamesArgumentName = "name(s)"
)

func NameArg(val *string) Arg {
return Argument(NameArgumentName, val)
}

func OptionalNameArg(val *string) Arg {
arg := NameArg(val)
arg.Optional = true
return arg
}

func NamesArg(vals *[]string) Arg {
return RemainingArguments(NamesArgumentName, vals)
}
22 changes: 13 additions & 9 deletions component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,30 @@ The function takes a message string as its argument and returns an error if the

If an error occurs during the function's execution, it will return a new error wrapped with the `github.com/pkg/errors` package.

## Colorable-TTY
## StringUtils (includes string colors and formatting)

`Colorable-TTY` is a Golang package that provides utilities for creating colorized and formatted console output. This package uses the `logrusorgru/aurora` package to generate the colorized output. It also includes several functions for formatting strings such as `Rpad`, `Underline`, `Bold`, `TrimRightSpace`, and `BeginsWith`.
This package that provides string utility functions for creating colorized and formatted console output. This include following predefined formatting options like `Faint`, `Bold` , `Underline` and `Italic` as well as color options like `InfoColor`, `SuccessColor`, `WarnColor` and `ErrorColor`.

Then, you can use the functions provided by this package. For example:
You can use the functions provided by this package to easily format your string. For example:

``` go
package main

import (
"fmt"
"github.com/vmware-tanzu/tanzu-plugin-runtime/component"
"github.com/vmware-tanzu/tanzu-plugin-runtime/component/stringutils"
)

func main() {
fmt.Println(component.Underline("This text is underlined!"))
fmt.Println(component.Bold("This text is bold!"))
fmt.Println(component.Rpad("Right padded text", 20))
fmt.Println(component.TrimRightSpace("Trim the whitespace at the end "))
fmt.Println(component.BeginsWith("This is a test", "This"))
fmt.Println(stringutils.Sinfof("This text is info text!"))
fmt.Println(stringutils.Ssuccessf("This text is success text!"))
fmt.Println(stringutils.Swarnf("This text is warning text!"))
fmt.Println(stringutils.Serrorf("This text is error text!"))
fmt.Println(stringutils.Sunderlinef("This text is underlined!"))
fmt.Println(stringutils.Sboldf("This text is bold!"))

fmt.Println(stringutils.Rpad("Right padded text", 20))
fmt.Println(stringutils.TrimRightSpace("Trim the whitespace at the end "))
}
```

Expand Down
12 changes: 0 additions & 12 deletions component/characters/spaces.go

This file was deleted.

4 changes: 2 additions & 2 deletions component/colorable-tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func IsTTYEnabled() bool {
}

// Rpad adds padding to the right of a string.
// from https://github.com/spf13/cobra/blob/993cc5372a05240dfd59e3ba952748b36b2cd117/cobra.go#L29
// from https://github.com/spf13/cobra/blob/4ba5566f5704a9c0d205e1ef3efc4896156d33fa/cobra.go#L173-L177
//
// Deprecated: Rpad is being moved under `github.com/vmware-tanzu/tanzu-plugin-runtime/component/stringutils` package
func Rpad(s string, padding int) string {
Expand All @@ -59,7 +59,7 @@ func TrimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}

// Deprecated: BeginsWith is being moved under `github.com/vmware-tanzu/tanzu-plugin-runtime/component/stringutils` package
// Deprecated: BeginsWith is being deprecated. Use strings.HasPrefix instead
func BeginsWith(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}
Expand Down
4 changes: 2 additions & 2 deletions component/characters/icon.go → component/icons/icon.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2025 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package characters
// Package icon is predefined set of icons that can be used for consistency

Check failure on line 4 in component/icons/icon.go

View workflow job for this annotation

GitHub Actions / Lint

ST1000: package comment should be of the form "Package icons..." (stylecheck)
package icons

type Icon rune

Expand All @@ -15,7 +16,6 @@ const (
Check Icon = '✓'
Exclamation Icon = '❗'
Factory Icon = '🏭'
HammerWrench Icon = '🛠'
Magnifying Icon = '🔎'
Package Icon = '📦'
Question Icon = '❓'
Expand Down
10 changes: 5 additions & 5 deletions component/stringutils/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ import (
func init() {
// Configure the global `NoColor` option within github.com/fatih/color library
// based on the user's terminal and provided options
color.NoColor = !component.IsTTYEnabled()
color.NoColor = color.NoColor || !component.IsTTYEnabled()
}

var (
FaintColor = color.New(color.Faint)
InfoColor = color.New(color.FgCyan)
SuccessColor = color.New(color.FgGreen)
WarnColor = color.New(color.FgYellow)
ErrorColor = color.New(color.FgRed)
BoldColor = color.New(color.Bold)

Bold = color.New(color.Bold)
Faint = color.New(color.Faint)
Underline = color.New(color.Underline)
Italic = color.New(color.Italic)
)

func Sfaintf(format string, a ...interface{}) string {
return FaintColor.Sprintf(format, a...)
return Faint.Sprintf(format, a...)
}

func Sinfof(format string, a ...interface{}) string {
Expand All @@ -51,7 +51,7 @@ func Serrorf(format string, a ...interface{}) string {
}

func Sboldf(format string, a ...interface{}) string {
return BoldColor.Sprintf(format, a...)
return Bold.Sprintf(format, a...)
}

func Sunderlinef(format string, a ...interface{}) string {
Expand Down
7 changes: 2 additions & 5 deletions component/stringutils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ import (
"unicode"
)

// TrimRightSpace removes space (including all unicode space character) from right
func TrimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}

func BeginsWith(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}

// Rpad adds padding to the right of a string.
// from https://github.com/spf13/cobra/blob/993cc5372a05240dfd59e3ba952748b36b2cd117/cobra.go#L29
// from https://github.com/spf13/cobra/blob/4ba5566f5704a9c0d205e1ef3efc4896156d33fa/cobra.go#L173-L177
func Rpad(s string, padding int) string {
tmpl := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(tmpl, s)
Expand Down
21 changes: 0 additions & 21 deletions component/stringutils/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,6 @@ func TestTrimRightSpace(t *testing.T) {
}
}

func TestBeginsWith(t *testing.T) {
tests := []struct {
s string
prefix string
expected bool
}{
{"hello world", "hello", true},
{"hello world", "world", false},
{"", "", true},
{"", "nonempty", false},
{"prefix test", "prefix", true},
}

for _, tt := range tests {
result := BeginsWith(tt.s, tt.prefix)
if result != tt.expected {
t.Errorf("BeginsWith(%q, %q) = %v; want %v", tt.s, tt.prefix, result, tt.expected)
}
}
}

func TestRpad(t *testing.T) {
tests := []struct {
s string
Expand Down
36 changes: 27 additions & 9 deletions component/tabwriter/internal/tabwriter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
// Copyright 2025 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Copyright 2009 The Go Authors.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Package implements a write filter (tabwriter.Writer) that
// translates tabbed columns in input into properly aligned text.
Expand All @@ -15,9 +36,6 @@
// The package is using the Elastic Tabstops algorithm described at
// http://nickgravgaard.com/elastictabstops/index.html.

// repackaged from https://github.com/liggitt/tabwriter/tree/6880f16551ff10c60d3989df2bbd68a55bb3517f
// adds IgnoreAnsiCodes

// Package internal is repackaged from https://github.com/liggitt/tabwriter/tree/6880f16551ff10c60d3989df2bbd68a55bb3517f and adds IgnoreAnsiCodes
//
//nolint:gocyclo,gocritic,unused,nakedret,stylecheck,whitespace
Expand Down
1 change: 1 addition & 0 deletions component/tabwriter/tabwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

// Package tabwriter exposes an tabwriter functionality
// from https://github.com/kubernetes/cli-runtime/blob/v0.28.1/pkg/printers/tabwriter.go
package tabwriter

import (
Expand Down
8 changes: 5 additions & 3 deletions component/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"strings"
)

// From https://github.com/kubernetes/cli-runtime/blob/v0.28.1/pkg/printers/terminal.go"

// terminalEscaper replaces ANSI escape sequences and other terminal special
// characters to avoid terminal escape character attacks (issue #101695).
// Add "\x1b", "^[" to the `NewReplacer` params to scape color
var terminalEscaper = strings.NewReplacer("\r", "\\r")
// characters to avoid terminal escape character attacks (https://github.com/kubernetes/kubernetes/issues/101695).
// Add "\x1b", "^[" to the `NewReplacer` params to escape color
var terminalEscaper = strings.NewReplacer("\x1b", "^[", "\r", "\\r")

// WriteEscaped replaces unsafe terminal characters with replacement strings
// and writes them to the given writer.
Expand Down
2 changes: 1 addition & 1 deletion plugin/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ var TemplateFuncs = template.FuncMap{
"bold": stringutils.Sboldf,
"underline": stringutils.Sunderlinef,
"trimTrailingWhitespaces": stringutils.TrimRightSpace,
"beginsWith": stringutils.BeginsWith,
"beginsWith": strings.HasPrefix,
}

func shouldPrintInvocationWithoutTarget(target types.Target, ic *InvocationContext) bool {
Expand Down

0 comments on commit 26ad5bc

Please sign in to comment.