git-lfs/commands/command_logs.go

109 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"
2014-06-26 20:44:30 +00:00
"github.com/spf13/cobra"
"io/ioutil"
2013-12-05 22:36:57 +00:00
"os"
"path/filepath"
2013-12-05 22:36:57 +00:00
)
2014-06-26 20:44:30 +00:00
var (
clearLogsFlag bool
boomtownFlag bool
2013-12-05 22:36:57 +00:00
2014-06-26 20:44:30 +00:00
logsCmd = &cobra.Command{
Use: "logs",
Short: "View error logs.",
Run: logsCommand,
}
)
2013-12-05 22:36:57 +00:00
2014-06-26 20:44:30 +00:00
func logsCommand(cmd *cobra.Command, args []string) {
if clearLogsFlag {
clearLogs()
2013-12-05 22:36:57 +00:00
}
2014-06-26 20:44:30 +00:00
if boomtownFlag {
boomtown()
2013-12-05 22:36:57 +00:00
return
}
var sub string
2014-06-26 20:44:30 +00:00
if len(args) > 0 {
sub = args[0]
}
switch sub {
case "last":
2014-06-26 20:44:30 +00:00
lastLog()
case "":
2014-06-26 20:44:30 +00:00
listLogs()
default:
2014-06-26 20:44:30 +00:00
showLog(sub)
}
}
2014-06-26 20:44:30 +00:00
func listLogs() {
for _, path := range sortedLogs() {
Print(path)
}
}
2014-06-26 20:44:30 +00:00
func lastLog() {
logs := sortedLogs()
if len(logs) < 1 {
2014-06-05 21:35:15 +00:00
Print("No logs to show")
return
}
2014-06-26 20:44:30 +00:00
showLog(logs[len(logs)-1])
}
2014-06-26 20:44:30 +00:00
func showLog(name string) {
by, err := ioutil.ReadFile(filepath.Join(gitmedia.LocalLogDir, name))
if err != nil {
Exit("Error reading log: %s", name)
}
Debug("Reading log: %s", name)
os.Stdout.Write(by)
2013-12-05 22:36:57 +00:00
}
2014-06-26 20:44:30 +00:00
func clearLogs() {
err := os.RemoveAll(gitmedia.LocalLogDir)
2013-12-05 22:36:57 +00:00
if err != nil {
2014-06-05 18:48:23 +00:00
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
}
2014-06-26 20:44:30 +00:00
func boomtown() {
Debug("Debug message")
2013-12-05 22:36:57 +00:00
err := errors.New("Error!")
2014-06-05 18:48:23 +00:00
Panic(err, "Welcome to Boomtown")
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() {
2014-06-26 20:44:30 +00:00
logsCmd.Flags().BoolVarP(&clearLogsFlag, "clear", "c", false, "Clear existing error logs")
logsCmd.Flags().BoolVarP(&boomtownFlag, "boomtown", "b", false, "Trigger a panic")
RootCmd.AddCommand(logsCmd)
2013-12-05 22:36:57 +00:00
}