Skip to content

Commit

Permalink
Use go:embed for defaults (#137).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Mar 1, 2021
1 parent e8cacfc commit 1a20ba0
Show file tree
Hide file tree
Showing 272 changed files with 49 additions and 11 deletions.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
60 changes: 49 additions & 11 deletions cmd/site.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"embed"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
Expand All @@ -16,6 +18,15 @@ import (
"github.com/spf13/cobra"
)

//go:embed defaults/*
var defaultsFS embed.FS

//go:embed defaults_bare/*
var defaultsBareFS embed.FS

//go:embed defaults_node_modules/*
var defaultsNodeModulesFS embed.FS

// siteCmd represents the site command
var siteCmd = &cobra.Command{
Use: "site [name]",
Expand Down Expand Up @@ -80,26 +91,53 @@ var siteCmd = &cobra.Command{
}

// set to Defaults and overwrite if bareFlag is set
scaffolding := generated.Defaults
scaffolding, err := fs.Sub(defaultsFS, "defaults")
if err != nil {
common.CheckErr(fmt.Errorf("Unable to get defaults: %w", err))
}
// Choose which scaffolding to use for new site.
if bareFlag {
scaffolding = generated.Defaults_bare
scaffolding, err = fs.Sub(defaultsBareFS, "defaults_bare")
if err != nil {
common.CheckErr(fmt.Errorf("Unable to get bare defaults: %w", err))
}
}

// Loop through generated file defaults to create site scaffolding
for file, content := range scaffolding {
// Create the directories needed for the current file
pth := fmt.Sprintf("%s/%s", newpath, strings.Trim(filepath.Dir(file), "/"))
if err := os.MkdirAll(pth, os.ModePerm); err != nil {
common.CheckErr(fmt.Errorf("Unable to create path(s) %s: %v", pth, err))
fs.WalkDir(scaffolding, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
// Create the directories needed for the current file
if err := os.MkdirAll(newpath+"/"+path, os.ModePerm); err != nil {
common.CheckErr(fmt.Errorf("Unable to create path(s) %s: %v", path, err))
}
return nil
}
content, _ := scaffolding.Open(path)
contentBytes, err := ioutil.ReadAll(content)
// Create the current default file
if err := ioutil.WriteFile(fmt.Sprintf("%s/%s", newpath, file), content, 0755); err != nil {

if err := ioutil.WriteFile(newpath+"/"+path, contentBytes, 0755); err != nil {
common.CheckErr(fmt.Errorf("Unable to write file: %w", err))
}
}
return nil
})
/*
for file, content := range scaffolding {
// Create the directories needed for the current file
pth := fmt.Sprintf("%s/%s", newpath, strings.Trim(filepath.Dir(file), "/"))
if err := os.MkdirAll(pth, os.ModePerm); err != nil {
common.CheckErr(fmt.Errorf("Unable to create path(s) %s: %v", pth, err))
}
// Create the current default file
if err := ioutil.WriteFile(fmt.Sprintf("%s/%s", newpath, file), content, 0755); err != nil {
common.CheckErr(fmt.Errorf("Unable to write file: %w", err))
}
}
*/

// Loop through generated node_modules npm pacakges to include in scaffolding
for file, content := range generated.Defaults_node_modules {
Expand Down

0 comments on commit 1a20ba0

Please sign in to comment.