Initial commit
This commit is contained in:
113
skills/go-cli-builder/assets/templates/init.go.template
Normal file
113
skills/go-cli-builder/assets/templates/init.go.template
Normal file
@@ -0,0 +1,113 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"{{.ModuleName}}/internal/templates"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const defaultConfigContent = `# Configuration file for {{.ProjectName}}
|
||||
# Copy this to {{.ProjectName}}.yaml and customize as needed
|
||||
|
||||
# Database configuration
|
||||
database: "{{.ProjectName}}.db"
|
||||
|
||||
# Logging configuration
|
||||
verbose: false
|
||||
debug: false
|
||||
log_json: false
|
||||
|
||||
# Add your application-specific configuration here
|
||||
# Example:
|
||||
# myapp:
|
||||
# api_url: "https://api.example.com"
|
||||
# api_token: "your-token-here"
|
||||
# timeout: 30s
|
||||
`
|
||||
|
||||
// initCmd represents the init command
|
||||
var initCmd = &cobra.Command{
|
||||
Use: "init",
|
||||
Short: "Initialize configuration and template files",
|
||||
Long: ` + "`" + `Create default configuration file and custom template file for customization.
|
||||
|
||||
This command generates:
|
||||
- {{.ProjectName}}.yaml (configuration file)
|
||||
- {{.ProjectName}}.md (customizable template, or use --template-file to specify)
|
||||
|
||||
Use --force to overwrite existing files.
|
||||
|
||||
Example:
|
||||
{{.ProjectName}} init
|
||||
{{.ProjectName}} init --template-file my-template.md
|
||||
{{.ProjectName}} init --force` + "`" + `,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
log := GetLogger()
|
||||
force, _ := cmd.Flags().GetBool("force")
|
||||
templateFile, _ := cmd.Flags().GetString("template-file")
|
||||
|
||||
configFile := "{{.ProjectName}}.yaml"
|
||||
|
||||
// Check if config file exists
|
||||
configExists := fileExists(configFile)
|
||||
if configExists && !force {
|
||||
return fmt.Errorf("config file %s already exists (use --force to overwrite)", configFile)
|
||||
}
|
||||
|
||||
// Check if template file exists
|
||||
templateExists := fileExists(templateFile)
|
||||
if templateExists && !force {
|
||||
return fmt.Errorf("template file %s already exists (use --force to overwrite)", templateFile)
|
||||
}
|
||||
|
||||
// Create config file
|
||||
if err := os.WriteFile(configFile, []byte(defaultConfigContent), 0o644); err != nil {
|
||||
return fmt.Errorf("failed to create config file: %w", err)
|
||||
}
|
||||
|
||||
if configExists {
|
||||
log.Infof("Overwrote %s", configFile)
|
||||
} else {
|
||||
log.Infof("Created %s", configFile)
|
||||
}
|
||||
|
||||
// Get default template content
|
||||
templateContent, err := templates.GetDefaultTemplate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get default template: %w", err)
|
||||
}
|
||||
|
||||
// Create template file
|
||||
if err := os.WriteFile(templateFile, []byte(templateContent), 0o644); err != nil {
|
||||
return fmt.Errorf("failed to create template file: %w", err)
|
||||
}
|
||||
|
||||
if templateExists {
|
||||
log.Infof("Overwrote %s", templateFile)
|
||||
} else {
|
||||
log.Infof("Created %s", templateFile)
|
||||
}
|
||||
|
||||
fmt.Printf("\n✅ Initialization complete!\n\n")
|
||||
fmt.Printf("Next steps:\n")
|
||||
fmt.Printf(" 1. Edit %s and add your configuration\n", configFile)
|
||||
fmt.Printf(" 2. (Optional) Customize %s for your preferred output format\n", templateFile)
|
||||
fmt.Printf(" 3. Run: {{.ProjectName}} <command> --help for usage information\n\n")
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(initCmd)
|
||||
initCmd.Flags().Bool("force", false, "Overwrite existing files")
|
||||
initCmd.Flags().String("template-file", "{{.ProjectName}}.md", "Name of custom template file to create")
|
||||
}
|
||||
|
||||
// fileExists checks if a file exists
|
||||
func fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
Reference in New Issue
Block a user