You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.2KB

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "regexp"
  9. "runtime"
  10. log "github.com/Sirupsen/logrus"
  11. "github.com/codegangsta/cli"
  12. "github.com/goanywhere/cmd"
  13. "github.com/goanywhere/crypto"
  14. )
  15. const endpoint = "https://github.com/goanywhere/rex"
  16. var secrets = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*(-_+)")
  17. type project struct {
  18. name string
  19. root string
  20. }
  21. func (self *project) create() {
  22. cmd.Prompt("Fetching project template\n")
  23. var done = make(chan bool)
  24. cmd.Loading(done)
  25. command := exec.Command("git", "clone", "-b", "scaffolds", endpoint, self.name)
  26. command.Dir = cwd
  27. if e := command.Run(); e == nil {
  28. self.root = filepath.Join(cwd, self.name)
  29. // create dotenv under project's root.
  30. filename := filepath.Join(self.root, ".env")
  31. if dotenv, err := os.Create(filename); err == nil {
  32. defer dotenv.Close()
  33. buffer := bufio.NewWriter(dotenv)
  34. buffer.WriteString(fmt.Sprintf("export Rex_Secret_Keys=\"%s, %s\"\n", crypto.Random(64), crypto.Random(32)))
  35. buffer.Flush()
  36. // close loading here as nodejs will take over prompt.
  37. done <- true
  38. // initialize project packages via nodejs.
  39. self.setup()
  40. }
  41. os.RemoveAll(filepath.Join(self.root, ".git"))
  42. os.Remove(filepath.Join(self.root, "README.md"))
  43. } else {
  44. // loading prompt should be closed in anyway.
  45. done <- true
  46. }
  47. }
  48. func (self *project) setup() {
  49. if e := exec.Command("npm", "-v").Run(); e == nil {
  50. cmd.Prompt("Fetching project dependencies\n")
  51. command := exec.Command("npm", "install")
  52. command.Dir = self.root
  53. command.Stdout = os.Stdout
  54. command.Stderr = os.Stderr
  55. command.Run()
  56. } else {
  57. log.Fatalf("Failed to setup project dependecies: nodejs is missing.")
  58. }
  59. }
  60. func New(context *cli.Context) {
  61. var pattern *regexp.Regexp
  62. if runtime.GOOS == "windows" {
  63. pattern = regexp.MustCompile(`\A(?:[0-9a-zA-Z\.\_\-]+\\?)+\z`)
  64. } else {
  65. pattern = regexp.MustCompile(`\A(?:[0-9a-zA-Z\.\_\-]+\/?)+\z`)
  66. }
  67. args := context.Args()
  68. if len(args) != 1 || !pattern.MatchString(args[0]) {
  69. log.Fatal("Please provide a valid project name/path")
  70. } else {
  71. project := new(project)
  72. project.name = args[0]
  73. project.create()
  74. }
  75. }