Esempio n. 1
0
func themePullCommand(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
	}
	stderr := os.Stderr

	if len(ctx.Args()) != 1 {
		fmt.Fprintln(stderr, "please input theme id with format: provider/name:version")
		os.Exit(1)
	}

	provider, name, version, err := parse_theme(ctx.Args()[0])
	if err != nil {
		fmt.Fprintln(stderr, "package format: provider/name:version, please try again")
		os.Exit(1)
	}

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

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

	var pkg Package
	pkg.Provider = provider
	pkg.Name = name
	pkg.Version = version

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

	bar := pb.New(int(pkg.ArchiveLen)).SetUnits(pb.U_BYTES)
	bar.Prefix(fmt.Sprintf("%s/%s:%s ", pkg.Provider, pkg.Name, pkg.Version))
	bar.Start()
	// create multi writer
	rd := pb.NewPbReader(pkg.ArchiveReader, bar)

	if err := archive.Untar(rd, directory, nil); err != nil {
		fmt.Fprintln(stderr, err.Error())
		os.Exit(1)
	}
	bar.FinishPrint(fmt.Sprintf("%s/%s:%s pulled succussfully.", pkg.Provider, pkg.Name, pkg.Version))
}
Esempio n. 2
0
func (cli *Client) ThemePush(token string, pkg *Package) error {
	params := url.Values{}
	params.Set("token", token)

	URL := rpc.BuildHttpURL(cli.addr,
		fmt.Sprintf("/themes/push/%s/%s/%s", pkg.Provider, pkg.Name, pkg.Version),
		params)

	var b bytes.Buffer
	wr := multipart.NewWriter(&b)

	if err := wr.WriteField("catagory", fmt.Sprintf("%d", pkg.Catagory)); err != nil {
		return err
	}
	if err := wr.WriteField("description", pkg.Description); err != nil {
		return err
	}
	if err := wr.WriteField("status", fmt.Sprintf("%d", pkg.Status)); err != nil {
		return err
	}
	if err := wr.WriteField("price", fmt.Sprintf("%f", pkg.Price)); err != nil {
		return err
	}
	p, err := wr.CreateFormFile("file", pkg.ArchiveName)
	if err != nil {
		return err
	}
	defer pkg.ArchiveReader.Close()

	if _, err := io.Copy(p, pkg.ArchiveReader); err != nil {
		return err
	}
	if err := wr.Close(); err != nil {
		return err
	}

	bar := pb.New(b.Len()).SetUnits(pb.U_BYTES)
	bar.Prefix(fmt.Sprintf("%s/%s:%s ", pkg.Provider, pkg.Name, pkg.Version))
	bar.Start()

	// create multi writer
	rd := pb.NewPbReader(&b, bar)

	if err := cli.conn.Post(nil, URL, wr.FormDataContentType(), rd, int64(b.Len()), nil); err != nil {
		return err
	}
	bar.FinishPrint(fmt.Sprintf("%s/%s:%s pushed succussfully.", pkg.Provider, pkg.Name, pkg.Version))
	return nil
}
Esempio n. 3
0
func deployPullCommand(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
	}

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

	h2oconf.SetSection("deploy")
	Host := h2oconf.StringDefault("host", "")
	Port := h2oconf.IntDefault("port", 80)
	AppID := h2oconf.StringDefault("appid", "")
	Secret := h2oconf.StringDefault("secret", "")

	client := api.NewClient(Host, Port)
	auth := api.NewAdminAuth(AppID, Secret)

	dirs := ctx.Args()
	if len(dirs) == 0 {
		body, size, err := client.Download(nil, auth, path.Join("/", ".export"))
		if err != nil {
			fmt.Fprintln(stderr, err.Error())
			os.Exit(1)
		}

		bar := pb.New(int(size)).SetUnits(pb.U_BYTES)
		bar.Prefix("/ ")
		bar.Start()
		// create multi writer
		rd := pb.NewPbReader(body, bar)
		if err := archive.Untar(rd, directory, nil); err != nil {
			fmt.Fprintln(stderr, err.Error())
			os.Exit(1)
		}	
		bar.FinishPrint(fmt.Sprintf("/ pulled succussfully without <h2object.conf> file."))
	} else {
		for _, dir := range dirs {
			if !strings.HasPrefix(dir, "markdowns") &&
			   !strings.HasPrefix(dir, "templates") &&
			   !strings.HasPrefix(dir, "statics") &&
			   !strings.HasPrefix(dir, "storage") &&
			   !strings.HasPrefix(dir, "indexes") {
				fmt.Fprintf(stderr, "push path ignored: %s\n", dir)	
				continue
			}
			
			body, size, err := client.Download(nil, auth, path.Join("/", dir + ".export"))
			if err != nil {
				fmt.Fprintln(stderr, err.Error())
				os.Exit(1)
			}

			bar := pb.New(int(size)).SetUnits(pb.U_BYTES)
			bar.Prefix(dir + " ")
			bar.Start()
			// create multi writer
			rd := pb.NewPbReader(body, bar)
			if err := archive.Untar(rd, path.Join(directory, dir), nil); err != nil {
				fmt.Fprintln(stderr, err.Error())
				os.Exit(1)
			}	
			bar.FinishPrint(fmt.Sprintf("%s pulled succussfully.", dir))			
		}
	}
}
Esempio n. 4
0
func deployPushCommand(ctx *cli.Context) {
	workdir := ctx.GlobalString("workdir")
	if workdir == "" {
		fmt.Println("unknown working directory, please use -w to provide.")
		os.Exit(1)
	}

	// directory
	absworkdir, err := filepath.Abs(workdir)
	if err != nil {
		fmt.Println("workdir:", err)
		return
	}

	dirs := ctx.Args()
	if len(dirs) == 0 {
		dirs = append(dirs, "h2object.conf:h2object:third", "markdowns", "templates", "statics")
	}

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

	h2oconf.SetSection("deploy")
	Host := h2oconf.StringDefault("host", "")
	Port := h2oconf.IntDefault("port", 80)
	AppID := h2oconf.StringDefault("appid", "")
	Secret := h2oconf.StringDefault("secret", "")

	h2oconf.SetSection("h2object")
	markdown_suffixs := h2oconf.MultiStringDefault("markdown.suffix", ",", []string{"md", "markdown"})

	client := api.NewClient(Host, Port)
	auth := api.NewAdminAuth(AppID, Secret)
	
	for _, directory := range dirs {
		if strings.HasPrefix(directory, "markdowns") {

			size := util.FolderSize(path.Join(absworkdir, directory), []string{})

			bar := pb.New(int(size)).SetUnits(pb.U_BYTES)
			bar.Prefix(directory + " ")
			bar.Start()

			if err := push(client, auth, bar, absworkdir, path.Join(absworkdir, directory), []string{}); err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			bar.FinishPrint(fmt.Sprintf("%s push completed.", directory))
			continue
		}

		if strings.HasPrefix(directory, "templates") {

			size := util.FolderSize(path.Join(absworkdir, directory), []string{})

			bar := pb.New(int(size)).SetUnits(pb.U_BYTES)

			bar.Prefix(directory + " ")
			bar.Start()

			if err := push(client, auth, bar, absworkdir, path.Join(absworkdir, directory), []string{}); err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			bar.FinishPrint(fmt.Sprintf("%s push completed.", directory))
			continue
		}

		if strings.HasPrefix(directory, "statics") {
			size := util.FolderSize(path.Join(absworkdir, directory), markdown_suffixs)
			bar := pb.New(int(size)).SetUnits(pb.U_BYTES)
			bar.Prefix(directory + " ")
			bar.Start()

			if err := push(client, auth, bar, absworkdir, path.Join(absworkdir, directory), markdown_suffixs); err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			bar.FinishPrint(fmt.Sprintf("%s push completed.", directory))
			continue
		}

		if strings.HasPrefix(directory, "h2object.conf") {
			ds := strings.Split(directory, ":")
			
			sections := []string{}
			if len(ds) > 1 {
				sections = append(sections, ds[1:]...)
			} else {
				sections = append(sections, "h2object", "third")
			}

			for _, section := range sections {
				if h2oconf.HasSection(section) {
					h2oconf.SetSection(section)
					opts := h2oconf.Options("")

					data := map[string]string{}
					for _, opt := range opts {
						if section == "h2object" && (opt == "appid" || opt == "secret") {
							continue
						}
						data[opt] = h2oconf.StringDefault(opt, "")
					}

					if err := client.SetConfig(nil, auth, section, data); err != nil {
						fmt.Println(err)
						os.Exit(1)
					}
					fmt.Printf("section (%s) push succeed.\n", section)
				}
			}
			continue
		}

		fmt.Printf("push path ignored: %s\n", directory)
	}
}