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