### Cobra Generator Go File Header Source: https://github.com/spf13/cobra-cli/blob/main/README.md Provides an example of a Go file header generated from the custom license configuration. ```go /* Copyright © 2020 Steve Francia This file is part of CLI application foo. */ ``` -------------------------------- ### Initialize New Cobra Application (Bash) Source: https://context7.com/spf13/cobra-cli/llms.txt This command scaffolds a new Cobra CLI application structure, generating files like main.go, cmd/root.go, and LICENSE. It requires a pre-initialized Go module and supports flags for author, license (e.g., MIT, Apache), Viper integration, and subdirectory placement. Outputs include boilerplate code ready for building and running; limitations include manual Go module setup and potential overrides via config files. ```bash # First, create a new Go module mkdir myapp cd myapp go mod init github.com/username/myapp # Initialize a basic Cobra application cobra-cli init # Initialize with author information cobra-cli init --author "John Doe john@example.com" # Initialize with a specific license cobra-cli init --license apache # Initialize with Viper support for configuration management cobra-cli init --viper --author "Jane Smith jane@example.com" --license MIT # Initialize in a specific subdirectory cobra-cli init mysubdir # Run the generated application go run main.go # Output: Brief help text and available commands # Generated file structure: # myapp/ # ├── main.go # ├── cmd/ # │ └── root.go # └── LICENSE ``` -------------------------------- ### Cobra Generator Custom License Configuration Source: https://github.com/spf13/cobra-cli/blob/main/README.md Shows an example of a custom license configuration in YAML. It includes a header and text body using interpolation for dynamic content generation. ```yaml author: Steve Francia year: 2020 license: header: This file is part of CLI application foo. text: | {{ .copyright }} This is my license. There are many like it, but this one is mine. My license is my best friend. It is my life. I must master it as I must master my life. ``` -------------------------------- ### Create Command Files Using cobra-cli Source: https://context7.com/spf13/cobra-cli/llms.txt Demonstrates how to programmatically create new command files in the cmd directory using cobra-cli's Command.Create() method. Shows configuration of project metadata including absolute path, copyright, and Apache-2.0 license. Includes error handling and output formatting for the generated file location. ```go package main import ( "fmt" "github.com/spf13/cobra-cli/cmd" ) func main() { // Example: Programmatically add a command command := &cmd.Command{ CmdName: "server", CmdParent: "rootCmd", Project: &cmd.Project{ AbsolutePath: "/path/to/myapp", Copyright: "Copyright © 2024 Developer", Legal: cmd.License{ Name: "Apache-2.0", Header: "Licensed under Apache License 2.0", }, }, } if err := command.Create(); err != nil { fmt.Printf("Error creating command: %v\n", err) return } fmt.Printf("%s command created at %s/cmd/%s.go\n", command.CmdName, command.AbsolutePath, command.CmdName) // Output: server command created at /path/to/myapp/cmd/server.go // Generated file contains: // - Package declaration // - Import statements // - Command variable (serverCmd) // - init() function that registers with parent } ``` -------------------------------- ### Programmatically Create Cobra Project (Go) Source: https://context7.com/spf13/cobra-cli/llms.txt This API function generates a Cobra project structure including main.go, cmd/root.go, and LICENSE using the cmd.Project type. It requires importing cobra-cli/cmd and setting properties like path, package name, and license details. Outputs files at the specified absolute path; limitations: Manual error handling needed, and it assumes a valid Go environment. ```go package main import ( "fmt" "github.com/spf13/cobra-cli/cmd" ) func main() { // Example: Programmatically create a project structure project := &cmd.Project{ AbsolutePath: "/path/to/myapp", PkgName: "github.com/username/myapp", AppName: "myapp", Copyright: "Copyright © 2024 John Doe", Viper: true, Legal: cmd.License{ Name: "MIT", Header: "Licensed under MIT", Text: "MIT License text...", }, } if err := project.Create(); err != nil { fmt.Printf("Error creating project: %v\n", err) return } fmt.Println("Project created successfully at", project.AbsolutePath) // Output: Project created successfully at /path/to/myapp // Files created: // - /path/to/myapp/main.go // - /path/to/myapp/cmd/root.go // - /path/to/myapp/LICENSE } ``` -------------------------------- ### Cobra Generator YAML Configuration Source: https://github.com/spf13/cobra-cli/blob/main/README.md Demonstrates a YAML configuration file for the Cobra generator. This file helps reduce repeated input in flags and includes author, license, and Viper usage settings. ```yaml author: Steve Francia license: MIT useViper: true ``` -------------------------------- ### Manage License Templates in cobra-cli Source: https://context7.com/spf13/cobra-cli/llms.txt Shows how to work with built-in and custom license templates using the cobra-cli library and Viper configuration. Demonstrates retrieving licenses by name/alias, iterating through available licenses, and configuring custom license headers and text with templating support for dynamic content like copyright information. ```go package main import ( "fmt" "github.com/spf13/cobra-cli/cmd" "github.com/spf13/viper" ) func main() { // Example: Working with licenses programmatically // Set up Viper configuration viper.Set("author", "Jane Developer ") viper.Set("year", "2024") // Get a built-in license mitLicense := cmd.Licenses["mit"] fmt.Printf("License: %s\n", mitLicense.Name) // Output: License: MIT // Find license by name or alias license := cmd.Licenses["apache2"] if license.Name != "" { fmt.Printf("Found: %s\n", license.Name) // Output: Found: Apache 2.0 } // Available built-in licenses: licenses := []string{"mit", "apache2", "gpl2", "gpl3", "lgpl", "agpl", "bsd2", "bsd3", "none"} for _, name := range licenses { if lic, exists := cmd.Licenses[name]; exists { fmt.Printf("- %s: %v matches\n", lic.Name, len(lic.PossibleMatches)) } } // Output: // - MIT: 2 matches // - Apache 2.0: 3 matches // - GPL 2.0: 2 matches // ... // Custom license configuration viper.Set("license.header", "Proprietary Software") viper.Set("license.text", "All rights reserved.\n{{ .copyright }}") customLicense := cmd.License{ Header: viper.GetString("license.header"), Text: viper.GetString("license.text"), } fmt.Printf("Custom license header: %s\n", customLicense.Header) // Output: Custom license header: Proprietary Software } ``` -------------------------------- ### Configure Cobra CLI with YAML (Bash and YAML) Source: https://context7.com/spf13/cobra-cli/llms.txt This sets up a YAML configuration file for Cobra CLI defaults like author, license, and Viper usage, avoiding repeated flags. The bash command creates ~/.cobra.yaml; YAML defines settings including custom license templates. Outputs persistent config for init/add; limitations: Global file affects all projects unless overridden by flags or local paths. ```bash # Create configuration file at ~/.cobra.yaml cat > ~/.cobra.yaml << 'EOF' author: Steve Francia license: MIT useViper: true EOF # Now init and add commands use these defaults automatically cd newproject go mod init github.com/username/newproject cobra-cli init # Automatically uses author, license, and viper settings from ~/.cobra.yaml # Custom license configuration with template cat > ~/.cobra.yaml << 'EOF' author: Company Name year: 2024 license: header: This file is part of CLI application foo. text: | {{ .copyright }} Custom license text here. All rights reserved. EOF # Override config file defaults with flags cobra-cli init --license apache --config /path/to/custom/cobra.yaml ``` ```yaml author: Steve Francia license: MIT useViper: true ``` ```yaml author: Company Name year: 2024 license: header: This file is part of CLI application foo. text: | {{ .copyright }} Custom license text here. All rights reserved. ``` -------------------------------- ### Add Commands to Existing Cobra Application (Bash) Source: https://context7.com/spf13/cobra-cli/llms.txt This adds new command files to an existing Cobra project, creating Go files in cmd/ and registering them with parent commands if specified. It works in the project root, supporting flags for license and author; outputs include new .go files with command stubs. Limitations: Assumes an existing init'ed project; subcommands require parent command names. ```bash # Navigate to your application directory cd myapp # Add a top-level command cobra-cli add serve # Creates: cmd/serve.go with serveCmd # Add another top-level command cobra-cli add config # Add a subcommand with a parent cobra-cli add create -p 'configCmd' # Creates: cmd/create.go registered under configCmd # Add command with license and author cobra-cli add deploy --license apache --author "DevOps Team ops@example.com" # Test the new commands go run main.go serve # Output: "serve called" go run main.go config create # Output: "create called" go run main.go help serve # Output: Detailed help for the serve command # Resulting structure: # myapp/ # ├── main.go # ├── cmd/ # │ ├── root.go # │ ├── serve.go # │ ├── config.go # │ ├── create.go # │ └── deploy.go ``` -------------------------------- ### Validate and Transform Command Names in Go Source: https://context7.com/spf13/cobra-cli/llms.txt Implements a function to convert dash-case and snake_case command names to valid Go camelCase identifiers. Processes special characters by converting following letters to uppercase and handles edge cases like consecutive separators or trailing separators. Includes test cases demonstrating various input-output transformations. ```go package main import ( "fmt" ) // validateCmdName converts command names to valid Go identifiers func validateCmdName(source string) string { // Implementation from cmd/add.go i := 0 l := len(source) var output string for i < l { if source[i] == '-' || source[i] == '_' { if output == "" { output = source[:i] } if i == l-1 { break } if source[i+1] == '-' || source[i+1] == '_' { i++ continue } output += string(source[i+1] - 32) // Convert to uppercase i += 2 continue } if output != "" { output += string(source[i]) } i++ } if output == "" { return source } return output } func main() { // Example: Command name transformations examples := map[string]string{ "add-user": "addUser", "get_config": "getConfig", "list-all-items": "listAllItems", "simple": "simple", "API_server": "APIServer", "test--double": "testDouble", "end-": "end", } for input, expected := range examples { result := validateCmdName(input) status := "✓" if result != expected { status = "✗" } fmt.Printf("%s %s -> %s (expected: %s)\n", status, input, result, expected) } // Output: // ✓ add-user -> addUser (expected: addUser) // ✓ get_config -> getConfig (expected: getConfig) // ✓ list-all-items -> listAllItems (expected: listAllItems) // ✓ simple -> simple (expected: simple) // ✓ API_server -> APIServer (expected: APIServer) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.