Skip to content

Commit

Permalink
feat(cmd/file_templates/go_cli_project): Add command for quick bootst…
Browse files Browse the repository at this point in the history
…rap of Golang CLI project
  • Loading branch information
ondrejsika committed Aug 21, 2021
1 parent 4f5a835 commit 6a97d5b
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
116 changes: 116 additions & 0 deletions cmd/file_templates/go_cli_project/files.go
Original file line number Diff line number Diff line change
@@ -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()
}
`,
}
76 changes: 76 additions & 0 deletions cmd/file_templates/go_cli_project/go_cli_project.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 6a97d5b

Please sign in to comment.