template_plugin

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TemplatePlugin = plugin.Builder().
	TemplateFeature().
	WithName("default").
	WithDescription("the default template").
	WithGetTemplateData(func(ctx plugin.TemplateContext) (any, error) {

		name := path.Base(ctx.ProjectDir)

		default_module_url := path.Join(ctx.Config.ModuleUrlBase, name)

		prompt := promptui.Prompt{
			Label: "Module URL",
			Validate: func(s string) error {
				if !module_regexp.MatchString(s) {
					return errInvalidModuleURL
				}
				return nil
			},
			Default: default_module_url,
		}
		module, err := prompt.Run()
		if err != nil {
			return nil, err
		}

		cmd := exec.Command("go", "version")
		out, err := cmd.Output()
		if err != nil {
			return nil, err
		}

		matches := version_regexp.FindStringSubmatch(string(out))
		go_version := matches[version_regexp.SubexpIndex("version")]

		data := struct {
			Version, Module string
		}{
			Version: go_version,
			Module:  module,
		}
		return data, nil
	}).
	WithNewTemplate(*template.Must(template.New("main.go").
		Parse(strings.Trim(dedent.Dedent(`
		package main
	
		func main() {
			
		}
		`), "\n")))).
	WithNewTemplate(*template.Must(template.New("go.mod").
		Parse(strings.Trim(dedent.Dedent(`
		module {{.Module}}

		go {{.Version}}
		`), "\n")))).
	WithAvailabilityFilter(plugin.HasExec("go")).
	WithEntryPoint(func(ctx plugin.TemplateContext) plugin.EntryPoint {
		return plugin.EntryPoint{
			Path:  "main.go",
			Line:  4,
			IsDir: false,
		}
	}).
	AddFeature().
	TemplateFeature().
	WithName("library").
	WithDescription("the library template").
	WithGetTemplateData(func(ctx plugin.TemplateContext) (any, error) {

		name := path.Base(ctx.ProjectDir)

		default_module_url := path.Join(ctx.Config.ModuleUrlBase, name)

		prompt := promptui.Prompt{
			Label: "Module URL",
			Validate: func(s string) error {
				if !module_regexp.MatchString(s) {
					return errInvalidModuleURL
				}
				return nil
			},
			Default: default_module_url,
		}
		module, err := prompt.Run()
		if err != nil {
			return nil, err
		}

		cmd := exec.Command("go", "version")
		out, err := cmd.Output()
		if err != nil {
			return nil, err
		}

		matches := version_regexp.FindStringSubmatch(string(out))
		go_version := matches[version_regexp.SubexpIndex("version")]

		data := struct {
			Version, Module, Package string
		}{
			Version: go_version,
			Module:  module,
			Package: name,
		}
		return data, nil
	}).
	WithNewTemplate(*template.Must(template.New("lib.go").
		Parse(strings.Trim(dedent.Dedent(`
		package {{.Package}}
	
		
		`), "\n")))).
	WithNewTemplate(*template.Must(template.New("go.mod").
		Parse(strings.Trim(dedent.Dedent(`
		module {{.Module}}

		go {{.Version}}
		`), "\n")))).
	WithAvailabilityFilter(plugin.HasExec("go")).
	WithEntryPoint(func(ctx plugin.TemplateContext) plugin.EntryPoint {
		return plugin.EntryPoint{
			Path:  "lib.go",
			Line:  3,
			IsDir: false,
		}
	}).
	AddFeature().
	Build()

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL