github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/cmd/test/histogram/histogram.go (about)

     1  // Package histogram provides the histogram test command.
     2  package histogram
     3  
     4  import (
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"os"
     9  	"path"
    10  
    11  	"github.com/rclone/rclone/cmd"
    12  	"github.com/rclone/rclone/cmd/test"
    13  	"github.com/rclone/rclone/fs"
    14  	"github.com/rclone/rclone/fs/walk"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  func init() {
    19  	test.Command.AddCommand(commandDefinition)
    20  }
    21  
    22  var commandDefinition = &cobra.Command{
    23  	Use:   "histogram [remote:path]",
    24  	Short: `Makes a histogram of file name characters.`,
    25  	Long: `This command outputs JSON which shows the histogram of characters used
    26  in filenames in the remote:path specified.
    27  
    28  The data doesn't contain any identifying information but is useful for
    29  the rclone developers when developing filename compression.
    30  `,
    31  	Annotations: map[string]string{
    32  		"versionIntroduced": "v1.55",
    33  	},
    34  	Run: func(command *cobra.Command, args []string) {
    35  		cmd.CheckArgs(1, 1, command, args)
    36  		f := cmd.NewFsDir(args)
    37  		ctx := context.Background()
    38  		ci := fs.GetConfig(ctx)
    39  		cmd.Run(false, false, command, func() error {
    40  			var hist [256]int64
    41  			err := walk.ListR(ctx, f, "", false, ci.MaxDepth, walk.ListObjects, func(entries fs.DirEntries) error {
    42  				for _, entry := range entries {
    43  					base := path.Base(entry.Remote())
    44  					for i := range base {
    45  						hist[base[i]]++
    46  					}
    47  				}
    48  				return nil
    49  			})
    50  			if err != nil {
    51  				return err
    52  			}
    53  			enc := json.NewEncoder(os.Stdout)
    54  			// enc.SetIndent("", "\t")
    55  			err = enc.Encode(&hist)
    56  			if err != nil {
    57  				return err
    58  			}
    59  			fmt.Println()
    60  			return nil
    61  		})
    62  	},
    63  }