github.com/bscott/buffalo@v0.11.1/buffalo/cmd/info.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "reflect" 8 9 "github.com/gobuffalo/buffalo/meta" 10 "github.com/gobuffalo/envy" 11 "github.com/pkg/errors" 12 "github.com/spf13/cobra" 13 ) 14 15 // infoCmd represents the info command 16 var infoCmd = &cobra.Command{ 17 Use: "info", 18 Short: "Prints off diagnostic information useful for debugging.", 19 RunE: func(cmd *cobra.Command, args []string) error { 20 bb := os.Stdout 21 22 bb.WriteString(fmt.Sprintf("### Buffalo Version\n%s\n", Version)) 23 24 bb.WriteString("\n### App Information\n") 25 app := meta.New(".") 26 rv := reflect.ValueOf(app) 27 rt := rv.Type() 28 29 for i := 0; i < rt.NumField(); i++ { 30 f := rt.Field(i) 31 bb.WriteString(fmt.Sprintf("%s=%v\n", f.Name, rv.FieldByName(f.Name).Interface())) 32 } 33 34 return runInfoCmds() 35 }, 36 } 37 38 type infoCommand struct { 39 Name string 40 PathName string 41 Cmd *exec.Cmd 42 InfoLabel string 43 } 44 45 func runInfoCmds() error { 46 47 commands := []infoCommand{ 48 {"Go", envy.Get("GO_BIN", "go"), exec.Command(envy.Get("GO_BIN", "go"), "version"), "\n### Go Version\n"}, 49 {"Go", envy.Get("GO_BIN", "go"), exec.Command(envy.Get("GO_BIN", "go"), "env"), "\n### Go Env\n"}, 50 {"Node", "node", exec.Command("node", "--version"), "\n### Node Version\n"}, 51 {"NPM", "npm", exec.Command("npm", "--version"), "\n### NPM Version\n"}, 52 {"Yarn", "yarn", exec.Command("yarn", "--version"), "\n### Yarn Version\n"}, 53 {"PostgreSQL", "pg_ctl", exec.Command("pg_ctl", "--version"), "\n### PostgreSQL Version\n"}, 54 {"MySQL", "mysql", exec.Command("mysql", "--version"), "\n### MySQL Version\n"}, 55 {"SQLite", "sqlite3", exec.Command("sqlite3", "--version"), "\n### SQLite Version\n"}, 56 {"dep", "dep", exec.Command("dep", "version"), "\n### Dep Version\n"}, 57 {"dep", "dep", exec.Command("dep", "status"), "\n### Dep Status\n"}, 58 } 59 60 for _, cmd := range commands { 61 err := execIfExists(cmd) 62 if err != nil { 63 return errors.WithStack(err) 64 } 65 } 66 67 return nil 68 } 69 70 func execIfExists(infoCmd infoCommand) error { 71 bb := os.Stdout 72 bb.WriteString(infoCmd.InfoLabel) 73 74 if _, err := exec.LookPath(infoCmd.PathName); err != nil { 75 bb.WriteString(fmt.Sprintf("%s Not Found\n", infoCmd.Name)) 76 return nil 77 } 78 79 infoCmd.Cmd.Stdout = bb 80 infoCmd.Cmd.Stderr = bb 81 82 err := infoCmd.Cmd.Run() 83 return err 84 } 85 86 func init() { 87 decorate("info", RootCmd) 88 RootCmd.AddCommand(infoCmd) 89 }