Example #1
0
func newapp(args []string) {
	if len(args) == 0 {
		eprintf(newShortDesc)
	}
	filepath.Join(peony.GetPeonyPath(), "templates")
	importPath := args[0]

	gopath := build.Default.GOPATH

	if gopath == "" {
		eprintf("please set the GOPATH\n", importPath)
	}

	if filepath.IsAbs(importPath) {
		eprintf("importpath[%s] looks like the file path.\n", importPath)
	}

	if _, err := build.Import(importPath, "", build.FindOnly); err == nil {
		eprintf("importpath[%s] already exist.\n", importPath)
	}

	if _, err := build.Import(peony.PEONY_IMPORTPATH, "", build.FindOnly); err != nil {
		eprintf("peony source is required.\n")
	}

	tmplatesPath := filepath.Join(peony.GetPeonyPath(), "templates")
	errorsPath := filepath.Join(peony.GetPeonyPath(), "views", "errors")

	srcPath := filepath.Join(filepath.SplitList(gopath)[0], "src")
	appPath := filepath.Join(srcPath, filepath.FromSlash(importPath))
	if err := os.Mkdir(appPath, 0777); err != nil {
		eprintf("mdir app dir error, %s\n", err.Error())
	}
	if err := copyDir(tmplatesPath, appPath); err != nil {
		eprintf("copy dir error, %s\n", err.Error())
	}
	if err := copyDir(errorsPath, filepath.Join(appPath, "app", "views", "errors")); err != nil {
		eprintf("copy dir error, %s\n", err.Error())
	}
	appName := filepath.Base(filepath.FromSlash(importPath))
	param := map[string]string{
		"AppName":    appName,
		"ImportPath": importPath,
		"SecKey":     peony.GenSecKey(),
	}
	if err := genConfig(appPath, param); err != nil {
		eprintf("generator configure error, %s\n", err.Error())
	}
	fmt.Println("app already is ready, please execute command: peony run", importPath)
}
Example #2
0
func tryTarErrors(srcDir, appName string, tarWriter *tar.Writer) {
	errorsDir := filepath.Join(srcDir, "app", "views", "errors")
	_, err := os.Stat(errorsDir)
	if err == nil {
		return
	}
	if err != nil && !os.IsNotExist(err) {
		return
	}
	path := peony.GetPeonyPath()
	errsPath := filepath.Join(path, "views", "errors")
	filepath.Walk(errsPath, func(srcPath string, info os.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}

		srcFile, err := os.Open(srcPath)
		panicOnError(err, "Failed to read source file")
		defer srcFile.Close()

		header := &tar.Header{
			Name:    filepath.Join(appName, "app", strings.TrimLeft(srcPath[len(path):], string(os.PathSeparator))),
			Size:    info.Size(),
			Mode:    int64(info.Mode()),
			ModTime: info.ModTime(),
		}
		targzWrite(tarWriter, header, srcFile)

		return nil
	})

}
Example #3
0
func NewAgent(app *peony.App) (agent *Agent, err error) {
	agent = &Agent{app: app}
	port := peony.GetRandomListenPort()
	targetSvrUrl := &url.URL{Scheme: "http", Host: fmt.Sprintf("127.0.0.1:%d", port)}
	agent.proxy = httputil.NewSingleHostReverseProxy(targetSvrUrl)
	agent.notifier = peony.NewNotifier()
	agent.forceRefresh = true
	var binPath string
	binPath, err = GetBinPath(app)
	if err != nil {
		return
	}
	agent.AppPort = port
	agent.appBinPath = binPath
	//watch template. template should first watched by notifier
	agent.templateLoader = peony.NewTemplateLoader([]string{
		path.Join(path.Join(peony.GetPeonyPath(), "views")),
	})
	agent.templateLoader.FuncMap["IsDevMode"] = func() bool { return true }
	if err := agent.templateLoader.Refresh(); err != nil {
		panic(err)
	}
	agent.notifier.Watch(agent)
	return
}