From 683a3d7957fc080b7b231a87faf56cb84a3cae4b Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 25 Jul 2017 16:22:07 +0100 Subject: [PATCH] Build assets Former-commit-id: e15d54284d43d3fbc9e1914e37b45014f77137c5 [formerly e02e31cba63e08b7a9f9d5c2810ad3e8396ce2a2] [formerly bc1bfc4e0fac48e1798a70a460500eddd7ca90cf [formerly eb87aa6e9078d6dfece52a3eaf7b57c1a00b62e2]] Former-commit-id: e80b2367d4f391cf1663f16a2efe17c7fdb9c788 [formerly be6678b6662727d743c118d46f0f1fe55024032a] Former-commit-id: 7542bcc728b732a2bb6fcbbe800810972138aeb1 --- caddy/hugo/rice-box.go | 14 +++++++------- rice-box.go.REMOVED.git-id | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/caddy/hugo/rice-box.go b/caddy/hugo/rice-box.go index 1a421cca..63c33dce 100644 --- a/caddy/hugo/rice-box.go +++ b/caddy/hugo/rice-box.go @@ -15,7 +15,7 @@ func init() { } file3 := &embedded.EmbeddedFile{ Filename: `hugo.go`, - FileModTime: time.Unix(1500535931, 0), + FileModTime: time.Unix(1500977545, 0), Content: string("package hugo\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"strings\"\n\t\"time\"\n\n\trice \"github.com/GeertJohan/go.rice\"\n\t\"github.com/hacdias/filemanager\"\n\t\"github.com/hacdias/filemanager/variables\"\n\t\"github.com/robfig/cron\"\n)\n\ntype hugo struct {\n\t// Website root\n\tRoot string `description:\"The relative or absolute path to the place where your website is located.\"`\n\t// Public folder\n\tPublic string `description:\"The relative or absolute path to the public folder.\"`\n\t// Hugo executable path\n\tExe string `description:\"The absolute path to the Hugo executable or the command to execute.\"`\n\t// Hugo arguments\n\tArgs []string `description:\"The arguments to run when running Hugo\"`\n\t// Indicates if we should clean public before a new publish.\n\tCleanPublic bool `description:\"Indicates if the public folder should be cleaned before publishing the website.\"`\n\n\t// TODO: admin interface to cgange options\n}\n\nfunc (h hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {\n\t// If we are using the 'magic url' for the settings, we should redirect the\n\t// request for the acutual path.\n\tif r.URL.Path == \"/settings/\" || r.URL.Path == \"/settings\" {\n\t\tvar frontmatter string\n\t\tvar err error\n\n\t\tif _, err = os.Stat(filepath.Join(h.Root, \"config.yaml\")); err == nil {\n\t\t\tfrontmatter = \"yaml\"\n\t\t}\n\n\t\tif _, err = os.Stat(filepath.Join(h.Root, \"config.json\")); err == nil {\n\t\t\tfrontmatter = \"json\"\n\t\t}\n\n\t\tif _, err = os.Stat(filepath.Join(h.Root, \"config.toml\")); err == nil {\n\t\t\tfrontmatter = \"toml\"\n\t\t}\n\n\t\tr.URL.Path = \"/config.\" + frontmatter\n\t\treturn 0, nil\n\t}\n\n\t// From here on, we only care about 'hugo' router so we can bypass\n\t// the others.\n\tif c.Router != \"hugo\" {\n\t\treturn 0, nil\n\t}\n\n\t// If we are not using HTTP Post, we shall return Method Not Allowed\n\t// since we are only working with this method.\n\tif r.Method != http.MethodPost {\n\t\treturn http.StatusMethodNotAllowed, nil\n\t}\n\n\t// If we are creating a file built from an archetype.\n\tif r.Header.Get(\"Archetype\") != \"\" {\n\t\tif !c.User.AllowNew {\n\t\t\treturn http.StatusForbidden, nil\n\t\t}\n\n\t\tfilename := filepath.Join(string(c.User.FileSystem), r.URL.Path)\n\t\tfilename = strings.TrimPrefix(filename, \"/\")\n\t\tarchetype := r.Header.Get(\"archetype\")\n\n\t\text := filepath.Ext(filename)\n\n\t\t// If the request isn't for a markdown file, we can't\n\t\t// handle it.\n\t\tif ext != \".markdown\" && ext != \".md\" {\n\t\t\treturn http.StatusBadRequest, errUnsupportedFileType\n\t\t}\n\n\t\t// Tries to create a new file based on this archetype.\n\t\targs := []string{\"new\", filename, \"--kind\", archetype}\n\t\tif err := Run(h.Exe, args, h.Root); err != nil {\n\t\t\treturn http.StatusInternalServerError, err\n\t\t}\n\n\t\t// Writes the location of the new file to the Header.\n\t\tw.Header().Set(\"Location\", \"/files/content/\"+filename)\n\t\treturn http.StatusCreated, nil\n\t}\n\n\t// If we are trying to regenerate the website.\n\tif r.Header.Get(\"Regenerate\") == \"true\" {\n\t\tif !c.User.Permissions[\"allowPublish\"] {\n\t\t\treturn http.StatusForbidden, nil\n\t\t}\n\n\t\tfilename := filepath.Join(string(c.User.FileSystem), r.URL.Path)\n\t\tfilename = strings.TrimPrefix(filename, \"/\")\n\n\t\t// Before save command handler.\n\t\tif err := c.FM.Runner(\"before_publish\", filename); err != nil {\n\t\t\treturn http.StatusInternalServerError, err\n\t\t}\n\n\t\t// We only run undraft command if it is a file.\n\t\tif !strings.HasSuffix(filename, \"/\") {\n\t\t\targs := []string{\"undraft\", filename}\n\t\t\tif err := Run(h.Exe, args, h.Root); err != nil {\n\t\t\t\treturn http.StatusInternalServerError, err\n\t\t\t}\n\t\t}\n\n\t\t// Regenerates the file\n\t\th.run(false)\n\n\t\t// Executed the before publish command.\n\t\tif err := c.FM.Runner(\"before_publish\", filename); err != nil {\n\t\t\treturn http.StatusInternalServerError, err\n\t\t}\n\n\t\treturn http.StatusOK, nil\n\t}\n\n\tif r.Header.Get(\"Schedule\") != \"\" {\n\t\tif !c.User.Permissions[\"allowPublish\"] {\n\t\t\treturn http.StatusForbidden, nil\n\t\t}\n\n\t\treturn h.schedule(c, w, r)\n\t}\n\n\treturn http.StatusNotFound, nil\n}\n\nfunc (h hugo) AfterAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {\n\treturn 0, nil\n}\n\nfunc (h hugo) JavaScript() string {\n\treturn rice.MustFindBox(\"./\").MustString(\"hugo.js\")\n}\n\n// run runs Hugo with the define arguments.\nfunc (h hugo) run(force bool) {\n\t// If the CleanPublic option is enabled, clean it.\n\tif h.CleanPublic {\n\t\tos.RemoveAll(h.Public)\n\t}\n\n\t// Prevent running if watching is enabled\n\tif b, pos := variables.StringInSlice(\"--watch\", h.Args); b && !force {\n\t\tif len(h.Args) > pos && h.Args[pos+1] != \"false\" {\n\t\t\treturn\n\t\t}\n\n\t\tif len(h.Args) == pos+1 {\n\t\t\treturn\n\t\t}\n\t}\n\n\tif err := Run(h.Exe, h.Args, h.Root); err != nil {\n\t\tlog.Println(err)\n\t}\n}\n\n// schedule schedules a post to be published later.\nfunc (h hugo) schedule(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {\n\tt, err := time.Parse(\"2006-01-02T15:04\", r.Header.Get(\"Schedule\"))\n\tpath := filepath.Join(string(c.User.FileSystem), r.URL.Path)\n\tpath = filepath.Clean(path)\n\n\tif err != nil {\n\t\treturn http.StatusInternalServerError, err\n\t}\n\n\tscheduler := cron.New()\n\tscheduler.AddFunc(t.Format(\"05 04 15 02 01 *\"), func() {\n\t\targs := []string{\"undraft\", path}\n\t\tif err := Run(h.Exe, args, h.Root); err != nil {\n\t\t\tlog.Printf(err.Error())\n\t\t\treturn\n\t\t}\n\n\t\th.run(false)\n\t})\n\n\tscheduler.Start()\n\treturn http.StatusOK, nil\n}\n"), } file4 := &embedded.EmbeddedFile{ @@ -25,19 +25,19 @@ func init() { } file5 := &embedded.EmbeddedFile{ Filename: `setup.go`, - FileModTime: time.Unix(1500540481, 0), - Content: string("package hugo\n\nimport (\n\t\"crypto/md5\"\n\t\"encoding/hex\"\n\t\"errors\"\n\t\"fmt\"\n\t\"net/http\"\n\t\"os\"\n\t\"os/exec\"\n\t\"path/filepath\"\n\t\"strings\"\n\n\t\"github.com/hacdias/filemanager\"\n\t\"github.com/mholt/caddy\"\n\t\"github.com/mholt/caddy/caddyhttp/httpserver\"\n\t\"golang.org/x/net/webdav\"\n)\n\nvar (\n\terrHugoNotFound = errors.New(\"It seems that tou don't have 'hugo' on your PATH\")\n\terrUnsupportedFileType = errors.New(\"The type of the provided file isn't supported for this action\")\n)\n\n// setup configures a new FileManager middleware instance.\nfunc setup(c *caddy.Controller) error {\n\tconfigs, err := parse(c)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\thttpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {\n\t\treturn plugin{Configs: configs, Next: next}\n\t})\n\n\treturn nil\n}\n\nfunc parse(c *caddy.Controller) ([]*filemanager.FileManager, error) {\n\tvar (\n\t\tconfigs []*filemanager.FileManager\n\t)\n\n\tfor c.Next() {\n\t\t// hugo [directory] [admin] {\n\t\t// \t\tdatabase path\n\t\t// }\n\t\tdirectory := \".\"\n\t\tadmin := \"/admin\"\n\t\tdatabase := \"\"\n\n\t\t// Get the baseURL and baseScope\n\t\targs := c.RemainingArgs()\n\n\t\tif len(args) == 1 {\n\t\t\tdirectory = args[0]\n\t\t}\n\n\t\tif len(args) > 1 {\n\t\t\tadmin = args[1]\n\t\t}\n\n\t\tfor c.NextBlock() {\n\t\t\tswitch c.Val() {\n\t\t\tcase \"database\":\n\t\t\t\tif !c.NextArg() {\n\t\t\t\t\treturn nil, c.ArgErr()\n\t\t\t\t}\n\n\t\t\t\tdatabase = c.Val()\n\t\t\t}\n\t\t}\n\n\t\tcaddyConf := httpserver.GetConfig(c)\n\n\t\tpath := filepath.Join(caddy.AssetsPath(), \"hugo\")\n\t\terr := os.MkdirAll(path, 0700)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\t// if there is a database path and it is not absolute,\n\t\t// it will be relative to \".caddy\" folder.\n\t\tif !filepath.IsAbs(database) && database != \"\" {\n\t\t\tdatabase = filepath.Join(path, database)\n\t\t}\n\n\t\t// If there is no database path on the settings,\n\t\t// store one in .caddy/hugo/{name}.db.\n\t\tif database == \"\" {\n\t\t\t// The name of the database is the hashed value of a string composed\n\t\t\t// by the host, address path and the baseurl of this File Manager\n\t\t\t// instance.\n\t\t\thasher := md5.New()\n\t\t\thasher.Write([]byte(caddyConf.Addr.Host + caddyConf.Addr.Path + admin))\n\t\t\tsha := hex.EncodeToString(hasher.Sum(nil))\n\t\t\tdatabase = filepath.Join(path, sha+\".db\")\n\n\t\t\tfmt.Println(\"[WARNING] A database is going to be created for your Hugo instace at \" + database +\n\t\t\t\t\". It is highly recommended that you set the 'database' option to '\" + sha + \".db'\\n\")\n\t\t}\n\n\t\tm, err := filemanager.New(database, filemanager.User{\n\t\t\tAllowCommands: true,\n\t\t\tAllowEdit: true,\n\t\t\tAllowNew: true,\n\t\t\tPermissions: map[string]bool{},\n\t\t\tCommands: []string{\"git\", \"svn\", \"hg\"},\n\t\t\tRules: []*filemanager.Rule{{\n\t\t\t\tRegex: true,\n\t\t\t\tAllow: false,\n\t\t\t\tRegexp: &filemanager.Regexp{Raw: \"\\\\/\\\\..+\"},\n\t\t\t}},\n\t\t\tCSS: \"\",\n\t\t\tFileSystem: webdav.Dir(directory),\n\t\t})\n\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\t// Initialize the default settings for Hugo.\n\t\thugo := &hugo{\n\t\t\tRoot: directory,\n\t\t\tPublic: filepath.Join(directory, \"public\"),\n\t\t\tArgs: []string{},\n\t\t\tCleanPublic: true,\n\t\t}\n\n\t\t// Try to find the Hugo executable path.\n\t\tif hugo.Exe, err = exec.LookPath(\"hugo\"); err != nil {\n\t\t\treturn nil, errHugoNotFound\n\t\t}\n\n\t\terr = m.RegisterPlugin(\"hugo\", hugo)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\terr = m.RegisterEventType(\"before_publish\")\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\terr = m.RegisterEventType(\"after_publish\")\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\terr = m.RegisterPermission(\"allowPublish\", true)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\tm.SetBaseURL(admin)\n\t\tm.SetPrefixURL(strings.TrimSuffix(caddyConf.Addr.Path, \"/\"))\n\t\tconfigs = append(configs, m)\n\t}\n\n\treturn configs, nil\n}\n\n// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.\nfunc (p plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {\n\tfor i := range p.Configs {\n\t\t// Checks if this Path should be handled by File Manager.\n\t\tif !httpserver.Path(r.URL.Path).Matches(p.Configs[i].BaseURL) {\n\t\t\tcontinue\n\t\t}\n\n\t\treturn p.Configs[i].ServeWithErrorHTTP(w, r)\n\t}\n\n\treturn p.Next.ServeHTTP(w, r)\n}\n\nfunc init() {\n\tcaddy.RegisterPlugin(\"hugo\", caddy.Plugin{\n\t\tServerType: \"http\",\n\t\tAction: setup,\n\t})\n}\n\ntype plugin struct {\n\tNext httpserver.Handler\n\tConfigs []*filemanager.FileManager\n}\n"), + FileModTime: time.Unix(1500977558, 0), + Content: string("package hugo\n\nimport (\n\t\"crypto/md5\"\n\t\"encoding/hex\"\n\t\"errors\"\n\t\"fmt\"\n\t\"net/http\"\n\t\"os\"\n\t\"os/exec\"\n\t\"path/filepath\"\n\t\"strings\"\n\n\t\"github.com/hacdias/filemanager\"\n\t\"github.com/mholt/caddy\"\n\t\"github.com/mholt/caddy/caddyhttp/httpserver\"\n\t\"golang.org/x/net/webdav\"\n)\n\nvar (\n\terrHugoNotFound = errors.New(\"It seems that tou don't have 'hugo' on your PATH\")\n\terrUnsupportedFileType = errors.New(\"The type of the provided file isn't supported for this action\")\n)\n\n// setup configures a new FileManager middleware instance.\nfunc setup(c *caddy.Controller) error {\n\tconfigs, err := parse(c)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\thttpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {\n\t\treturn plugin{Configs: configs, Next: next}\n\t})\n\n\treturn nil\n}\n\nfunc parse(c *caddy.Controller) ([]*filemanager.FileManager, error) {\n\tvar (\n\t\tconfigs []*filemanager.FileManager\n\t)\n\n\tfor c.Next() {\n\t\t// hugo [directory] [admin] {\n\t\t// \t\tdatabase path\n\t\t// }\n\t\tdirectory := \".\"\n\t\tadmin := \"/admin\"\n\t\tdatabase := \"\"\n\n\t\t// Get the baseURL and baseScope\n\t\targs := c.RemainingArgs()\n\n\t\tif len(args) == 1 {\n\t\t\tdirectory = args[0]\n\t\t}\n\n\t\tif len(args) > 1 {\n\t\t\tadmin = args[1]\n\t\t}\n\n\t\tfor c.NextBlock() {\n\t\t\tswitch c.Val() {\n\t\t\tcase \"database\":\n\t\t\t\tif !c.NextArg() {\n\t\t\t\t\treturn nil, c.ArgErr()\n\t\t\t\t}\n\n\t\t\t\tdatabase = c.Val()\n\t\t\t}\n\t\t}\n\n\t\tcaddyConf := httpserver.GetConfig(c)\n\n\t\tpath := filepath.Join(caddy.AssetsPath(), \"hugo\")\n\t\terr := os.MkdirAll(path, 0700)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\t// if there is a database path and it is not absolute,\n\t\t// it will be relative to \".caddy\" folder.\n\t\tif !filepath.IsAbs(database) && database != \"\" {\n\t\t\tdatabase = filepath.Join(path, database)\n\t\t}\n\n\t\t// If there is no database path on the settings,\n\t\t// store one in .caddy/hugo/{name}.db.\n\t\tif database == \"\" {\n\t\t\t// The name of the database is the hashed value of a string composed\n\t\t\t// by the host, address path and the baseurl of this File Manager\n\t\t\t// instance.\n\t\t\thasher := md5.New()\n\t\t\thasher.Write([]byte(caddyConf.Addr.Host + caddyConf.Addr.Path + admin))\n\t\t\tsha := hex.EncodeToString(hasher.Sum(nil))\n\t\t\tdatabase = filepath.Join(path, sha+\".db\")\n\n\t\t\tfmt.Println(\"[WARNING] A database is going to be created for your Hugo instace at \" + database +\n\t\t\t\t\". It is highly recommended that you set the 'database' option to '\" + sha + \".db'\\n\")\n\t\t}\n\n\t\tm, err := filemanager.New(database, filemanager.User{\n\t\t\tAllowCommands: true,\n\t\t\tAllowEdit: true,\n\t\t\tAllowNew: true,\n\t\t\tPermissions: map[string]bool{},\n\t\t\tCommands: []string{\"git\", \"svn\", \"hg\"},\n\t\t\tRules: []*filemanager.Rule{{\n\t\t\t\tRegex: true,\n\t\t\t\tAllow: false,\n\t\t\t\tRegexp: &filemanager.Regexp{Raw: \"\\\\/\\\\..+\"},\n\t\t\t}},\n\t\t\tCSS: \"\",\n\t\t\tFileSystem: webdav.Dir(directory),\n\t\t})\n\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\t// Initialize the default settings for Hugo.\n\t\thugo := &hugo{\n\t\t\tRoot: directory,\n\t\t\tPublic: filepath.Join(directory, \"public\"),\n\t\t\tArgs: []string{},\n\t\t\tCleanPublic: true,\n\t\t}\n\n\t\t// Try to find the Hugo executable path.\n\t\tif hugo.Exe, err = exec.LookPath(\"hugo\"); err != nil {\n\t\t\treturn nil, errHugoNotFound\n\t\t}\n\n\t\terr = m.RegisterPlugin(\"hugo\", hugo)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\terr = m.RegisterEventType(\"before_publish\")\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\terr = m.RegisterEventType(\"after_publish\")\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\terr = m.RegisterPermission(\"allowPublish\", true)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\tm.SetBaseURL(admin)\n\t\tm.SetPrefixURL(strings.TrimSuffix(caddyConf.Addr.Path, \"/\"))\n\t\tconfigs = append(configs, m)\n\t}\n\n\treturn configs, nil\n}\n\n// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.\nfunc (p plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {\n\tfor i := range p.Configs {\n\t\t// Checks if this Path should be handled by File Manager.\n\t\tif !httpserver.Path(r.URL.Path).Matches(p.Configs[i].BaseURL) {\n\t\t\tcontinue\n\t\t}\n\n\t\tp.Configs[i].ServeHTTP(w, r)\n\t\treturn 0, nil\n\t}\n\n\treturn p.Next.ServeHTTP(w, r)\n}\n\nfunc init() {\n\tcaddy.RegisterPlugin(\"hugo\", caddy.Plugin{\n\t\tServerType: \"http\",\n\t\tAction: setup,\n\t})\n}\n\ntype plugin struct {\n\tNext httpserver.Handler\n\tConfigs []*filemanager.FileManager\n}\n"), } file6 := &embedded.EmbeddedFile{ Filename: `utils.go`, - FileModTime: time.Unix(1500387945, 0), - Content: string("package hugo\r\n\r\nimport (\r\n\t\"errors\"\r\n\t\"os/exec\"\r\n)\r\n\r\n// Run executes an external command\r\nfunc Run(command string, args []string, path string) error {\r\n\tcmd := exec.Command(command, args...)\r\n\tcmd.Dir = path\r\n\tout, err := cmd.CombinedOutput()\r\n\r\n\tif err != nil {\r\n\t\treturn errors.New(string(out))\r\n\t}\r\n\r\n\treturn nil\r\n}\r\n"), + FileModTime: time.Unix(1500981861, 0), + Content: string("package hugo\n\nimport (\n\t\"errors\"\n\t\"os/exec\"\n)\n\n// Run executes an external command\nfunc Run(command string, args []string, path string) error {\n\tcmd := exec.Command(command, args...)\n\tcmd.Dir = path\n\tout, err := cmd.CombinedOutput()\n\n\tif err != nil {\n\t\treturn errors.New(string(out))\n\t}\n\n\treturn nil\n}\n"), } // define dirs dir1 := &embedded.EmbeddedDir{ Filename: ``, - DirModTime: time.Unix(1500966835, 0), + DirModTime: time.Unix(1500991408, 0), ChildFiles: []*embedded.EmbeddedFile{ file2, // README.md file3, // hugo.go @@ -54,7 +54,7 @@ func init() { // register embeddedBox embedded.RegisterEmbeddedBox(`./`, &embedded.EmbeddedBox{ Name: `./`, - Time: time.Unix(1500966835, 0), + Time: time.Unix(1500991408, 0), Dirs: map[string]*embedded.EmbeddedDir{ "": dir1, }, diff --git a/rice-box.go.REMOVED.git-id b/rice-box.go.REMOVED.git-id index b270cd52..3e53058d 100644 --- a/rice-box.go.REMOVED.git-id +++ b/rice-box.go.REMOVED.git-id @@ -1 +1 @@ -2d1a3b37e73fec5e25740554af91b63050cac682 \ No newline at end of file +a633747158f5a18549c300695b06cadbd55ddfb5 \ No newline at end of file