github.com/daixiang0/gci@v0.13.0/cmd/gci/gcicommand.go (about)

     1  package gci
     2  
     3  import (
     4  	"github.com/spf13/cobra"
     5  	"go.uber.org/zap/zapcore"
     6  
     7  	"github.com/daixiang0/gci/pkg/config"
     8  	"github.com/daixiang0/gci/pkg/log"
     9  	"github.com/daixiang0/gci/pkg/section"
    10  )
    11  
    12  type processingFunc = func(args []string, gciCfg config.Config) error
    13  
    14  func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdInSupport bool, processingFunc processingFunc) *cobra.Command {
    15  	var noInlineComments, noPrefixComments, skipGenerated, skipVendor, customOrder, debug *bool
    16  	var sectionStrings, sectionSeparatorStrings *[]string
    17  	cmd := cobra.Command{
    18  		Use:               use,
    19  		Aliases:           aliases,
    20  		Short:             short,
    21  		Long:              long,
    22  		ValidArgsFunction: goFileCompletion,
    23  		RunE: func(cmd *cobra.Command, args []string) error {
    24  			fmtCfg := config.BoolConfig{
    25  				NoInlineComments: *noInlineComments,
    26  				NoPrefixComments: *noPrefixComments,
    27  				Debug:            *debug,
    28  				SkipGenerated:    *skipGenerated,
    29  				SkipVendor:       *skipVendor,
    30  				CustomOrder:      *customOrder,
    31  			}
    32  			gciCfg, err := config.YamlConfig{Cfg: fmtCfg, SectionStrings: *sectionStrings, SectionSeparatorStrings: *sectionSeparatorStrings}.Parse()
    33  			if err != nil {
    34  				return err
    35  			}
    36  			if *debug {
    37  				log.SetLevel(zapcore.DebugLevel)
    38  			}
    39  			return processingFunc(args, *gciCfg)
    40  		},
    41  	}
    42  	if !stdInSupport {
    43  		cmd.Args = cobra.MinimumNArgs(1)
    44  	}
    45  
    46  	// register command as subcommand
    47  	e.rootCmd.AddCommand(&cmd)
    48  
    49  	debug = cmd.Flags().BoolP("debug", "d", false, "Enables debug output from the formatter")
    50  
    51  	sectionHelp := `Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias. The default value is [standard,default].
    52  standard - standard section that Go provides officially, like "fmt"
    53  Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
    54  default - default section, contains all rest imports
    55  blank - blank section, contains all blank imports.
    56  dot - dot section, contains all dot imports.
    57  alias - alias section, contains all alias imports.`
    58  
    59  	skipGenerated = cmd.Flags().Bool("skip-generated", false, "Skip generated files")
    60  	skipVendor = cmd.Flags().Bool("skip-vendor", false, "Skip files inside vendor directory")
    61  
    62  	customOrder = cmd.Flags().Bool("custom-order", false, "Enable custom order of sections")
    63  	sectionStrings = cmd.Flags().StringArrayP("section", "s", section.DefaultSections().String(), sectionHelp)
    64  
    65  	// deprecated
    66  	noInlineComments = cmd.Flags().Bool("NoInlineComments", false, "Drops inline comments while formatting")
    67  	cmd.Flags().MarkDeprecated("NoInlineComments", "Drops inline comments while formatting")
    68  	noPrefixComments = cmd.Flags().Bool("NoPrefixComments", false, "Drops comment lines above an import statement while formatting")
    69  	cmd.Flags().MarkDeprecated("NoPrefixComments", "Drops inline comments while formatting")
    70  	sectionSeparatorStrings = cmd.Flags().StringSliceP("SectionSeparator", "x", section.DefaultSectionSeparators().String(), "SectionSeparators are inserted between Sections")
    71  	cmd.Flags().MarkDeprecated("SectionSeparator", "Drops inline comments while formatting")
    72  	cmd.Flags().MarkDeprecated("x", "Drops inline comments while formatting")
    73  
    74  	return &cmd
    75  }