cmd

package
v0.0.0-...-bbd21b2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2021 License: Apache-2.0 Imports: 32 Imported by: 1

Documentation

Overview

You can use the "packr clean" command to clean up this, and any other packr generated files.

Index

Constants

View Source
const (
	// Version constant
	Version = "3.2.3"
)

Variables

View Source
var Create = &cobra.Command{
	Use:   "create [collection-name]",
	Short: "Create photo collection",
	Long:  `create is for creating new photo collection.`,
	Args:  cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		s := spinner.New(spinner.CharSets[21], 100*time.Millisecond)
		s.Prefix = "■ Creating a new collection... "
		s.Start()
		time.Sleep(1 * time.Second)
		if _, err := os.Stat(args[0]); !os.IsNotExist(err) {
			color.Yellow("`%s` already exists!", args[0])
			os.Exit(1)
		}
		cwd, err := os.Getwd()
		if err != nil {
			color.Yellow("`%s`", err)
		}

		err = os.MkdirAll(args[0], os.ModePerm)
		if err != nil {
			color.Yellow("%s", err)
		}

		folders := []string{
			filepath.Join("photos", "cover"),
			filepath.Join("photos", "avatar"),
			filepath.Join("photos", "collection"),
			filepath.Join(".moul", "photos", "cover"),
			filepath.Join(".moul", "photos", "avatar"),
			filepath.Join(".moul", "photos", "collection"),
		}
		for _, folder := range folders {
			os.MkdirAll(filepath.Join(".", args[0], folder), os.ModePerm)
		}

		box := packr.New("assets", "./assets")

		mtoml, _ := box.FindString("moul.toml")
		ioutil.WriteFile(filepath.Join(args[0], "moul.toml"), []byte(mtoml), 0644)

		s.Stop()
		fmt.Print("\nSuccess! Created collection at")
		color.Blue(" `%s`", filepath.Join(cwd, args[0]))

		fmt.Print("\n■ Add your `cover`, `avatar`, and `collection`\ninto")
		color.Blue(" `%s`", filepath.Join(args[0], "photos", "cover|avatar|collection"))
		fmt.Print("■ Add your information in")
		color.Blue(" `%s`", filepath.Join(args[0], "moul.toml"))
		fmt.Print("\n● Preview your photo collection:")
		color.Green(" `moul preview`")
		fmt.Print("● Export your photo collection:")
		color.Green(" `moul export`")
	},
}

Create cmd

