Skip to content
Permalink
Browse files

Add support for themesDir and cache folder

  • Loading branch information...
ArmanMazdaee committed Feb 6, 2019
1 parent 2e4f840 commit b38cb81fc27a9cd2dbf74dd9bddb6b52e44bf5ae
@@ -5,29 +5,47 @@ import (
"io/ioutil"

"github.com/resumic/schema/render"
"github.com/resumic/schema/theme"
"github.com/spf13/cobra"
)

func renderRun(cmd *cobra.Command, args []string) error {
resumePath := args[0]
htmlPath := args[1]
themeName, err := cmd.Flags().GetString("theme")
cacheDir, err := cmd.Flags().GetString("cacheDir")
if err != nil {
panic(err)
}
themesName, err := cmd.Flags().GetString("theme")
if err != nil {
panic(err)
}
themesDir, err := cmd.Flags().GetString("themesDir")
if err != nil {
panic(err)
}

if themesDir == "" {
theme.UpdateCache(cacheDir)
themesDir, err = theme.GetThemesPath(themesName, cacheDir)
if err != nil {
return err
}
}

resume, err := ioutil.ReadFile(resumePath)
if err != nil {
return fmt.Errorf("Couldn't read the json resume from %s: %s", resumePath, err)
}
html, err := render.RenderHTML(resume, themeName)
html, err := render.RenderHTML(resume, themesDir)
if err != nil {
return fmt.Errorf("Couldn't render the html resume: %s", err)
}
err = ioutil.WriteFile(htmlPath, html, 0600)
if err != nil {
return fmt.Errorf("Couldn't write the html resume to %s: %s", htmlPath, err)
}
fmt.Printf("%s rendered successfully to %s using %s as theme\n", resumePath, htmlPath, themeName)
fmt.Printf("%s rendered successfully to %s using %s as theme\n", resumePath, htmlPath, themesDir)
return nil
}

@@ -40,5 +58,6 @@ var renderCmd = &cobra.Command{

func init() {
renderCmd.Flags().StringP("theme", "t", "test-theme", "Theme to use")
renderCmd.Flags().StringP("themesDir", "d", "", "Filesystem path to themes directory")
rootCmd.AddCommand(renderCmd)
}
@@ -1,6 +1,9 @@
package cmd

import (
"os"
"path"

"github.com/spf13/cobra"
)

@@ -9,6 +12,15 @@ var rootCmd = &cobra.Command{
Short: "Resumic is a standardized and generic data schema for your resume/CV",
}

func init() {
userCache, err := os.UserCacheDir()
if err != nil {
userCache = path.Join(os.TempDir(), "cache")
}
cacheDir := path.Join(userCache, "resumic")
rootCmd.PersistentFlags().String("cacheDir", cacheDir, "Filesystem path to cache directory.")
}

func Execute() {
rootCmd.Execute()
}
@@ -18,26 +18,19 @@ type frontmatter struct {
Layout string `json:"layout"`
}

func build(root string) error {
resp := commands.Execute([]string{"--quiet", "-s", root})
func build(root, themePath string) error {
resp := commands.Execute([]string{"--quiet", "-s", root, "--themesDir", themePath})
return resp.Err
}

func RenderHTML(resume []byte, theme string) ([]byte, error) {
func RenderHTML(resume []byte, themePath string) ([]byte, error) {
sitePath, err := ioutil.TempDir(os.TempDir(), "resumic")
if err != nil {
return nil, err
}
defer os.RemoveAll(sitePath)

themePath := path.Join(sitePath, "themes")
err = extractTheme(themePath, theme)
if err != nil {
return nil, err
}

config := siteConfig{}
config.Theme = theme
config.DisableKinds = []string{"taxonomy", "taxonomyTerm", "category", "sitemap", "RSS", "404", "robotsTXT", "home", "section"}

configJSON, err := json.MarshalIndent(config, "", " ")
@@ -77,7 +70,7 @@ func RenderHTML(resume []byte, theme string) ([]byte, error) {
return nil, err
}

err = build(sitePath)
err = build(sitePath, themePath)
if err != nil {
return nil, err
}

This file was deleted.

@@ -0,0 +1,33 @@
package theme

import (
"io"
"os"
"path"

"github.com/gobuffalo/packd"
packr "github.com/gobuffalo/packr/v2"
)

func getDefaultThemesPath(cachePath string) string {
return path.Join(cachePath, "default_themes")
}

func extractDefaultThemes(root string) error {
box := packr.New("themes", "../theme/defaults")
err := os.RemoveAll(root)
if err != nil {
return err
}
return box.Walk(func(name string, file packd.File) error {
p := path.Join(root, name)
os.MkdirAll(path.Dir(p), 0777)
dst, err := os.Create(p)
defer dst.Close()
if err != nil {
return err
}
_, err = io.Copy(dst, file)
return err
})
}
@@ -0,0 +1,28 @@
package theme

import (
"os"
"path"
)

type ThemeNotFoundError struct {
name string
}

func (e *ThemeNotFoundError) Error() string {
return "theme: " + e.name + " could not be found."
}

func UpdateCache(cachePath string) error {
defaultPath := getDefaultThemesPath(cachePath)
return extractDefaultThemes(defaultPath)
}

func GetThemesPath(themesName, cachePath string) (string, error) {
defaultPath := getDefaultThemesPath(cachePath)
themesPath := path.Join(defaultPath, themesName)
if _, err := os.Stat(themesPath); os.IsNotExist(err) {
return "", &ThemeNotFoundError{name: themesName}
}
return themesPath, nil
}

0 comments on commit b38cb81

Please sign in to comment.
You can’t perform that action at this time.