Skip to content
Snippets Groups Projects
Commit febf83b3 authored by Tamer Tas's avatar Tamer Tas
Browse files

Implement templates

parent 63c46d32
No related branches found
No related tags found
No related merge requests found
main.go 0 → 100644
package main
import "github.com/spf13/cobra"
var buildCmd = &cobra.Command{
Use: "build",
Short: "Creates boiler-plate code for a project",
Run: func(cmd *cobra.Command, args []string) {
tmpl, err := template.Get(args[0])
if err != nil {
panic(err)
}
/*
*err := tmpl.Persist()
*if err != nil {
* panic(err)
*}
*/
},
}
func main() {
mainCmd := &cobra.Command{
Use: "main",
}
mainCmd.AddCommand(buildCmd)
mainCmd.Execute()
}
package template
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"text/template"
)
// ?
type Metadata struct {
Name string
Author string
Email string
Date string
Version string
}
type Interface interface {
Execute(string, Metadata) error
}
func Get(path string) (Interface, error) {
return &dirTemplate{Path: path}, nil
}
type dirTemplate struct {
Path string
}
// Execute fills the template with the given metadata.
func (d *dirTemplate) Execute(dirPrefix string, md Metadata) error {
fill := func(filepath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
oldName := info.Name()
// BUG:
buf := bytes.NewBuffer([]byte{})
tmpl := template.Must(template.New("x").Parse(oldName))
if err := tmpl.Execute(buf, md); err != nil {
return err
}
newName := buf.String()
fmt.Print(oldName, "\n")
fmt.Print(newName, "\n")
if oldName != newName {
if err := os.Rename(filepath, newName); err != nil {
return err
}
}
if !info.IsDir() && false {
f, err := os.Open(filepath)
if err != nil {
return err
} else {
defer f.Close()
}
}
return nil
}
// TODO: {{Project.Name}} vs app/etc.
if _, err := exec.Command("/bin/cp", "-r", d.Path, filepath.Join(dirPrefix, "test")).Output(); err != nil {
return err
}
return filepath.Walk(filepath.Join(dirPrefix, "test"), fill)
}
package template_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/tmrts/cookie/pkg/template"
)
func TestRetrievesProjectTemplate(t *testing.T) {
tmpl, err := template.Get("test-data/test-template")
if err != nil {
t.Fatalf("template.Get(%q) got error -> %v", "test-template", err)
}
tmpDirPath, err := ioutil.TempDir("", "template-test")
if err != nil {
t.Fatalf("ioutil.TempDir() got error -> %v", err)
} else {
defer os.RemoveAll(tmpDirPath)
}
metadata := template.Metadata{
Name: "test-project-1",
Author: "test-author",
Email: "test@mail.com",
Date: time.Now().Format("Mon Jan 2 15:04:05 -0700 2006"),
Version: "0.0.1",
}
err = tmpl.Execute(tmpDirPath, metadata)
if err != nil {
t.Fatalf("template.Execute(metadata) got error -> %v", err)
}
_, err = os.Open(filepath.Join(tmpDirPath, metadata.Name))
if err != nil {
t.Errorf("template.Execute(metadata) directory %q should exist", metadata.Name)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment