gitee.com/quant1x/engine@v1.8.4/command/command_tools.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/engine/tools"
     6  	"gitee.com/quant1x/num"
     7  	"gitee.com/quant1x/pkg/tools/tail"
     8  	cmder "github.com/spf13/cobra"
     9  	"slices"
    10  	"strings"
    11  )
    12  
    13  const (
    14  	toolsCommand     = "tool"
    15  	toolsDescription = "工具"
    16  )
    17  
    18  var (
    19  	CmdTools    *cmder.Command = nil // 工具集合
    20  	cmdToolTail *cmder.Command = nil // tail工具
    21  )
    22  
    23  func initTools() {
    24  	CmdTools = &cmder.Command{
    25  		Use:     toolsCommand,
    26  		Example: Application + " " + toolsCommand + " --help",
    27  		Short:   toolsDescription,
    28  		Run: func(cmd *cmder.Command, args []string) {
    29  
    30  		},
    31  	}
    32  	toolsInitTail()
    33  	CmdTools.AddCommand(cmdToolTail)
    34  }
    35  
    36  func toolsInitTail() {
    37  	var taiConfig tail.Config
    38  	var n int
    39  	cmdToolTail = &cmder.Command{
    40  		Use:                "tail",
    41  		Example:            Application + " tool tail -f runtime.log",
    42  		Short:              "文件末端阅览",
    43  		DisableFlagParsing: true,
    44  		Run: func(cmd *cmder.Command, args []string) {
    45  			for i := 0; i < len(args); i++ {
    46  				args[i] = strings.TrimSpace(args[i])
    47  			}
    48  			if len(args) != 2 || slices.Contains(args, "--help") || slices.Contains(args, "-h") {
    49  				_ = cmd.Usage()
    50  				return
    51  			}
    52  			if args[0] == "-f" {
    53  				taiConfig.Follow = true
    54  				taiConfig.Poll = true
    55  			} else if args[0][:1] == "-" {
    56  				n = int(num.AnyToInt64(args[0]))
    57  				n = -n
    58  			}
    59  			name := args[1]
    60  			if n > 0 {
    61  				tools.TailFileWithNumber(name, taiConfig, n)
    62  			} else {
    63  				done := make(chan bool)
    64  				tools.TailFile(name, taiConfig, done)
    65  				<-done
    66  			}
    67  		},
    68  		//PreRunE: func(cmd *cmder.Command, args []string) error {
    69  		//	//fmt.Println(args)
    70  		//	if slices.Contains(args, "--help") || slices.Contains(args, "-h") {
    71  		//		cmd.Usage()
    72  		//	}
    73  		//	return nil
    74  		//},
    75  	}
    76  	cmdToolTail.SetUsageFunc(func(command *cmder.Command) error {
    77  		fmt.Println("Usage:\n" + Application + " tool tail [-f] [-n #] [file ...]")
    78  		return nil
    79  	})
    80  	cmdToolTail.Flags().BoolVarP(&taiConfig.Follow, commandDefaultLongFlag, "f", false, "一直等待新数据添加到文件")
    81  }