...

Source file src/k8s.io/kubectl/pkg/util/templates/normalizers.go

Documentation: k8s.io/kubectl/pkg/util/templates

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package templates
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/MakeNowJust/heredoc"
    23  	"github.com/russross/blackfriday/v2"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  const Indentation = `  `
    28  
    29  // LongDesc normalizes a command's long description to follow the conventions.
    30  func LongDesc(s string) string {
    31  	if len(s) == 0 {
    32  		return s
    33  	}
    34  	return normalizer{s}.heredoc().markdown().trim().string
    35  }
    36  
    37  // Examples normalizes a command's examples to follow the conventions.
    38  func Examples(s string) string {
    39  	if len(s) == 0 {
    40  		return s
    41  	}
    42  	return normalizer{s}.trim().indent().string
    43  }
    44  
    45  // Normalize perform all required normalizations on a given command.
    46  func Normalize(cmd *cobra.Command) *cobra.Command {
    47  	if len(cmd.Long) > 0 {
    48  		cmd.Long = LongDesc(cmd.Long)
    49  	}
    50  	if len(cmd.Example) > 0 {
    51  		cmd.Example = Examples(cmd.Example)
    52  	}
    53  	return cmd
    54  }
    55  
    56  // NormalizeAll perform all required normalizations in the entire command tree.
    57  func NormalizeAll(cmd *cobra.Command) *cobra.Command {
    58  	if cmd.HasSubCommands() {
    59  		for _, subCmd := range cmd.Commands() {
    60  			NormalizeAll(subCmd)
    61  		}
    62  	}
    63  	Normalize(cmd)
    64  	return cmd
    65  }
    66  
    67  type normalizer struct {
    68  	string
    69  }
    70  
    71  func (s normalizer) markdown() normalizer {
    72  	bytes := []byte(s.string)
    73  	formatted := blackfriday.Run(bytes, blackfriday.WithExtensions(blackfriday.NoIntraEmphasis), blackfriday.WithRenderer(&ASCIIRenderer{Indentation: Indentation}))
    74  	s.string = string(formatted)
    75  	return s
    76  }
    77  
    78  func (s normalizer) heredoc() normalizer {
    79  	s.string = heredoc.Doc(s.string)
    80  	return s
    81  }
    82  
    83  func (s normalizer) trim() normalizer {
    84  	s.string = strings.TrimSpace(s.string)
    85  	return s
    86  }
    87  
    88  func (s normalizer) indent() normalizer {
    89  	indentedLines := []string{}
    90  	for _, line := range strings.Split(s.string, "\n") {
    91  		trimmed := strings.TrimSpace(line)
    92  		indented := Indentation + trimmed
    93  		indentedLines = append(indentedLines, indented)
    94  	}
    95  	s.string = strings.Join(indentedLines, "\n")
    96  	return s
    97  }
    98  

View as plain text