2014-06-03 15:08:58 +00:00
|
|
|
package commands
|
2013-12-05 22:36:57 +00:00
|
|
|
|
|
|
|
import (
|
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"
|
2015-05-13 19:43:41 +00:00
|
|
|
|
2016-05-13 16:38:06 +00:00
|
|
|
"github.com/github/git-lfs/config"
|
2016-08-18 20:20:33 +00:00
|
|
|
"github.com/github/git-lfs/errors"
|
2016-05-23 18:02:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
2013-12-05 22:36:57 +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]
|
2016-05-13 16:38:06 +00:00
|
|
|
by, err := ioutil.ReadFile(filepath.Join(config.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) {
|
2016-05-13 16:38:06 +00:00
|
|
|
err := os.RemoveAll(config.LocalLogDir)
|
2013-12-05 22:36:57 +00:00
|
|
|
if err != nil {
|
2016-05-13 16:38:06 +00:00
|
|
|
Panic(err, "Error clearing %s", config.LocalLogDir)
|
2013-12-05 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 16:38:06 +00:00
|
|
|
Print("Cleared %s", config.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")
|
2016-08-18 20:35:11 +00:00
|
|
|
err := errors.Wrapf(errors.New("Inner error message!"), "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 {
|
2016-05-13 16:38:06 +00:00
|
|
|
fileinfos, err := ioutil.ReadDir(config.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
|
|
|
}
|
|
|
|
|
2015-06-19 20:46:27 +00:00
|
|
|
names := make([]string, 0, len(fileinfos))
|
|
|
|
for _, info := range fileinfos {
|
2015-06-05 16:39:55 +00:00
|
|
|
if info.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
2015-06-19 20:46:27 +00:00
|
|
|
names = append(names, info.Name())
|
2013-12-05 23:19:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 20:46:27 +00:00
|
|
|
return names
|
2013-12-05 23:19:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:36:57 +00:00
|
|
|
func init() {
|
2016-08-31 16:17:13 +00:00
|
|
|
RegisterCommand("logs", logsCommand, func(cmd *cobra.Command) bool {
|
2016-08-10 15:33:25 +00:00
|
|
|
cmd.AddCommand(
|
|
|
|
&cobra.Command{
|
2016-08-22 17:13:37 +00:00
|
|
|
Use: "last",
|
|
|
|
PreRun: resolveLocalStorage,
|
|
|
|
Run: logsLastCommand,
|
2016-08-10 15:33:25 +00:00
|
|
|
},
|
|
|
|
&cobra.Command{
|
2016-08-22 17:13:37 +00:00
|
|
|
Use: "show",
|
|
|
|
PreRun: resolveLocalStorage,
|
|
|
|
Run: logsShowCommand,
|
2016-08-10 15:33:25 +00:00
|
|
|
},
|
|
|
|
&cobra.Command{
|
2016-08-22 17:13:37 +00:00
|
|
|
Use: "clear",
|
|
|
|
PreRun: resolveLocalStorage,
|
|
|
|
Run: logsClearCommand,
|
2016-08-10 15:33:25 +00:00
|
|
|
},
|
|
|
|
&cobra.Command{
|
2016-08-22 17:13:37 +00:00
|
|
|
Use: "boomtown",
|
|
|
|
PreRun: resolveLocalStorage,
|
|
|
|
Run: logsBoomtownCommand,
|
2016-08-10 15:33:25 +00:00
|
|
|
},
|
|
|
|
)
|
2016-08-31 16:17:13 +00:00
|
|
|
return true
|
2016-08-10 15:33:25 +00:00
|
|
|
})
|
2013-12-05 22:36:57 +00:00
|
|
|
}
|