func deployDeleteCommand(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 { fmt.Println("delete file uri absent") return } 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", "") client := api.NewClient(Host, Port) auth := api.NewAdminAuth(AppID, Secret) for _, directory := range dirs { directory = strings.TrimPrefix(directory, "markdowns") directory = strings.TrimPrefix(directory, "templates") if err := client.Delete(nil, auth, directory); err != nil { fmt.Printf("delete (%s) failed: (%s)\n", directory, err.Error()) continue } fmt.Printf("delete (%s) ok.\n", directory) } }
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)) } } }
func httpStartCommand(ctx *cli.Context) { workdir := ctx.GlobalString("workdir") if workdir == "" { fmt.Println("unknown working directory, please use -w to provide.") os.Exit(1) } // daemon daemon := ctx.GlobalBool("daemon") // options options := app.NewOptions(ctx.GlobalString("host"), ctx.GlobalInt("port")) if err := options.Prepare(workdir); err != nil { fmt.Println("options prepare failed:", err) os.Exit(1) } refresh := ctx.GlobalString("refresh") appid := ctx.String("appid") secret := ctx.String("secret") options.SetRefreshDefault(refresh, time.Minute*10) options.SetApplication(appid, secret) storage := ctx.GlobalString("storage") if storage != "" { options.SetStorargeMax(storage) } // configs configs, err := app.LoadCONFIG(path.Join(options.Root, "h2object.conf")) if err != nil { configs = app.DefaultCONFIG() if err := configs.Save(path.Join(options.Root, "h2object.conf")); err != nil { fmt.Println("h2object.conf saving failed:", err) os.Exit(1) } } logger := log.NewH2OLogger() defer logger.Close() logger.SetConsole(true) configs.SetSection("logs") fenable := configs.BoolDefault("file.enable", false) fname := configs.StringDefault("file.name", "h2o.log") flevel := configs.StringDefault("file.level", "info") fsize := configs.IntDefault("file.rotate_max_size", 1024*1024*1024) fline := configs.IntDefault("file.rotate_max_line", 102400) fdaily := configs.BoolDefault("file.rotate_daily", true) fn := path.Join(options.LogsRoot, fname) if fenable == true { logger.SetFileLog(fn, flevel, fsize, fline, fdaily) } application := app.NewApplication(options, configs, logger) if err := application.Init(); err != nil { fmt.Println("[h2object] init failed:", err) os.Exit(1) } application.Version(version) start(application, daemon) }
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) } }
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) }