git-lfs/commands/command_logs.go

104 lines
1.8 KiB
Go
Raw Normal View History

package commands
2013-12-05 22:36:57 +00:00
import (
"errors"
"fmt"
"github.com/github/git-media/gitmedia"
"io/ioutil"
2013-12-05 22:36:57 +00:00
"os"
"path/filepath"
2013-12-05 22:36:57 +00:00
)
2013-12-05 23:24:13 +00:00
type LogsCommand struct {
2013-12-05 22:36:57 +00:00
ClearLogs bool
Boomtown bool
*Command
}
2013-12-05 23:24:13 +00:00
func (c *LogsCommand) Setup() {
2013-12-05 22:36:57 +00:00
c.FlagSet.BoolVar(&c.ClearLogs, "clear", false, "Clear existing error logs")
c.FlagSet.BoolVar(&c.Boomtown, "boomtown", false, "Trigger a panic")
}
2013-12-05 23:24:13 +00:00
func (c *LogsCommand) Run() {
2013-12-05 22:36:57 +00:00
if c.ClearLogs {
c.clear()
}
if c.Boomtown {
c.boomtown()
return
}
var sub string
if len(c.SubCommands) > 0 {
sub = c.SubCommands[0]
}
switch sub {
case "last":
2013-12-05 23:24:13 +00:00
c.lastLog()
case "":
2013-12-05 23:24:13 +00:00
c.listLogs()
default:
2013-12-05 23:24:13 +00:00
c.showLog(sub)
}
}
2013-12-05 23:24:13 +00:00
func (c *LogsCommand) listLogs() {
for _, path := range sortedLogs() {
gitmedia.Print(path)
}
}
2013-12-05 23:24:13 +00:00
func (c *LogsCommand) lastLog() {
logs := sortedLogs()
2013-12-05 23:24:13 +00:00
c.showLog(logs[len(logs)-1])
}
2013-12-05 23:24:13 +00:00
func (c *LogsCommand) showLog(name string) {
by, err := ioutil.ReadFile(filepath.Join(gitmedia.LocalLogDir, name))
if err != nil {
gitmedia.Exit("Error reading log: %s", name)
}
gitmedia.Debug("Reading log: %s", name)
os.Stdout.Write(by)
2013-12-05 22:36:57 +00:00
}
2013-12-05 23:24:13 +00:00
func (c *LogsCommand) clear() {
err := os.RemoveAll(gitmedia.LocalLogDir)
2013-12-05 22:36:57 +00:00
if err != nil {
gitmedia.Panic(err, "Error clearing %s", gitmedia.LocalLogDir)
2013-12-05 22:36:57 +00:00
}
fmt.Println("Cleared", gitmedia.LocalLogDir)
2013-12-05 22:36:57 +00:00
}
2013-12-05 23:24:13 +00:00
func (c *LogsCommand) boomtown() {
gitmedia.Debug("Debug message")
2013-12-05 22:36:57 +00:00
err := errors.New("Error!")
gitmedia.Panic(err, "Welcome to Boomtown")
gitmedia.Debug("Never seen")
2013-12-05 22:36:57 +00:00
}
func sortedLogs() []string {
fileinfos, err := ioutil.ReadDir(gitmedia.LocalLogDir)
if err != nil {
return []string{}
}
names := make([]string, len(fileinfos))
for index, info := range fileinfos {
names[index] = info.Name()
}
return names
}
2013-12-05 22:36:57 +00:00
func init() {
2013-12-05 23:24:13 +00:00
registerCommand("logs", func(c *Command) RunnableCommand {
return &LogsCommand{Command: c}
2013-12-05 22:36:57 +00:00
})
}