Skip to content

Commit

Permalink
Merge pull request #12 from vhespanha/main
Browse files Browse the repository at this point in the history
Revert "First implementation of subprojects and subtasks, needs testing"
  • Loading branch information
d4r1us-drk authored Aug 17, 2024
2 parents d1714e6 + e3c5316 commit f76d026
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 334 deletions.
26 changes: 0 additions & 26 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ func init() {

editCmd.Flags().StringP("name", "n", "", "New name")
editCmd.Flags().StringP("description", "d", "", "New description")
editCmd.Flags().StringP("project", "p", "", "New parent project name or ID")
editCmd.Flags().StringP("task", "t", "", "New parent task ID for subtasks")
editCmd.Flags().StringP("due", "D", "", "New due date for task (format: YYYY-MM-DD HH:MM)")
editCmd.Flags().
IntP("priority", "r", 0, "New priority for task (1: High, 2: Medium, 3: Low, 4: None)")
Expand All @@ -65,27 +63,13 @@ func editProject(cmd *cobra.Command, repo *repository.Repository, id int) {

name, _ := cmd.Flags().GetString("name")
description, _ := cmd.Flags().GetString("description")
parentProjectIdentifier, _ := cmd.Flags().GetString("project")

if name != "" {
project.Name = name
}
if description != "" {
project.Description = description
}
if parentProjectIdentifier != "" {
if utils.IsNumeric(parentProjectIdentifier) {
parentID, _ := strconv.Atoi(parentProjectIdentifier)
project.ParentProjectId = &parentID
} else {
parentProject, err := repo.GetProjectByName(parentProjectIdentifier)
if err != nil || parentProject == nil {
fmt.Printf("Parent project '%s' not found.\n", parentProjectIdentifier)
return
}
project.ParentProjectId = &parentProject.ID
}
}

err = repo.UpdateProject(project)
if err != nil {
Expand All @@ -107,7 +91,6 @@ func editTask(cmd *cobra.Command, repo *repository.Repository, id int) {
description, _ := cmd.Flags().GetString("description")
dueDateStr, _ := cmd.Flags().GetString("due")
priority, _ := cmd.Flags().GetInt("priority")
parentTaskIdentifier, _ := cmd.Flags().GetString("task")

if name != "" {
task.Name = name
Expand All @@ -130,15 +113,6 @@ func editTask(cmd *cobra.Command, repo *repository.Repository, id int) {
fmt.Println("Invalid priority. Keeping the existing priority.")
}
}
if parentTaskIdentifier != "" {
if utils.IsNumeric(parentTaskIdentifier) {
parentID, _ := strconv.Atoi(parentTaskIdentifier)
task.ParentTaskId = &parentID
} else {
fmt.Println("Parent task must be identified by a numeric ID.")
return
}
}

err = repo.UpdateTask(task)
if err != nil {
Expand Down
156 changes: 38 additions & 118 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"strconv"
"strings"

"github.com/d4r1us-drk/clido/pkg/models"
"github.com/d4r1us-drk/clido/pkg/repository"
Expand All @@ -32,14 +31,13 @@ var listCmd = &cobra.Command{
defer repo.Close()

outputJSON, _ := cmd.Flags().GetBool("json")
treeView, _ := cmd.Flags().GetBool("tree")

switch args[0] {
case "projects":
listProjects(repo, outputJSON, treeView)
listProjects(repo, outputJSON)
case "tasks":
projectFilter, _ := cmd.Flags().GetString("project")
listTasks(repo, projectFilter, outputJSON, treeView)
listTasks(repo, projectFilter, outputJSON)
default:
fmt.Println("Invalid option. Use 'list projects' or 'list tasks'.")
}
Expand All @@ -50,10 +48,9 @@ func init() {
rootCmd.AddCommand(listCmd)
listCmd.Flags().StringP("project", "p", "", "Filter tasks by project name or ID")
listCmd.Flags().BoolP("json", "j", false, "Output list in JSON format")
listCmd.Flags().BoolP("tree", "t", false, "Display projects or tasks in a tree-like structure")
}

func listProjects(repo *repository.Repository, outputJSON bool, treeView bool) {
func listProjects(repo *repository.Repository, outputJSON bool) {
projects, err := repo.GetAllProjects()
if err != nil {
fmt.Printf("Error listing projects: %v\n", err)
Expand All @@ -67,14 +64,25 @@ func listProjects(repo *repository.Repository, outputJSON bool, treeView bool) {
return
}
fmt.Println(string(jsonData))
} else if treeView {
printProjectTree(repo, projects, nil, 0)
} else {
printProjectTable(repo, projects)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Name", "Description"})
table.SetRowLine(true)

for _, project := range projects {
table.Append([]string{
strconv.Itoa(project.ID),
utils.WrapText(project.Name, 30),
utils.WrapText(project.Description, 50),
})
}

fmt.Println("Projects:")
table.Render()
}
}

func listTasks(repo *repository.Repository, projectFilter string, outputJSON bool, treeView bool) {
func listTasks(repo *repository.Repository, projectFilter string, outputJSON bool) {
var tasks []*models.Task
var err error

Expand Down Expand Up @@ -120,120 +128,32 @@ func listTasks(repo *repository.Repository, projectFilter string, outputJSON boo
return
}
fmt.Println(string(jsonData))
} else if treeView {
printTaskTree(repo, tasks, nil, 0)
} else {
printTaskTable(repo, tasks)
}
}

func printProjectTable(repo *repository.Repository, projects []*models.Project) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Name", "Description", "Type", "Child Of"})
table.SetRowLine(true)

for _, project := range projects {
typeField := "Parent"
parentChildField := "None"
if project.ParentProjectId != nil {
typeField = "Child"
parentProject, _ := repo.GetProjectByID(*project.ParentProjectId)
if parentProject != nil {
parentChildField = parentProject.Name
}
} else {
subprojects, _ := repo.GetSubprojects(project.ID)
if len(subprojects) > 0 {
typeField = "Parent"
}
}

table.Append([]string{
strconv.Itoa(project.ID),
utils.WrapText(project.Name, 30),
utils.WrapText(project.Description, 50),
typeField,
parentChildField,
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"ID", "Name", "Description", "Due Date", "Completed", "Past Due", "Priority", "Project",
})
}
table.SetRowLine(true)

fmt.Println("Projects:")
table.Render()
}

func printTaskTable(repo *repository.Repository, tasks []*models.Task) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"ID", "Name", "Description", "Due Date", "Completed", "Past Due", "Priority", "Project", "Type", "Parent/Child Of",
})
table.SetRowLine(true)

for _, task := range tasks {
typeField := "Parent"
parentChildField := "None"
if task.ParentTaskId != nil {
typeField = "Child"
parentTask, _ := repo.GetTaskByID(*task.ParentTaskId)
if parentTask != nil {
parentChildField = parentTask.Name
for _, task := range tasks {
project, _ := repo.GetProjectByID(task.ProjectID)
projectName := ""
if project != nil {
projectName = project.Name
}
} else {
subtasks, _ := repo.GetSubtasks(task.ID)
if len(subtasks) > 0 {
typeField = "Parent"
}
}

project, _ := repo.GetProjectByID(task.ProjectID)
projectName := ""
if project != nil {
projectName = project.Name
}

table.Append([]string{
strconv.Itoa(task.ID),
utils.WrapText(task.Name, 20),
utils.WrapText(task.Description, 30),
utils.FormatDate(task.DueDate),
fmt.Sprintf("%v", task.TaskCompleted),
utils.ColoredPastDue(task.DueDate, task.TaskCompleted),
utils.GetPriorityString(task.Priority),
utils.WrapText(projectName, 20),
typeField,
parentChildField,
})
}

table.Render()
}

func printProjectTree(repo *repository.Repository, projects []*models.Project, parentID *int, level int) {
indent := strings.Repeat("│ ", level)
for i, project := range projects {
if (parentID == nil && project.ParentProjectId == nil) || (parentID != nil && project.ParentProjectId != nil && *project.ParentProjectId == *parentID) {
prefix := "├──"
if i == len(projects)-1 {
prefix = "└──"
}
fmt.Printf("%s%s %s (ID: %d)\n", indent, prefix, project.Name, project.ID)
printProjectTree(repo, projects, &project.ID, level+1)
table.Append([]string{
strconv.Itoa(task.ID),
utils.WrapText(task.Name, 20),
utils.WrapText(task.Description, 30),
utils.FormatDate(task.DueDate),
fmt.Sprintf("%v", task.TaskCompleted),
utils.ColoredPastDue(task.DueDate, task.TaskCompleted),
utils.GetPriorityString(task.Priority),
utils.WrapText(projectName, 20),
})
}
}
}

func printTaskTree(repo *repository.Repository, tasks []*models.Task, parentID *int, level int) {
indent := strings.Repeat("│ ", level)
for i, task := range tasks {
if (parentID == nil && task.ParentTaskId == nil) || (parentID != nil && task.ParentTaskId != nil && *task.ParentTaskId == *parentID) {
prefix := "├──"
if i == len(tasks)-1 {
prefix = "└──"
}
fmt.Printf("%s%s %s (ID: %d)\n", indent, prefix, task.Name, task.ID)
fmt.Printf("%s Description: %s\n", indent, task.Description)
fmt.Printf("%s Due Date: %s, Completed: %v, Priority: %s\n",
indent, utils.FormatDate(task.DueDate), task.TaskCompleted, utils.GetPriorityString(task.Priority))
printTaskTree(repo, tasks, &task.ID, level+1)
}
table.Render()
}
}
75 changes: 20 additions & 55 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func init() {

newCmd.Flags().StringP("name", "n", "", "Name of the project or task")
newCmd.Flags().StringP("description", "d", "", "Description of the project or task")
newCmd.Flags().StringP("project", "p", "", "Parent project name or ID for subprojects or tasks")
newCmd.Flags().StringP("task", "t", "", "Parent task ID for subtasks")
newCmd.Flags().StringP("project", "p", "", "Project name or ID for the task")
newCmd.Flags().StringP("due", "D", "", "Due date for the task (format: YYYY-MM-DD HH:MM)")
newCmd.Flags().
IntP("priority", "r", 4, "Priority of the task (1: High, 2: Medium, 3: Low, 4: None)")
Expand All @@ -54,32 +53,15 @@ func init() {
func createProject(cmd *cobra.Command, repo *repository.Repository) {
name, _ := cmd.Flags().GetString("name")
description, _ := cmd.Flags().GetString("description")
parentProjectIdentifier, _ := cmd.Flags().GetString("project")

if name == "" {
fmt.Println("Project name is required.")
return
}

var parentProjectID *int
if parentProjectIdentifier != "" {
if utils.IsNumeric(parentProjectIdentifier) {
id, _ := strconv.Atoi(parentProjectIdentifier)
parentProjectID = &id
} else {
parentProject, err := repo.GetProjectByName(parentProjectIdentifier)
if err != nil || parentProject == nil {
fmt.Printf("Parent project '%s' not found.\n", parentProjectIdentifier)
return
}
parentProjectID = &parentProject.ID
}
}

project := &models.Project{
Name: name,
Description: description,
ParentProjectId: parentProjectID,
Name: name,
Description: description,
}

err := repo.CreateProject(project)
Expand All @@ -95,43 +77,27 @@ func createTask(cmd *cobra.Command, repo *repository.Repository) {
name, _ := cmd.Flags().GetString("name")
description, _ := cmd.Flags().GetString("description")
projectIdentifier, _ := cmd.Flags().GetString("project")
parentTaskIdentifier, _ := cmd.Flags().GetString("task")
dueDateStr, _ := cmd.Flags().GetString("due")
priority, _ := cmd.Flags().GetInt("priority")

if name == "" {
fmt.Println("Task name is required.")
if name == "" || projectIdentifier == "" {
fmt.Println("Task name and project identifier are required.")
return
}

var projectID int
var parentTaskID *int
var project *models.Project
var err error

if projectIdentifier != "" {
if utils.IsNumeric(projectIdentifier) {
id, _ := strconv.Atoi(projectIdentifier)
projectID = id
} else {
project, err := repo.GetProjectByName(projectIdentifier)
if err != nil || project == nil {
fmt.Printf("Project '%s' not found.\n", projectIdentifier)
return
}
projectID = project.ID
}
if utils.IsNumeric(projectIdentifier) {
projectID, _ := strconv.Atoi(projectIdentifier)
project, err = repo.GetProjectByID(projectID)
} else {
fmt.Println("Task must be associated with a project.")
return
project, err = repo.GetProjectByName(projectIdentifier)
}

if parentTaskIdentifier != "" {
if utils.IsNumeric(parentTaskIdentifier) {
id, _ := strconv.Atoi(parentTaskIdentifier)
parentTaskID = &id
} else {
fmt.Println("Parent task must be identified by a numeric ID.")
return
}
if err != nil || project == nil {
fmt.Printf("Project '%s' not found.\n", projectIdentifier)
return
}

var dueDate *time.Time
Expand All @@ -145,15 +111,14 @@ func createTask(cmd *cobra.Command, repo *repository.Repository) {
}

task := &models.Task{
Name: name,
Description: description,
ProjectID: projectID,
DueDate: dueDate,
Priority: priority,
ParentTaskId: parentTaskID,
Name: name,
Description: description,
ProjectID: project.ID,
DueDate: dueDate,
Priority: priority,
}

err := repo.CreateTask(task)
err = repo.CreateTask(task)
if err != nil {
fmt.Printf("Error creating task: %v\n", err)
return
Expand Down
Loading

0 comments on commit f76d026

Please sign in to comment.