scaffold/internal/command/from_project.go

131 lines
2.8 KiB
Go
Raw Normal View History

2020-02-20 08:31:22 +01:00
package command
import (
"fmt"
2020-02-20 08:31:22 +01:00
"net/url"
"os"
"forge.cadoles.com/wpetit/scaffold/internal/template"
2020-02-20 08:31:22 +01:00
"forge.cadoles.com/wpetit/scaffold/internal/project"
2020-02-20 08:31:22 +01:00
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
2020-04-07 08:44:21 +02:00
func fromCommand() *cli.Command {
2020-02-20 08:31:22 +01:00
return &cli.Command{
2020-04-07 08:44:21 +02:00
Name: "from",
2020-02-20 08:31:22 +01:00
Aliases: []string{"n"},
Usage: "generate a new project from a given template url",
ArgsUsage: "<URL>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "directory",
Aliases: []string{"d"},
Usage: "Set destination to `DIR`",
Value: "./",
},
&cli.StringFlag{
Name: "manifest",
Aliases: []string{"m"},
Usage: "The scaffold manifest `FILE`",
Value: "scaffold.yml",
},
&cli.StringSliceFlag{
Name: "ignore",
Aliases: []string{"i"},
Usage: "Ignore files matching pattern",
Value: cli.NewStringSlice(".git", ".git/**", "./.git/**/*"),
},
2020-02-20 08:31:22 +01:00
},
2020-04-07 08:44:21 +02:00
Action: fromAction,
2020-02-20 08:31:22 +01:00
}
}
2020-04-07 08:44:21 +02:00
func fromAction(c *cli.Context) error {
2020-02-20 08:31:22 +01:00
rawURL := c.Args().First()
projectURL, err := url.Parse(rawURL)
if err != nil {
return errors.Wrap(err, "could not parse url")
}
availableFetchers := []project.Fetcher{
project.NewGitFetcher(),
project.NewLocalFetcher(),
}
var fetcher project.Fetcher
for _, f := range availableFetchers {
if f.Match(projectURL) {
fetcher = f
break
}
}
2020-04-07 08:44:21 +02:00
if fetcher == nil {
return errors.Errorf("could not find any fetcher matching URL '%v'", projectURL)
}
2020-02-20 08:31:22 +01:00
vfs, err := fetcher.Fetch(projectURL)
if err != nil {
return errors.Wrap(err, "could not fetch project")
}
templateData := template.Data{}
2020-02-20 08:31:22 +01:00
manifestPath := c.String("manifest")
2020-02-20 08:31:22 +01:00
manifestFile, err := vfs.Open(manifestPath)
if err != nil {
if !os.IsNotExist(err) {
return errors.Wrapf(err, "could not read manifest '%s'", manifestPath)
2020-02-20 08:31:22 +01:00
}
fmt.Println("Could not find scaffold manifest.")
2020-02-20 08:31:22 +01:00
}
if manifestFile != nil {
defer manifestFile.Close()
2020-02-20 08:31:22 +01:00
fmt.Println("Loading scaffold manifest.")
2020-02-20 08:31:22 +01:00
manifest, err := template.Load(manifestFile)
if err != nil {
return errors.Wrapf(err, "could not load manifest '%s'", manifestPath)
2020-02-20 08:31:22 +01:00
}
if err := manifest.Validate(); err != nil {
return errors.Wrapf(err, "could not validate manifest '%s'", manifestPath)
2020-02-20 08:31:22 +01:00
}
fmt.Println("Prompting for templates variables.")
if err := template.Prompt(manifest, templateData); err != nil {
return errors.Wrap(err, "could not prompt for template data")
}
2020-02-20 08:31:22 +01:00
}
directory := c.String("directory")
2020-02-20 08:31:22 +01:00
ignore := c.StringSlice("ignore")
ignore = append(ignore, manifestPath)
fmt.Println("Generating project tree.")
err = template.CopyDir(vfs, ".", directory, &template.Option{
TemplateData: templateData,
TemplateExt: ".gotpl",
IgnorePatterns: ignore,
PreferredStrategy: template.Undefined,
})
if err != nil {
return err
}
fmt.Println("Done.")
return nil
2020-02-20 08:31:22 +01:00
}