Beispiel #1
0
func do_export(ctx *context, ctrl *ext.Controller) bool {
	r := ctrl.Request
	if r.MethodToLower() != "get" {
		ctrl.JsonError(http.StatusMethodNotAllowed, errors.New("method not allowed"))
		return true
	}

	directory := path.Join(ctx.app.Options.Root, r.TrimSuffixURI(".export"))
	var tarOpt archive.TarOptions
	tarOpt.ExcludePatterns = append(tarOpt.ExcludePatterns,
		".tmp", ".h2object", "h2object.pid", "h2object.conf")
	tarOpt.Compression = archive.Gzip
	rd, err := archive.TarWithOptions(directory, &tarOpt)
	if err != nil {
		ctrl.JsonError(http.StatusInternalServerError, err)
		return true
	}
	defer rd.Close()

	_, fname := path.Split(r.TrimSuffixURI(".export"))
	if fname == "" {
		fname = "h2object"
	}
	fname = fname + ".tar.gz"
	fn := path.Join(ctx.app.Options.TempRoot, fname)

	fd, err := os.Create(fn)
	if err != nil {
		ctrl.JsonError(http.StatusInternalServerError, err)
		return true
	}
	if _, err := io.Copy(fd, rd); err != nil {
		ctrl.JsonError(http.StatusInternalServerError, err)
		return true
	}
	fd.Close()

	fout, err := os.Open(fn)
	if err != nil {
		ctrl.JsonError(http.StatusInternalServerError, err)
		return true
	}
	fstat, err := fout.Stat()
	if err != nil {
		ctrl.JsonError(http.StatusInternalServerError, err)
		return true
	}

	ctrl.Binary(fname, fout, fstat.Size())
	return true
}
Beispiel #2
0
func themePushCommand(ctx *cli.Context) {
	workdir := ctx.GlobalString("workdir")
	if workdir == "" {
		fmt.Println("unknown working directory, please use -w to provide.")
		os.Exit(1)
	}
	// directory
	directory, err := filepath.Abs(workdir)
	if err != nil {
		fmt.Println("workdir:", err)
		return
	}

	stdout := os.Stdout
	stderr := os.Stderr

	// auth check
	config, err := LoadConfigFile(directory)
	if err != nil {
		fmt.Fprintln(stderr, err.Error())
		os.Exit(1)
	}

	if config.Auth.Token == "" || config.Auth.Secret == "" {
		fmt.Fprintln(stdout, "theme push need login first. ")
		os.Exit(1)
	}

	h2oconf, err := app.LoadCONFIG(path.Join(directory, "h2object.conf"))
	if err != nil {
		fmt.Fprintln(stdout, err)
		os.Exit(1)
	}

	host := ctx.String("Host")
	port := ctx.Int("Port")
	client := NewClient(directory, host, port)

	var pkg Package
	h2oconf.SetSection("theme")
	pkg.Provider = h2oconf.StringDefault("provider", "")
	if pkg.Provider == "" {
		fmt.Fprintln(stderr, "please set h2object.conf [theme]provider first.")
		os.Exit(1)
	}
	pkg.Name = h2oconf.StringDefault("name", "")
	if pkg.Name == "" {
		fmt.Fprintln(stderr, "please set h2object.conf [theme]name first.")
		os.Exit(1)
	}

	pkg.Description = h2oconf.StringDefault("description", "")
	if pkg.Description == "" {
		fmt.Fprintln(stderr, "please set h2object.conf [theme]description first.")
		os.Exit(1)
	}

	pkg.Version = h2oconf.StringDefault("version", "1.0.0")
	public := h2oconf.BoolDefault("public", false)
	pkg.Status = 1
	if public == true {
		pkg.Status = 2
	}
	pkg.Price = h2oconf.FloatDefault("price", 0.0)
	pkg.Catagory = int64(h2oconf.IntDefault("catagory", 1))

	var tarOpt archive.TarOptions
	tarOpt.ExcludePatterns = append(tarOpt.ExcludePatterns, ".h2object")
	tarOpt.Compression = archive.Gzip
	rd, err := archive.TarWithOptions(directory, &tarOpt)
	if err != nil {
		fmt.Fprintln(stderr, err.Error())
		os.Exit(1)
	}
	defer rd.Close()
	pkg.ArchiveReader = rd
	pkg.ArchiveName = pkg.Version + ".tar.gz"

	if err := client.ThemePush(config.Auth.Token, &pkg); err != nil {
		fmt.Fprintln(stderr, err.Error())
		os.Exit(1)
	}
	os.Exit(0)
}