View Source
var Export = &cobra.Command{
	Use:   "export",
	Short: "Export photo collection",
	Long:  `Export photo collection to static website that can be deploy anywhere.`,
	Run: func(cmd *cobra.Command, args []string) {
		v := semver.MustParse(Version)
		latest, found, _ := selfupdate.DetectLatest("moulco/moul")
		if found && !latest.Version.LTE(v) {
			color.Yellow("Newer version is available for update.")
			fmt.Print("Update to latest version by:")
			color.Green(" moul update\n\n")
		}

		s := spinner.New(spinner.CharSets[21], 100*time.Millisecond)
		s.Prefix = "■ Exporting photo collection... "
		s.Start()
		start := time.Now()

		dir, err := internal.GetDirectory()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		collectionPath := filepath.Join(dir, "photos", "collection")
		if _, err := os.Stat(collectionPath); os.IsNotExist(err) {
			color.Red("`collection` folder is not found!")
			os.Exit(1)
		}

		moulConfig := viper.New()
		moulConfig.SetConfigName("moul")
		moulConfig.SetDefault("ga_measurement_id", "")
		moulConfig.SetDefault("favicon", "false")
		moulConfig.SetDefault("content", map[string]string{})
		moulConfig.AddConfigPath(".")
		err = moulConfig.ReadInConfig()
		if err != nil {
			fmt.Printf("Fatal error config file: %s \n", err)
		}

		slugName := slug.Make(moulConfig.GetString("profile.name"))

		coverPath := filepath.Join(dir, "photos", "cover")
		if _, err := os.Stat(coverPath); os.IsNotExist(err) {
			color.Yellow("Skipped `cover`")
		} else {
			internal.Resize(coverPath, slugName, "cover", []int{2560, 1280, 620})
		}

		avatarPath := filepath.Join(dir, "photos", "avatar")
		if _, err := os.Stat(avatarPath); os.IsNotExist(err) {
			color.Yellow("Skipped `avatar`")
		} else {
			internal.Resize(avatarPath, slugName, "avatar", []int{512, 320})
		}

		config := viper.New()
		config.AddConfigPath(".moul")
		config.SetConfigType("toml")

		coverPhotos := internal.GetPhotos(coverPath)
		config.SetConfigName("cover")
		config.ReadInConfig()

		var cname, aname string

		if len(coverPhotos) > 0 {
			cname = filepath.Base(coverPhotos[0])
		}
		cid := config.GetString(slug.Make(cname) + ".id")
		coverPathToSqip := filepath.Join(".moul", "photos", cid, "cover", "sqip",
			internal.GetFileName(cname, slugName)+".svg",
		)
		inlineCover := internal.GetEncodedSvg(coverPathToSqip)
		cover := map[string]string{
			"id":   cid,
			"name": internal.GetFileName(cname, slugName),
			"sqip": inlineCover,
		}

		avatarPhotos := internal.GetPhotos(avatarPath)
		config.SetConfigName("avatar")
		config.ReadInConfig()

		if len(avatarPhotos) > 0 {
			aname = filepath.Base(avatarPhotos[0])
		}
		aid := config.GetString(slug.Make(aname) + ".id")
		avatarPathToSqip := filepath.Join(".moul", "photos", aid, "avatar", "sqip",
			internal.GetFileName(aname, slugName)+".svg",
		)
		inlineAvatar := internal.GetEncodedSvg(avatarPathToSqip)
		avatar := map[string]string{
			"id":   aid,
			"name": internal.GetFileName(aname, slugName),
			"sqip": inlineAvatar,
		}

		t := internal.Template()
		ctx := plush.NewContext()
		ctx.Set("md", text.Markdown)
		ctx.Set("between", iterators.Between)
		ctx.Set("toString", func(i int) string {
			return strconv.Itoa(i)
		})
		ctx.Set("joinPath", func(path, i string) string {
			return filepath.Join(path, i)
		})
		ctx.Set("getPhotos", internal.GetPhotoProd)

		ctx.Set("isProd", true)
		ctx.Set("version", Version)
		ctx.Set("base", moulConfig.Get("base"))
		ctx.Set("favicon", moulConfig.Get("favicon"))
		ctx.Set("style", moulConfig.Get("style"))
		ctx.Set("profile", moulConfig.Get("profile"))
		ctx.Set("by", slugName)
		ctx.Set("cover", cover)
		ctx.Set("avatar", avatar)
		ctx.Set("content", moulConfig.Get("content"))
		ctx.Set("section", moulConfig.Get("section"))
		ctx.Set("slugName", slugName)
		ctx.Set("social", moulConfig.Get("social"))
		ctx.Set("measurementId", moulConfig.Get("ga_measurement_id"))

		ts, err := plush.Render(t, ctx)
		if err != nil {
			log.Fatal(err)
		}
		m := minify.New()
		m.AddFunc("text/css", css.Minify)
		m.AddFunc("text/html", html.Minify)
		m.AddFunc("image/svg+xml", svg.Minify)
		mts, err := m.String("text/html", ts)
		if err != nil {
			fmt.Println(err)
		}
		ioutil.WriteFile(filepath.Join(".", ".moul", "index.html"), []byte(mts), 0644)

		out := filepath.Join(".", output)
		if _, err := os.Stat(out); !os.IsNotExist(err) {
			internal.RemoveAll(out)
		}
		os.MkdirAll(out, os.ModePerm)

		os.MkdirAll(filepath.Join(out, "assets"), os.ModePerm)
		box := packr.New("assets", "./assets")
		mjs, _ := box.FindString("moul.0c839.js")
		ioutil.WriteFile(
			filepath.Join(out, "assets", "moul.0c839.js"), []byte(mjs), 0644,
		)
		mcss, _ := box.FindString("moul.0c839.css")
		ioutil.WriteFile(
			filepath.Join(out, "assets", "moul.0c839.css"), []byte(mcss), 0644,
		)
		if moulConfig.GetBool("favicon") == true {
			copy.Copy(filepath.Join(".", "favicon"), filepath.Join(out, "favicon"))
		}
		copy.Copy(filepath.Join(".", ".moul", "index.html"), filepath.Join(out, "index.html"))

		config.SetConfigName("photos")
		config.ReadInConfig()

		for _, k := range config.AllKeys() {
			for _, v := range config.GetStringSlice(k) {
				copy.Copy(v, filepath.Join(out, strings.Split(v, ".moul")[1]))
			}
		}

		fmt.Print("\n● Success! Exported photo collection in")
		color.Green(" `%s`", time.Since(start))
		s.Stop()
	},
}

Export cmd

View Source
var Update = &cobra.Command{
	Use:   "update",
	Short: "update to latest version",
	Run: func(cmd *cobra.Command, args []string) {
		v := semver.MustParse(Version)

		latest, err := selfupdate.UpdateSelf(v, "moulco/moul")
		if err != nil {
			color.Red("Binary update failed:", err)
		}

		if latest.Version.Equals(v) {
			fmt.Println("Current binary is the latest version", Version)
		} else {
			color.Green("Successfully updated to latest version")
		}
	},
}

Update cmd

View Source
var VersionCmd = &cobra.Command{
	Use:   "version",
	Short: "Version",
	Long:  "Version",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(Version)
	},
}

VersionCmd version

Functions

func Execute

func Execute()

Execute func

func ImageHandler

func ImageHandler(w http.ResponseWriter, r *http.Request)

ImageHandler func

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL