github.com/git-lfs/git-lfs@v2.5.2+incompatible/commands/command_logs.go (about)

     1  package commands
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/git-lfs/git-lfs/errors"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  func logsCommand(cmd *cobra.Command, args []string) {
    13  	for _, path := range sortedLogs() {
    14  		Print(path)
    15  	}
    16  }
    17  
    18  func logsLastCommand(cmd *cobra.Command, args []string) {
    19  	logs := sortedLogs()
    20  	if len(logs) < 1 {
    21  		Print("No logs to show")
    22  		return
    23  	}
    24  
    25  	logsShowCommand(cmd, logs[len(logs)-1:])
    26  }
    27  
    28  func logsShowCommand(cmd *cobra.Command, args []string) {
    29  	if len(args) == 0 {
    30  		Print("Supply a log name.")
    31  		return
    32  	}
    33  
    34  	name := args[0]
    35  	by, err := ioutil.ReadFile(filepath.Join(cfg.LocalLogDir(), name))
    36  	if err != nil {
    37  		Exit("Error reading log: %s", name)
    38  	}
    39  
    40  	Debug("Reading log: %s", name)
    41  	os.Stdout.Write(by)
    42  }
    43  
    44  func logsClearCommand(cmd *cobra.Command, args []string) {
    45  	err := os.RemoveAll(cfg.LocalLogDir())
    46  	if err != nil {
    47  		Panic(err, "Error clearing %s", cfg.LocalLogDir())
    48  	}
    49  
    50  	Print("Cleared %s", cfg.LocalLogDir())
    51  }
    52  
    53  func logsBoomtownCommand(cmd *cobra.Command, args []string) {
    54  	Debug("Debug message")
    55  	err := errors.Wrapf(errors.New("Inner error message!"), "Error")
    56  	Panic(err, "Welcome to Boomtown")
    57  	Debug("Never seen")
    58  }
    59  
    60  func sortedLogs() []string {
    61  	fileinfos, err := ioutil.ReadDir(cfg.LocalLogDir())
    62  	if err != nil {
    63  		return []string{}
    64  	}
    65  
    66  	names := make([]string, 0, len(fileinfos))
    67  	for _, info := range fileinfos {
    68  		if info.IsDir() {
    69  			continue
    70  		}
    71  		names = append(names, info.Name())
    72  	}
    73  
    74  	return names
    75  }
    76  
    77  func init() {
    78  	RegisterCommand("logs", logsCommand, func(cmd *cobra.Command) {
    79  		cmd.AddCommand(
    80  			NewCommand("last", logsLastCommand),
    81  			NewCommand("show", logsShowCommand),
    82  			NewCommand("clear", logsClearCommand),
    83  			NewCommand("boomtown", logsBoomtownCommand),
    84  		)
    85  	})
    86  }