-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable displaying list of advanced checklists.
- Loading branch information
1 parent
ff6aef2
commit dc11a11
Showing
8 changed files
with
275 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package sncli | ||
|
||
import ( | ||
"fmt" | ||
"github.com/alexeyco/simpletable" | ||
"github.com/gookit/color" | ||
"github.com/jonhadfield/gosn-v2/cache" | ||
"github.com/jonhadfield/gosn-v2/items" | ||
"slices" | ||
"time" | ||
) | ||
|
||
func conflictedWarning([]items.Checklist) string { | ||
if len(items.Checklists{}) > 0 { | ||
return color.Yellow.Sprintf("%d conflicted versions", len(items.Checklists{})) | ||
} | ||
|
||
return "-" | ||
} | ||
|
||
func (ci *ListChecklistsInput) Run() (err error) { | ||
checklists, err := getChecklists(ci.Session) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
table := simpletable.New() | ||
|
||
table.Header = &simpletable.Header{ | ||
Cells: []*simpletable.Cell{ | ||
{Align: simpletable.AlignCenter, Text: "Title"}, | ||
{Align: simpletable.AlignCenter, Text: "Last Updated"}, | ||
{Align: simpletable.AlignCenter, Text: "UUID"}, | ||
{Align: simpletable.AlignCenter, Text: "Issues"}, | ||
}, | ||
} | ||
|
||
for _, row := range checklists { | ||
r := []*simpletable.Cell{ | ||
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", row.Title)}, | ||
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", row.UpdatedAt.String())}, | ||
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", row.UUID)}, | ||
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", conflictedWarning(row.Duplicates))}, | ||
} | ||
|
||
table.Body.Cells = append(table.Body.Cells, r) | ||
} | ||
|
||
table.SetStyle(simpletable.StyleCompactLite) | ||
fmt.Println(table.String()) | ||
|
||
return nil | ||
} | ||
|
||
// construct a map of duplicates | ||
func getChecklistsDuplicatesMap(checklistNotes items.Notes) (map[string][]items.Checklist, error) { | ||
duplicates := make(map[string][]items.Checklist) | ||
|
||
for x := range checklistNotes { | ||
if checklistNotes[x].DuplicateOf != "" { | ||
// checklist is a duplicate | ||
// get the checklist content | ||
cl, err := checklistNotes[x].Content.ToCheckList() | ||
if err != nil { | ||
return map[string][]items.Checklist{}, err | ||
} | ||
|
||
// skip trashed content | ||
if cl.Trashed { | ||
continue | ||
} | ||
|
||
cl.UUID = checklistNotes[x].UUID | ||
cl.UpdatedAt, err = time.Parse(timeLayout, checklistNotes[x].UpdatedAt) | ||
if err != nil { | ||
return map[string][]items.Checklist{}, err | ||
} | ||
|
||
duplicates[checklistNotes[x].DuplicateOf] = append(duplicates[checklistNotes[x].DuplicateOf], cl) | ||
} | ||
} | ||
|
||
return duplicates, nil | ||
} | ||
|
||
func getChecklists(sess *cache.Session) (items.Checklists, error) { | ||
var so cache.SyncOutput | ||
|
||
so, err := Sync(cache.SyncInput{ | ||
Session: sess, | ||
}, true) | ||
if err != nil { | ||
return items.Checklists{}, err | ||
} | ||
|
||
var allPersistedItems cache.Items | ||
|
||
if err = so.DB.All(&allPersistedItems); err != nil { | ||
return items.Checklists{}, err | ||
} | ||
|
||
allItemUUIDs := allPersistedItems.UUIDs() | ||
|
||
var gitems items.Items | ||
gitems, err = allPersistedItems.ToItems(sess) | ||
if err != nil { | ||
return items.Checklists{}, err | ||
} | ||
|
||
gitems.Filter(items.ItemFilters{ | ||
Filters: []items.Filter{ | ||
{ | ||
Type: "Note", | ||
Key: "editor", | ||
Comparison: "==", | ||
Value: "com.sncommunity.advanced-checklist", | ||
}, | ||
}, | ||
}) | ||
|
||
var checklists items.Checklists | ||
checklistNotes := gitems.Notes() | ||
|
||
duplicatesMap, err := getChecklistsDuplicatesMap(checklistNotes) | ||
// strip any duplicated items that no longer exist | ||
for k := range duplicatesMap { | ||
if !slices.Contains(allItemUUIDs, k) { | ||
delete(duplicatesMap, k) | ||
} | ||
} | ||
|
||
// second pass to get all non-deleted and non-trashed checklists | ||
for x := range checklistNotes { | ||
// strip deleted and trashed | ||
if checklistNotes[x].Deleted || checklistNotes[x].Content.Trashed != nil && *checklistNotes[x].Content.Trashed { | ||
continue | ||
} | ||
|
||
var cl items.Checklist | ||
cl, err = checklistNotes[x].Content.ToCheckList() | ||
if err != nil { | ||
return items.Checklists{}, err | ||
} | ||
|
||
cl.UUID = checklistNotes[x].UUID | ||
cl.UpdatedAt, err = time.Parse(timeLayout, checklistNotes[x].UpdatedAt) | ||
if err != nil { | ||
return items.Checklists{}, err | ||
} | ||
|
||
cl.Duplicates = duplicatesMap[checklistNotes[x].UUID] | ||
|
||
checklists = append(checklists, cl) | ||
} | ||
|
||
return checklists, nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/jonhadfield/gosn-v2/cache" | ||
sncli "github.com/jonhadfield/sn-cli" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func cmdChecklist() *cli.Command { | ||
return &cli.Command{ | ||
Name: "checklist", | ||
Usage: "manage checklists", | ||
BashComplete: func(c *cli.Context) { | ||
addTasks := []string{"list"} | ||
if c.NArg() > 0 { | ||
return | ||
} | ||
for _, t := range addTasks { | ||
fmt.Println(t) | ||
} | ||
}, | ||
Subcommands: []*cli.Command{ | ||
{ | ||
Name: "list", | ||
Usage: "list checklists", | ||
Action: func(c *cli.Context) error { | ||
var opts configOptsOutput | ||
opts, err := getOpts(c) | ||
if err != nil { | ||
return err | ||
} | ||
var sess cache.Session | ||
sess, _, err = cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug) | ||
if !sess.SchemaValidation { | ||
panic("schema validation is false") | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
sess.CacheDBPath, err = cache.GenCacheDBPath(sess, opts.cacheDBDir, snAppName) | ||
if err != nil { | ||
return err | ||
} | ||
listChecklistsConfig := sncli.ListChecklistsInput{ | ||
Session: &sess, | ||
} | ||
|
||
return listChecklistsConfig.Run() | ||
}, | ||
}, | ||
// { | ||
// Name: "show", | ||
// Usage: "show checklist", | ||
// BashComplete: func(c *cli.Context) { | ||
// if c.NArg() > 0 { | ||
// return | ||
// } | ||
// fmt.Println("--title") | ||
// }, | ||
// Flags: []cli.Flag{ | ||
// &cli.StringFlag{ | ||
// Name: "title", | ||
// Usage: "new tag title (separate multiple with commas)", | ||
// }, | ||
// }, | ||
// Action: func(c *cli.Context) error { | ||
// var opts configOptsOutput | ||
// opts, err := getOpts(c) | ||
// if err != nil { | ||
// return err | ||
// } | ||
// // useStdOut = opts.useStdOut | ||
// err = processAddTags(c, opts) | ||
// | ||
// return err | ||
// }, | ||
// }, | ||
}, | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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