-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/file_templates/go_cli_project): Add command for quick bootst…
…rap of Golang CLI project
- Loading branch information
1 parent
4f5a835
commit 6a97d5b
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
`, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |