🏗️ InitProject
Initializes a new Composer project by generating a basic composer.json file, equivalent to running composer init (interactive).
When to use
Use this when you need to create a brand-new composer.json in an empty directory and accept Composer's interactive prompts. The method interactively asks for the project name, description, author, dependencies, and so on.
Signature
go
func (c *Composer) InitProject() errorParameters
This method takes no parameters.
Return value
error: Returns the corresponding error message when an error occurs during project initialization; nil on success.
Note
composer init is interactive by default. Using this method in a non-interactive automation script may fail due to missing input; use InitProjectWithOptions instead and pass --no-interaction in the options.
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("Initialization failed: %v", err)
}
// Initialize composer.json in the current working directory
if err := comp.InitProject(); err != nil {
log.Fatalf("Project init failed: %v", err)
}
fmt.Println("Project initialized")
}Advanced
- To non-interactively specify project name, description, author, and other metadata, use the
InitProjectWithOptionsvariant. - To create a full project based on a framework skeleton (rather than an empty
composer.json), useCreateProject. - After initialization, you can use
GetProjectInfoto read the generated project information.