diff --git a/cmd/cmd.go b/cmd/cmd.go index 9b861846..9ea4b6d0 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -7,6 +7,7 @@ import ( _ "github.com/sikalabs/slu/cmd/file_templates" _ "github.com/sikalabs/slu/cmd/file_templates/editorconfig" _ "github.com/sikalabs/slu/cmd/file_templates/gitignore" + _ "github.com/sikalabs/slu/cmd/file_templates/go_cli_project" _ "github.com/sikalabs/slu/cmd/generate_docs" _ "github.com/sikalabs/slu/cmd/gitlab_ci" _ "github.com/sikalabs/slu/cmd/gitlab_ci/skip_stage" diff --git a/cmd/file_templates/go_cli_project/files.go b/cmd/file_templates/go_cli_project/files.go new file mode 100644 index 00000000..371a4a47 --- /dev/null +++ b/cmd/file_templates/go_cli_project/files.go @@ -0,0 +1,116 @@ +package go_cli_project + +var Files = map[string]string{ + // README.md + "README.md": `# {{.ProjectName}} +`, + ".gitignore": `# Mac +.DS_Store + +# Editor +.vscode +.idea + +# Generic +*.log +*.backup + +# Go +{{.ProjectName}} +*.exe +/dist/** +cobra-docs +`, + // .editorconfig + ".editorconfig": `root = true +[*] +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +end_of_line = lf +max_line_length = off +[*.go] +indent_style = tab +[Makefile] +indent_style = tab +`, + // go.mod + "go.mod": `module {{.Package}} + +go 1.16 + +require ( + github.com/spf13/cobra v1.2.1 +) +`, + // version/version.go + "version/version.go": `package version + +var Version string = "v0.1.0-dev" +`, + // cmd/cmd.go + "cmd/cmd.go": `package cmd + +import ( + "{{.Package}}/cmd/root" + _ "{{.Package}}/cmd/version" + "github.com/spf13/cobra" +) + +func Execute() { + cobra.CheckErr(root.Cmd.Execute()) +} +`, + // cmd/root/root.go + "cmd/root/root.go": ` +package root + +import ( + "{{.Package}}/version" + "github.com/spf13/cobra" +) + +var Cmd = &cobra.Command{ + Use: "{{.ProjectName}}", + Short: "{{.ProjectName}}, " + version.Version, +} +`, + // cmd/version/version.go + "cmd/version/version.go": `package version + +import ( + "fmt" + + "{{.Package}}/cmd/root" + "{{.Package}}/version" + "github.com/spf13/cobra" +) + +var Cmd = &cobra.Command{ + Use: "version", + Short: "Prints version", + Aliases: []string{"v"}, + Args: cobra.NoArgs, + Run: func(c *cobra.Command, args []string) { + fmt.Printf("%s\n", version.Version) + }, +} + +func init() { + root.Cmd.AddCommand(Cmd) +} +`, + // main.go + "main.go": `package main + +import ( + "{{.Package}}/cmd" +) + +func main() { + cmd.Execute() +} +`, +} diff --git a/cmd/file_templates/go_cli_project/go_cli_project.go b/cmd/file_templates/go_cli_project/go_cli_project.go new file mode 100644 index 00000000..701ab7c8 --- /dev/null +++ b/cmd/file_templates/go_cli_project/go_cli_project.go @@ -0,0 +1,76 @@ +package go_cli_project + +import ( + "html/template" + "os" + "path" + "path/filepath" + + file_templates_cmd "github.com/sikalabs/slu/cmd/file_templates" + + "github.com/spf13/cobra" +) + +var CmdFlagPathPrefix string +var CmdFlagProjectName string +var CmdFlagPackage string + +type TemplateVariables struct { + ProjectName string + Package string +} + +var Cmd = &cobra.Command{ + Use: "go-cli-project", + Short: "Create Golang CLI project", + Args: cobra.NoArgs, + Run: func(c *cobra.Command, args []string) { + for filename, content := range Files { + _ = content + fullPath := path.Join(CmdFlagPathPrefix, filename) + err := os.MkdirAll(filepath.Dir(fullPath), os.ModePerm) + if err != nil { + panic(err) + } + t, err := template.New(fullPath).Parse(content) + if err != nil { + panic(err) + } + f, err := os.Create(fullPath) + if err != nil { + panic(err) + } + defer f.Close() + t.Execute(f, TemplateVariables{ + ProjectName: CmdFlagProjectName, + Package: CmdFlagPackage, + }) + } + }, +} + +func init() { + file_templates_cmd.Cmd.AddCommand(Cmd) + Cmd.Flags().StringVar( + &CmdFlagPathPrefix, + "path", + ".", + "Path prefix", + ) + Cmd.Flags().StringVarP( + &CmdFlagProjectName, + "project-name", + "n", + "", + "Project name {{.ProjectName}}", + ) + Cmd.MarkFlagRequired("project-name") + Cmd.Flags().StringVarP( + &CmdFlagPackage, + "package", + "p", + "", + "Package {{.Package}}", + ) + Cmd.MarkFlagRequired("package") +}