git-lfs/commands/command_logs.go
risk danger olson a12cdc889f RegisterCommand() can no longer disable commands
the config is available at init(), so isCommandEnabled() checks can be
pulled out of the RegisterCommand() callback, simplifying its signature.
2016-09-01 10:09:38 -06:00

88 lines
1.7 KiB
Go

package commands
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/github/git-lfs/config"
"github.com/github/git-lfs/errors"
"github.com/spf13/cobra"
)
func logsCommand(cmd *cobra.Command, args []string) {
for _, path := range sortedLogs() {
Print(path)
}
}
func logsLastCommand(cmd *cobra.Command, args []string) {
logs := sortedLogs()
if len(logs) < 1 {
Print("No logs to show")
return
}
logsShowCommand(cmd, logs[len(logs)-1:])
}
func logsShowCommand(cmd *cobra.Command, args []string) {
if len(args) == 0 {
Print("Supply a log name.")
return
}
name := args[0]
by, err := ioutil.ReadFile(filepath.Join(config.LocalLogDir, name))
if err != nil {
Exit("Error reading log: %s", name)
}
Debug("Reading log: %s", name)
os.Stdout.Write(by)
}
func logsClearCommand(cmd *cobra.Command, args []string) {
err := os.RemoveAll(config.LocalLogDir)
if err != nil {
Panic(err, "Error clearing %s", config.LocalLogDir)
}
Print("Cleared %s", config.LocalLogDir)
}
func logsBoomtownCommand(cmd *cobra.Command, args []string) {
Debug("Debug message")
err := errors.Wrapf(errors.New("Inner error message!"), "Error")
Panic(err, "Welcome to Boomtown")
Debug("Never seen")
}
func sortedLogs() []string {
fileinfos, err := ioutil.ReadDir(config.LocalLogDir)
if err != nil {
return []string{}
}
names := make([]string, 0, len(fileinfos))
for _, info := range fileinfos {
if info.IsDir() {
continue
}
names = append(names, info.Name())
}
return names
}
func init() {
RegisterCommand("logs", logsCommand, func(cmd *cobra.Command) {
cmd.AddCommand(
NewCommand("last", logsLastCommand),
NewCommand("show", logsShowCommand),
NewCommand("clear", logsClearCommand),
NewCommand("boomtown", logsBoomtownCommand),
)
})
}