82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package domain
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"redmine_tree/domain/model" // Importing the model package
|
|
)
|
|
|
|
func Run() {
|
|
ui := NewUI() // Instantiate UI
|
|
|
|
// Load .env file
|
|
err := godotenv.Load()
|
|
if err != nil && !os.IsNotExist(err) {
|
|
ui.PrintError("Error loading .env file: %v\n", err)
|
|
}
|
|
|
|
// Load config from ~/.redmine-tree.json
|
|
config, err := LoadConfig()
|
|
if err != nil {
|
|
ui.PrintError("Error loading config file: %v\n", err)
|
|
}
|
|
|
|
baseURL := ""
|
|
apiKey := ""
|
|
|
|
// Try to get values from config file first
|
|
if config != nil {
|
|
baseURL = config.Host
|
|
apiKey = config.Token
|
|
}
|
|
|
|
// Environment variables override config file
|
|
if envBaseURL := os.Getenv("REDMINE_URL"); envBaseURL != "" {
|
|
baseURL = envBaseURL
|
|
}
|
|
if envAPIKey := os.Getenv("REDMINE_TOKEN"); envAPIKey != "" {
|
|
apiKey = envAPIKey
|
|
}
|
|
|
|
var projectID string
|
|
|
|
// projectID must be provided as a command-line argument, or list projects
|
|
if len(os.Args) > 1 {
|
|
projectID = os.Args[1]
|
|
} else {
|
|
projects, err := model.FetchAllProjects(baseURL, apiKey)
|
|
if err != nil {
|
|
ui.PrintError("Error fetching projects: %v\n", err)
|
|
}
|
|
ui.PrintProjects(projects)
|
|
os.Exit(0)
|
|
}
|
|
|
|
if baseURL == "" || apiKey == "" {
|
|
ui.PrintUsage(os.Args[0]) // Pass app name for usage message
|
|
}
|
|
|
|
project, err := model.FetchProject(baseURL, apiKey, projectID)
|
|
if err != nil {
|
|
ui.PrintError("Error fetching project details: %v\n", err)
|
|
}
|
|
|
|
ui.PrintFetchingProjectDetails(project.Name, projectID)
|
|
|
|
issues, err := model.FetchAllIssues(baseURL, apiKey, projectID)
|
|
if err != nil {
|
|
ui.PrintError("Error fetching issues: %v\n", err)
|
|
}
|
|
|
|
ui.PrintTotalIssuesFetched(len(issues))
|
|
|
|
roots := model.BuildTree(issues) // Corrected call
|
|
ui.PrintIssueTreeHeader(len(roots))
|
|
model.PrintTree(roots, "", false) // Corrected call
|
|
|
|
// Print summary
|
|
ui.PrintSummary(len(issues), len(roots))
|
|
}
|