Last active
February 4, 2019 01:58
-
-
Save wemgl/b339eb138390678f9b9bd1bd8bf2543b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c, err := db.Collection(collName).Find(ctx, bson.D{}) | |
if err != nil { | |
return fmt.Errorf("readTasks: couldn't list all to-dos: %v", err) | |
} | |
defer c.Close(ctx) | |
tw := tabwriter.NewWriter(os.Stdout, 24, 2, 4, ' ', tabwriter.TabIndent) | |
_, _ = fmt.Fprintln(tw, "ID\tCreated At\tModified At\tTask\t") | |
for c.Next(ctx) { | |
elem := &bson.D{} | |
if err = c.Decode(elem); err != nil { | |
return fmt.Errorf("readTasks: couldn't make to-do item ready for display: %v", err) | |
} | |
m := elem.Map() | |
t := todo{ | |
ID: m["_id"].(primitive.ObjectID).Hex(), | |
CreatedAt: dateTimeMillis(m["createdAt"].(primitive.DateTime)), | |
ModifiedAt: dateTimeMillis(m["modifiedAt"].(primitive.DateTime)), | |
Task: m["task"].(string), | |
} | |
output := fmt.Sprintf("%s\t%s\t%s\t%s\t", | |
t.ID, | |
formatForDisplay(t.CreatedAt), | |
formatForDisplay(t.ModifiedAt), | |
t.Task, | |
) | |
_, _ = fmt.Fprintln(tw, output) | |
if err = tw.Flush(); err != nil { | |
return fmt.Errorf("readTasks: all data for the to-do couldn't be printed: %v", err) | |
} | |
} | |
if err = c.Err(); err != nil { | |
return fmt.Errorf("readTasks: all to-do items couldn't be listed: %v", err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment