Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to create nested Tags via parents #49

Merged
merged 11 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ coverage.txt

# local build
.local_dist
bin/*
!bin/.gitkeep

# github release build
dist

Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ clean:
rm -rf ./dist

setup:
go get -u github.com/alecthomas/gometalinter
go get -u golang.org/x/tools/cmd/cover
gometalinter --install --update
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
go install -v github.com/go-critic/go-critic/cmd/gocritic@latest

test:
echo 'mode: atomic' > coverage.txt && go list ./... | xargs -n1 -I{} sh -c 'go test -v -failfast -p 1 -parallel 1 -timeout=600s -covermode=atomic -coverprofile=coverage.tmp {} && tail -n +2 coverage.tmp >> coverage.txt' && rm coverage.tmp
Expand Down
10 changes: 9 additions & 1 deletion cmd/sncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,21 @@ func startCLI(args []string) (msg string, useStdOut bool, err error) {
if c.NArg() > 0 {
return
}
fmt.Println("--title")
fmt.Println("--title", "--parent", "--parent-uuid")
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "title",
Usage: "new tag title (separate multiple with commas)",
},
cli.StringFlag{
Name: "parent",
Usage: "parent tag title to make a sub-tag of",
},
cli.StringFlag{
Name: "parent-uuid",
Usage: "parent tag uuid to make a sub-tag of",
},
},
Action: func(c *cli.Context) error {
var opts configOptsOutput
Expand Down
10 changes: 6 additions & 4 deletions cmd/sncli/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func processGetTags(c *cli.Context, opts configOptsOutput) (msg string, err erro
inTitle := strings.TrimSpace(c.String("title"))
inUUID := strings.TrimSpace(c.String("uuid"))

var matchAny bool
matchAny := true
if c.Bool("match-all") {
matchAny = false
}
Expand Down Expand Up @@ -397,9 +397,11 @@ func processAddTags(c *cli.Context, opts configOptsOutput) (msg string, err erro
// prepare input
tags := sncli.CommaSplit(tagInput)
addTagInput := sncli.AddTagsInput{
Session: &session,
Tags: tags,
Debug: opts.debug,
Session: &session,
Tags: tags,
Parent: c.String("parent"),
ParentUUID: c.String("parent-uuid"),
Debug: opts.debug,
}

// attempt to add tags
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/fatih/color v1.16.0
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/jonhadfield/gosn-v2 v0.0.0-20231211223627-abb88b737146
github.com/jonhadfield/gosn-v2 v0.0.0-20231212210044-96a40e9cc8cc
github.com/ryanuber/columnize v2.1.2+incompatible
github.com/spf13/viper v1.18.1
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jonhadfield/gosn-v2 v0.0.0-20231211223627-abb88b737146 h1:JPl0PwwOe06qDLz3g98RimTAYbJ0Wq7SYNLgtxdiH5Q=
github.com/jonhadfield/gosn-v2 v0.0.0-20231211223627-abb88b737146/go.mod h1:4/EdV1iRAKpaa4ZcF1TuniwRaajeWIMyMEWkrVnq/k8=
github.com/jonhadfield/gosn-v2 v0.0.0-20231212210044-96a40e9cc8cc h1:f3RUJ+JRyS1q6Hpm1e36xC3grCec0KRBvC2fuk0+pvo=
github.com/jonhadfield/gosn-v2 v0.0.0-20231212210044-96a40e9cc8cc/go.mod h1:4/EdV1iRAKpaa4ZcF1TuniwRaajeWIMyMEWkrVnq/k8=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down
13 changes: 7 additions & 6 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/gob"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -132,7 +131,7 @@ type EncryptedItemsFile struct {
}

func readJSON(filePath string) (items items.EncryptedItems, err error) {
file, err := ioutil.ReadFile(filePath)
file, err := os.ReadFile(filePath)
if err != nil {
err = fmt.Errorf("%w failed to open: %s", err, filePath)
return
Expand All @@ -154,8 +153,9 @@ func ItemRefsToYaml(irs []items.ItemReference) []ItemReferenceYAML {

for _, ref := range irs {
iRef := ItemReferenceYAML{
UUID: ref.UUID,
ContentType: ref.ContentType,
UUID: ref.UUID,
ContentType: ref.ContentType,
ReferenceType: ref.ReferenceType,
}
iRefs = append(iRefs, iRef)
}
Expand All @@ -168,8 +168,9 @@ func ItemRefsToJSON(irs []items.ItemReference) []ItemReferenceJSON {

for _, ref := range irs {
iRef := ItemReferenceJSON{
UUID: ref.UUID,
ContentType: ref.ContentType,
UUID: ref.UUID,
ContentType: ref.ContentType,
ReferenceType: ref.ReferenceType,
}
iRefs = append(iRefs, iRef)
}
Expand Down
20 changes: 12 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ const (
)

type ItemReferenceYAML struct {
UUID string `yaml:"uuid"`
ContentType string `yaml:"content_type"`
UUID string `yaml:"uuid"`
ContentType string `yaml:"content_type"`
ReferenceType string `yaml:"reference_type",omitempty`
}

type ItemReferenceJSON struct {
UUID string `json:"uuid"`
ContentType string `json:"content_type"`
UUID string `json:"uuid"`
ContentType string `json:"content_type"`
ReferenceType string `json:"reference_type",omitempty`
}

type OrgStandardNotesSNDetailJSON struct {
Expand Down Expand Up @@ -151,10 +153,12 @@ type TagItemsConfig struct {
}

type AddTagsInput struct {
Session *cache.Session
Tags []string
Debug bool
Replace bool
Session *cache.Session
Tags []string
Parent string
ParentUUID string
Debug bool
Replace bool
}

type AddTagsOutput struct {
Expand Down
39 changes: 32 additions & 7 deletions tag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sncli

import (
"errors"
"strings"

"github.com/jonhadfield/gosn-v2/cache"
Expand Down Expand Up @@ -181,9 +182,11 @@ func (i *AddTagsInput) Run() (output AddTagsOutput, err error) {
}()

ati := addTagsInput{
tagTitles: i.Tags,
session: i.Session,
replace: i.Replace,
tagTitles: i.Tags,
parent: i.Parent,
parentUUID: i.ParentUUID,
session: i.Session,
replace: i.Replace,
}

var ato addTagsOutput
Expand Down Expand Up @@ -346,9 +349,11 @@ func deleteTags(session *cache.Session, tagTitles []string, tagUUIDs []string) (
}

type addTagsInput struct {
session *cache.Session
tagTitles []string
replace bool
session *cache.Session
tagTitles []string
parent string
parentUUID string
replace bool
}

type addTagsOutput struct {
Expand Down Expand Up @@ -399,6 +404,7 @@ func addTags(ati addTagsInput) (ato addTagsOutput, err error) {
gItems.Filter(addFilter)

var allTags items.Tags
var parentRef items.ItemReferences

for _, item := range gItems {
if item.IsDeleted() {
Expand All @@ -408,9 +414,28 @@ func addTags(ati addTagsInput) (ato addTagsOutput, err error) {
if item.GetContentType() == "Tag" {
tag := item.(*items.Tag)
allTags = append(allTags, *tag)
if tag.Content.GetTitle() == ati.parent || tag.GetUUID() == ati.parentUUID {
if parentRef != nil {
return ato, errors.New("multiple parent tags found, specify by UUID")
}
itemRef := items.ItemReference{
UUID: tag.GetUUID(),
ContentType: "Tag",
ReferenceType: "TagToParentTag",
}
parentRef = items.ItemReferences{itemRef}
}
}
}

if ati.parent != "" && len(parentRef) == 0 {
return ato, errors.New("parent tag not found by title")
}

if ati.parentUUID != "" && len(parentRef) == 0 {
return ato, errors.New("parent tag not found by UUID")
}

var tagsToAdd items.Tags

for _, tag := range ati.tagTitles {
Expand All @@ -419,7 +444,7 @@ func addTags(ati addTagsInput) (ato addTagsOutput, err error) {
continue
}

newTag, _ := items.NewTag(tag, nil)
newTag, _ := items.NewTag(tag, parentRef)

tagsToAdd = append(tagsToAdd, newTag)
ato.added = append(ato.added, tag)
Expand Down
35 changes: 35 additions & 0 deletions tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,41 @@ func TestAddDeleteTagByTitle(t *testing.T) {
require.NoError(t, err)
}

func TestAddTagWithParent(t *testing.T) {
testDelay()

addTagConfigParent := AddTagsInput{
Session: testSession,
Tags: []string{"TestTagParent"},
}

ato, err := addTagConfigParent.Run()
require.NoError(t, err)
require.Contains(t, ato.Added, "TestTagParent")
require.Empty(t, ato.Existing)

addTagConfigChild := AddTagsInput{
Session: testSession,
Tags: []string{"TestTagChild"},
Parent: "TestTagParent",
}

ato, err = addTagConfigChild.Run()
require.NoError(t, err)
require.Contains(t, ato.Added, "TestTagChild")
require.Empty(t, ato.Existing)

deleteTagConfig := DeleteTagConfig{
Session: testSession,
TagTitles: []string{"TestTagParent", "TestTagChild"},
}

var noDeleted int
noDeleted, err = deleteTagConfig.Run()
require.Equal(t, 2, noDeleted)
require.NoError(t, err)
}

func TestGetTag(t *testing.T) {
testDelay()

Expand Down
Loading