Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
d4r1us-drk committed Nov 26, 2024
2 parents ef966d3 + f76d026 commit 467102c
Show file tree
Hide file tree
Showing 10 changed files with 918 additions and 0 deletions.
127 changes: 127 additions & 0 deletions cmd/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package cmd

import (
"fmt"
"strconv"
"time"

"github.com/d4r1us-drk/clido/pkg/repository"
"github.com/d4r1us-drk/clido/pkg/utils"
"github.com/spf13/cobra"
)

var editCmd = &cobra.Command{
Use: "edit [project|task] <id>",
Short: "Edit an existing project or task",
Long: `Edit the details of an existing project or task identified by its ID.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
fmt.Println("Insufficient arguments. Use 'edit project <id>' or 'edit task <id>'.")
return
}

repo, err := repository.NewRepository()
if err != nil {
fmt.Printf("Error initializing repository: %v\n", err)
return
}
defer repo.Close()

id, err := strconv.Atoi(args[1])
if err != nil {
fmt.Println("Invalid ID. Please provide a numeric ID.")
return
}

switch args[0] {
case "project":
editProject(cmd, repo, id)
case "task":
editTask(cmd, repo, id)
default:
fmt.Println("Invalid option. Use 'edit project <id>' or 'edit task <id>'.")
}
},
}

func init() {
rootCmd.AddCommand(editCmd)

editCmd.Flags().StringP("name", "n", "", "New name")
editCmd.Flags().StringP("description", "d", "", "New description")
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)")
}

func editProject(cmd *cobra.Command, repo *repository.Repository, id int) {
project, err := repo.GetProjectByID(id)
if err != nil {
fmt.Printf("Error retrieving project: %v\n", err)
return
}

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

if name != "" {
project.Name = name
}
if description != "" {
project.Description = description
}

err = repo.UpdateProject(project)
if err != nil {
fmt.Printf("Error updating project: %v\n", err)
return
}

fmt.Printf("Project '%s' updated successfully.\n", project.Name)
}

func editTask(cmd *cobra.Command, repo *repository.Repository, id int) {
task, err := repo.GetTaskByID(id)
if err != nil {
fmt.Printf("Error retrieving task: %v\n", err)
return
}

name, _ := cmd.Flags().GetString("name")
description, _ := cmd.Flags().GetString("description")
dueDateStr, _ := cmd.Flags().GetString("due")
priority, _ := cmd.Flags().GetInt("priority")

if name != "" {
task.Name = name
}
if description != "" {
task.Description = description
}
if dueDateStr != "" {
parsedDate, err := time.Parse("2006-01-02 15:04", dueDateStr)
if err == nil {
task.DueDate = &parsedDate
} else {
fmt.Println("Invalid date format. Keeping the existing due date.")
}
}
if priority != 0 {
if priority >= 1 && priority <= 4 {
task.Priority = priority
} else {
fmt.Println("Invalid priority. Keeping the existing priority.")
}
}

err = repo.UpdateTask(task)
if err != nil {
fmt.Printf("Error updating task: %v\n", err)
return
}

fmt.Printf("Task '%s' updated successfully.\n", task.Name)
fmt.Printf("New details: Priority: %s, Due Date: %s\n",
utils.GetPriorityString(task.Priority),
utils.FormatDate(task.DueDate))
}
159 changes: 159 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"strconv"

"github.com/d4r1us-drk/clido/pkg/models"
"github.com/d4r1us-drk/clido/pkg/repository"
"github.com/d4r1us-drk/clido/pkg/utils"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

var listCmd = &cobra.Command{
Use: "list [projects|tasks]",
Short: "List projects or tasks",
Long: `List all projects or tasks, optionally filtered by project for tasks.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Insufficient arguments. Use 'list projects' or 'list tasks'.")
return
}

repo, err := repository.NewRepository()
if err != nil {
fmt.Printf("Error initializing repository: %v\n", err)
return
}
defer repo.Close()

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

switch args[0] {
case "projects":
listProjects(repo, outputJSON)
case "tasks":
projectFilter, _ := cmd.Flags().GetString("project")
listTasks(repo, projectFilter, outputJSON)
default:
fmt.Println("Invalid option. Use 'list projects' or 'list tasks'.")
}
},
}

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")
}

func listProjects(repo *repository.Repository, outputJSON bool) {
projects, err := repo.GetAllProjects()
if err != nil {
fmt.Printf("Error listing projects: %v\n", err)
return
}

if outputJSON {
jsonData, err := json.MarshalIndent(projects, "", " ")
if err != nil {
fmt.Printf("Error marshalling projects to JSON: %v\n", err)
return
}
fmt.Println(string(jsonData))
} else {
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) {
var tasks []*models.Task
var err error

if projectFilter != "" {
var project *models.Project
if utils.IsNumeric(projectFilter) {
projectID, _ := strconv.Atoi(projectFilter)
project, err = repo.GetProjectByID(projectID)
} else {
project, err = repo.GetProjectByName(projectFilter)
}

if err != nil || project == nil {
fmt.Printf("Project '%s' not found.\n", projectFilter)
return
}

tasks, err = repo.GetTasksByProjectID(project.ID)
if err != nil {
fmt.Printf("Error listing tasks: %v\n", err)
return
}

if !outputJSON {
fmt.Printf("Tasks in project '%s':\n", project.Name)
}
} else {
tasks, err = repo.GetAllTasks()
if err != nil {
fmt.Printf("Error listing tasks: %v\n", err)
return
}

if !outputJSON {
fmt.Println("All Tasks:")
}
}

if outputJSON {
jsonData, err := json.MarshalIndent(tasks, "", " ")
if err != nil {
fmt.Printf("Error marshalling tasks to JSON: %v\n", err)
return
}
fmt.Println(string(jsonData))
} else {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"ID", "Name", "Description", "Due Date", "Completed", "Past Due", "Priority", "Project",
})
table.SetRowLine(true)

for _, task := range tasks {
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),
})
}

table.Render()
}
}
Loading

0 comments on commit 467102c

Please sign in to comment.