2014-06-03 15:08:58 +00:00
|
|
|
package commands
|
2013-12-05 22:36:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
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 (
|
|
|
|
logsCmd = &cobra.Command{
|
|
|
|
Use: "logs",
|
2014-06-26 21:22:24 +00:00
|
|
|
Short: "View error logs",
|
2014-06-26 20:44:30 +00:00
|
|
|
Run: logsCommand,
|
|
|
|
}
|
2013-12-05 22:36:57 +00:00
|
|
|
|
2014-06-26 21:40:44 +00:00
|
|
|
logsLastCmd = &cobra.Command{
|
|
|
|
Use: "last",
|
|
|
|
Short: "View latest error log",
|
|
|
|
Run: logsLastCommand,
|
2013-12-05 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 21:40:44 +00:00
|
|
|
logsShowCmd = &cobra.Command{
|
|
|
|
Use: "show",
|
|
|
|
Short: "View a single error log",
|
|
|
|
Run: logsShowCommand,
|
2013-12-05 22:36:57 +00:00
|
|
|
}
|
2013-12-05 23:19:30 +00:00
|
|
|
|
2014-06-26 21:40:44 +00:00
|
|
|
logsClearCmd = &cobra.Command{
|
|
|
|
Use: "clear",
|
|
|
|
Short: "Clear all logs",
|
|
|
|
Run: logsClearCommand,
|
2013-12-05 23:19:30 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 21:40:44 +00:00
|
|
|
logsBoomtownCmd = &cobra.Command{
|
|
|
|
Use: "boomtown",
|
|
|
|
Short: "Trigger a sample error",
|
|
|
|
Run: logsBoomtownCommand,
|
2013-12-05 23:19:30 +00:00
|
|
|
}
|
2014-06-26 21:40:44 +00:00
|
|
|
)
|
2013-12-05 23:19:30 +00:00
|
|
|
|
2014-06-26 21:40:44 +00:00
|
|
|
func logsCommand(cmd *cobra.Command, args []string) {
|
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 21:40:44 +00:00
|
|
|
func logsLastCommand(cmd *cobra.Command, args []string) {
|
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 21:40:44 +00:00
|
|
|
|
|
|
|
logsShowCommand(cmd, logs[len(logs)-1:])
|
2013-12-05 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 21:40:44 +00:00
|
|
|
func logsShowCommand(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
Print("Supply a log name.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name := args[0]
|
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 21:40:44 +00:00
|
|
|
func logsClearCommand(cmd *cobra.Command, args []string) {
|
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-26 21:44:50 +00:00
|
|
|
Print("Cleared %s", gitmedia.LocalLogDir)
|
2013-12-05 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 21:40:44 +00:00
|
|
|
func logsBoomtownCommand(cmd *cobra.Command, args []string) {
|
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 21:40:44 +00:00
|
|
|
logsCmd.AddCommand(logsLastCmd, logsShowCmd, logsClearCmd, logsBoomtownCmd)
|
2014-06-26 20:44:30 +00:00
|
|
|
RootCmd.AddCommand(logsCmd)
|
2013-12-05 22:36:57 +00:00
|
|
|
}
|