2014-06-03 15:08:58 +00:00
|
|
|
package commands
|
2013-12-05 22:36:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-06-03 15:08:58 +00:00
|
|
|
"github.com/github/git-media/gitmedia"
|
2014-06-26 20:44:30 +00:00
|
|
|
"github.com/spf13/cobra"
|
2013-12-05 23:19:30 +00:00
|
|
|
"io/ioutil"
|
2013-12-05 22:36:57 +00:00
|
|
|
"os"
|
2013-12-05 23:19:30 +00:00
|
|
|
"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
|
|
|
|
}
|
2013-12-05 23:19:30 +00:00
|
|
|
|
|
|
|
var sub string
|
2014-06-26 20:44:30 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
sub = args[0]
|
2013-12-05 23:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch sub {
|
|
|
|
case "last":
|
2014-06-26 20:44:30 +00:00
|
|
|
lastLog()
|
2013-12-05 23:19:30 +00:00
|
|
|
case "":
|
2014-06-26 20:44:30 +00:00
|
|
|
listLogs()
|
2013-12-05 23:19:30 +00:00
|
|
|
default:
|
2014-06-26 20:44:30 +00:00
|
|
|
showLog(sub)
|
2013-12-05 23:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 20:44:30 +00:00
|
|
|
func listLogs() {
|
2013-12-05 23:19:30 +00:00
|
|
|
for _, path := range sortedLogs() {
|
2014-06-05 18:19:30 +00:00
|
|
|
Print(path)
|
2013-12-05 23:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 20:44:30 +00:00
|
|
|
func lastLog() {
|
2013-12-05 23:19:30 +00:00
|
|
|
logs := sortedLogs()
|
2014-06-04 15:12:06 +00:00
|
|
|
if len(logs) < 1 {
|
2014-06-05 21:35:15 +00:00
|
|
|
Print("No logs to show")
|
2014-06-04 15:12:06 +00:00
|
|
|
return
|
|
|
|
}
|
2014-06-26 20:44:30 +00:00
|
|
|
showLog(logs[len(logs)-1])
|
2013-12-05 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 20:44:30 +00:00
|
|
|
func showLog(name string) {
|
2014-06-03 15:08:58 +00:00
|
|
|
by, err := ioutil.ReadFile(filepath.Join(gitmedia.LocalLogDir, name))
|
2013-12-05 23:19:30 +00:00
|
|
|
if err != nil {
|
2014-06-05 18:19:30 +00:00
|
|
|
Exit("Error reading log: %s", name)
|
2013-12-05 23:19:30 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 18:19:30 +00:00
|
|
|
Debug("Reading log: %s", name)
|
2013-12-05 23:19:30 +00:00
|
|
|
os.Stdout.Write(by)
|
2013-12-05 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 20:44:30 +00:00
|
|
|
func clearLogs() {
|
2014-06-03 15:08:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-03 15:08:58 +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() {
|
2014-06-05 18:19:30 +00:00
|
|
|
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")
|
2014-06-05 18:19:30 +00:00
|
|
|
Debug("Never seen")
|
2013-12-05 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 23:19:30 +00:00
|
|
|
func sortedLogs() []string {
|
2014-06-03 15:08:58 +00:00
|
|
|
fileinfos, err := ioutil.ReadDir(gitmedia.LocalLogDir)
|
2013-12-05 23:19:30 +00:00
|
|
|
if err != nil {
|
2013-12-05 23:44:52 +00:00
|
|
|
return []string{}
|
2013-12-05 23:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|