// BackupTasks reads from the original task file and append the content to the backup file
// If doneOnly flag is provided, only the finished tasks will be back up
BackupTasks reads from the original task file and append the content to the backup file
If doneOnly flag is provided, only the finished tasks will be back up
// BackupTasks reads from the original task file and append the content to the backup file // If doneOnly flag is provided only the finished tasks will be back up
func BackupTasks(path string, doneOnly bool) error {
	var allTask []Task       
	var originalTasks []Task 
	var bkTasks []Task       
	allTask, err := ReadTasks(path)
	if err != nil {
		return err
	}
	for _, task := range allTask {
		if doneOnly {
			if task.Done == "x" {
				bkTasks = append(bkTasks, task)
			} else {
				originalTasks = append(originalTasks, task)
			}
		} else {
			bkTasks = append(bkTasks, task)
		}
	}
	if len(bkTasks) == 0 {
		return fmt.Errorf("/nNo new todo items for archive. /n")
	}
	backupPath := path + ".bk"                   
	err = WriteTasks(backupPath, bkTasks, false) 
	if err != nil {
		return fmt.Errorf("Can not write to backup file - %s \n", backupPath)
	}
	if len(originalTasks) > 0 {
		err = WriteTasks(path, originalTasks, true) 
		if err != nil {
			return fmt.Errorf("Can not write back to todo file - %s \n", backupPath)
		}
		fmt.Printf("\nWritten %d lines to todo file [%s]\n\n", len(originalTasks), path)
	}
	fmt.Printf("\nWritten %d lines to backup file [%s]\n\n", len(bkTasks), backupPath)
	return nil
}
func BackupTasks ( path string , doneOnly bool ) error { var allTask [ ] Task var originalTasks [ ] Task var bkTasks [ ] Task allTask , err := ReadTasks ( path ) if err != nil { return err } for _ , task := range allTask { if doneOnly { if task . Done == " " { bkTasks = append ( bkTasks , task ) } else { originalTasks = append ( originalTasks , task ) } } else { bkTasks = append ( bkTasks , task ) } } if len ( bkTasks ) == 0 { return fmt . Errorf ( " " ) } backupPath := path + " " err = WriteTasks ( backupPath , bkTasks , false ) if err != nil { return fmt . Errorf ( " \n " , backupPath ) } if len ( originalTasks ) > 0 { err = WriteTasks ( path , originalTasks , true ) if err != nil { return fmt . Errorf ( " \n " , backupPath ) } fmt . Printf ( " \n \n \n " , len ( originalTasks ) , path ) } fmt . Printf ( " \n \n \n " , len ( bkTasks ) , backupPath ) return nil }
BackupTasks reads from the original task file and append the content to the backup file
If doneOnly flag is provided, only the finished tasks will be back up
BackupTasks reads from the original task file and append the content to the backup file If doneOnly flag is provided only the finished tasks will be back up
// task from the original file // original task that would be written back to the original files / undone tasks // done tasks to be backuped / done tasks // Read tasks // Handle doneOnly flag and backup all // Backup all // Append to backup files // ~/todo.txt.bk // append // Write back to oringal file if any (undone tasks) // overwrite