Skip to content

Commit fa24355

Browse files
committed
multiple inputs
1 parent 0588686 commit fa24355

File tree

7 files changed

+183
-78
lines changed

7 files changed

+183
-78
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"mode": "debug",
1212
"program": "${workspaceFolder}/src",
13-
"args": ["build", "-i", "${workspaceFolder}/example-files/start.yaml", "-o", "${workspaceFolder}/output2.html"]
13+
"args": ["build", "-i", "${workspaceFolder}/example-files/links.yaml", "-i", "${workspaceFolder}/example-files/folders.yaml", "-o", "${workspaceFolder}/output.html"]
1414
}
1515
]
1616
}
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
links:
2-
- id: link-1
3-
name: link-1
4-
url: https://google.com/
5-
- id: link-2
6-
name: link-2
7-
url: https://google.com/
8-
- id: link-3
9-
name: link-2
10-
url: https://google.com/
11-
- id: art
12-
name: some art
13-
url: https://art.com/
14-
15-
161
folders:
172
general:
183
by name:

example-files/links.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
links:
2+
- id: link-1
3+
name: link-1
4+
url: https://google.com/
5+
- id: link-2
6+
name: link-2
7+
url: https://google.com/
8+
- id: link-3
9+
name: link-2
10+
url: https://google.com/
11+
- id: art
12+
name: some art
13+
url: https://art.com/

src/actions/reader.go

Lines changed: 85 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"reflect"
88
"strings"
99

10-
"github.com/yosssi/gohtml"
1110
"gopkg.in/yaml.v3"
1211
)
1312

@@ -22,24 +21,24 @@ type Link struct {
2221
Url string
2322
}
2423

25-
var globalLinks *[]Link
24+
var globalLinks []Link
2625

27-
func BuildStructure(inputDirectory string, outputFile string) {
26+
func BuildStructure(inputDirectory []string, outputFile string) {
2827

2928
links, folders, _ := readInput(inputDirectory)
3029

3130
globalLinks = links
3231

33-
log.Println(links)
34-
log.Println(folders)
32+
//log.Println(links)
33+
//log.Println(folders)
3534

3635
bookmarkFile := addHeader()
3736

3837
bookmarkFile = fmt.Sprintf("%s%s", bookmarkFile, filesToPaths(folders))
3938

4039
bookmarkFile += addFooter()
41-
log.Println("paths")
42-
log.Println(gohtml.Format(bookmarkFile))
40+
//log.Println("paths")
41+
//log.Println(gohtml.Format(bookmarkFile))
4342

4443
writefile := []byte(bookmarkFile)
4544
err := os.WriteFile(outputFile, writefile, 0644)
@@ -48,87 +47,98 @@ func BuildStructure(inputDirectory string, outputFile string) {
4847
}
4948
}
5049

51-
func readInput(inputDirectory string) (*[]Link, map[string]interface{}, error) {
50+
func readInput(inputDirectory []string) ([]Link, map[string]interface{}, error) {
5251

53-
var inputFile InputFile
52+
/////// MERGE 1 //////////
5453

55-
if strings.HasSuffix(inputDirectory, ".yaml") {
56-
data, err := os.ReadFile(inputDirectory)
57-
if err != nil {
58-
log.Println("Bad File Read")
59-
panic(err)
60-
}
61-
err = yaml.Unmarshal(data, &inputFile)
62-
if err != nil {
63-
log.Println("Bad Marshal")
64-
panic(err)
54+
var inputFiles []InputFile
55+
56+
for _, inputFilePath := range inputDirectory {
57+
if strings.HasSuffix(inputFilePath, ".yaml") {
58+
data, err := os.ReadFile(inputFilePath)
59+
if err != nil {
60+
log.Println("Bad File Read")
61+
panic(err)
62+
}
63+
var inputFile InputFile
64+
err = yaml.Unmarshal(data, &inputFile)
65+
if err != nil {
66+
log.Println("Bad Marshal")
67+
panic(err)
68+
}
69+
70+
inputFiles = append(inputFiles, inputFile)
6571
}
6672
}
6773

74+
inputFile := merge(inputFiles)
6875
// log.Println(inputFile.Folders)
69-
return &inputFile.Links, inputFile.Folders, nil
76+
//return &inputFile.Links, inputFile.Folders, nil
77+
78+
// junk := InputFile{Links: []Link{}, Folders: nil}
79+
return removeDuplicateLinks(inputFile.Links), inputFile.Folders, nil
80+
}
81+
82+
func merge(input []InputFile) InputFile {
83+
var response InputFile
84+
85+
//verify only one of each file
86+
87+
oneFolders := false
88+
89+
for _, inputItem := range input {
90+
if inputItem.Folders != nil && !oneFolders {
91+
response.Folders = inputItem.Folders
92+
oneFolders = true
93+
}
94+
95+
if inputItem.Links != nil {
96+
response.Links = append(response.Links, inputItem.Links...)
97+
}
98+
}
99+
100+
return response
70101
}
71102

72103
func filesToPaths(folders map[string]interface{}) string {
73104
var response string
74105
// currentString = fmt.Sprintf("%s/%s", currentString, key)
75106
for key, dir := range folders {
76-
log.Println(reflect.TypeOf(dir))
107+
//log.Println(reflect.TypeOf(dir))
77108

78109
// log.Printf("current: %s", currentString)
79110
// log.Printf("key: %s", key)
80-
log.Println(reflect.TypeOf(dir))
111+
//log.Println(reflect.TypeOf(dir))
81112

82113
if key == "bookmarks" {
83-
log.Println(dir)
114+
//log.Println(dir)
84115
response += generateBookmarks(dir.([]interface{}))
85-
log.Println("It is a link")
116+
//log.Println("It is a link")
86117
} else if key == "name" {
87-
log.Println("help")
118+
//log.Println("help")
88119
//response += fmt.Sprintf("%s/%s", currentString, key)
89120
} else if _, ok := dir.([]interface{}); ok {
90-
response += fmt.Sprintf("<DT><H3 ADD_DATE=\"1677903092\" LAST_MODIFIED=\"1677903150\">%s</H3>", key)
121+
response += fmt.Sprintf("<DT><H3 ADD_DATE=\"1677903092\" LAST_MODIFIED=\"1677903150\">%s</H3>\n", key)
91122

92-
response += `
93-
<DL><p>
94-
`
123+
response += "<DL><p>\n"
95124
// log.Println("file")
96125
// log.Println(dir)
97126
//currentString = fmt.Sprintf("%s/%s", currentString, dir)
98127

99128
response += generateBookmarks(dir.([]interface{}))
100-
response += `
101-
</DL><p>
102-
`
129+
response += "</DL><p>\n"
103130
} else if _, ok := dir.(map[string]interface{}); ok {
104-
response += fmt.Sprintf("<DT><H3 ADD_DATE=\"1677903092\" LAST_MODIFIED=\"1677903150\">%s</H3>", key)
131+
response += fmt.Sprintf("<DT><H3 ADD_DATE=\"1677903092\" LAST_MODIFIED=\"1677903150\">%s</H3>\n", key)
105132

106-
response += `
107-
<DL><p>
108-
`
133+
response += "<DL><p>\n"
109134
response += filesToPaths(dir.(map[string]interface{}))
110-
response += `
111-
</DL><p>
112-
`
135+
response += "</DL><p>\n"
113136
}
114137
}
115138

116139
return response
117140
}
118141

119-
func individualPaths(dir []interface{}) string {
120-
var response string
121-
122-
for _, data := range dir {
123-
log.Println(reflect.TypeOf(data))
124-
name, url := lookupLinkName(data.(string))
125-
response += fmt.Sprintf("<DT><A HREF=\"%s\" ADD_DATE=\"1677903092\" LAST_MODIFIED=\"1677903150\">%s</A>\n", url, name)
126-
// response += fmt.Sprintf("%s/%s", key, data)
127-
}
128-
129-
return response
130-
}
131-
132142
func addHeader() string {
133143
return `<!DOCTYPE NETSCAPE-Bookmark-file-1>
134144
<!-- This is an automatically generated file.
@@ -145,10 +155,7 @@ func addHeader() string {
145155
}
146156

147157
func addFooter() string {
148-
return `
149-
</DL><p>
150-
</DL><p>
151-
`
158+
return "</DL><p>\n</DL><p>\n"
152159
}
153160

154161
func generateBookmarks(filesToBookmark []interface{}) string {
@@ -177,7 +184,7 @@ func generateBookmarks(filesToBookmark []interface{}) string {
177184
}
178185
}
179186

180-
response += fmt.Sprintf("<DT><A HREF=\"%s\" ADD_DATE=\"1677903092\" LAST_MODIFIED=\"1677903150\">%s</A>", url, name)
187+
response += fmt.Sprintf("<DT><A HREF=\"%s\" ADD_DATE=\"1677903092\" LAST_MODIFIED=\"1677903150\">%s</A>\n", url, name)
181188

182189
}
183190
}
@@ -188,10 +195,30 @@ func generateBookmarks(filesToBookmark []interface{}) string {
188195
func lookupLinkName(linkName string) (string, string) {
189196
// log.Println("globalLinks", globalLinks)
190197

191-
for _, data := range *globalLinks {
198+
for _, data := range globalLinks {
192199
if linkName == data.Id {
193200
return data.Name, data.Url
194201
}
195202
}
196203
return "", ""
197204
}
205+
206+
func removeDuplicateLinks(items []Link) []Link {
207+
var outlinks []Link
208+
209+
for _, link := range items {
210+
found := false
211+
for _, outlink := range outlinks {
212+
if link.Id == outlink.Id {
213+
found = true
214+
log.Fatalln("you have a duplicate link: ", link.Id)
215+
}
216+
}
217+
if !found {
218+
outlinks = append(outlinks, link)
219+
}
220+
}
221+
222+
return outlinks
223+
224+
}

src/cmd/build.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3-
43
*/
54
package cmd
65

@@ -10,23 +9,24 @@ import (
109
"github.com/spf13/cobra"
1110
)
1211

13-
var Input, Output string
12+
var Inputs []string
13+
var Output string
1414

1515
// buildCmd represents the build command
1616
var buildCmd = &cobra.Command{
1717
Use: "build",
1818
Short: "A brief description of your command",
1919
Long: `Configure a bunch of links and a folder structure and this will build out the bookmarks for importing`,
2020
Run: func(cmd *cobra.Command, args []string) {
21-
actions.BuildStructure(Input, Output)
21+
actions.BuildStructure(Inputs, Output)
2222

2323
},
2424
}
2525

2626
func init() {
2727
rootCmd.AddCommand(buildCmd)
2828

29-
buildCmd.Flags().StringVarP(&Input, "inputDirectory", "i", ".", "file or directory where input yaml is.")
29+
buildCmd.Flags().StringArrayVarP(&Inputs, "inputDirectory", "i", []string{"."}, "file or directory where input yaml is.")
3030
buildCmd.Flags().StringVarP(&Output, "outputDirectory", "o", "output.html", "root of output directories.")
3131
// Here you will define your flags and configuration settings.
3232

src/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@ require (
99
)
1010

1111
require (
12+
github.com/BurntSushi/toml v1.2.1 // indirect
1213
github.com/inconshreveable/mousetrap v1.0.1 // indirect
1314
github.com/spf13/pflag v1.0.5 // indirect
15+
go.uber.org/atomic v1.10.0 // indirect
16+
go.uber.org/config v1.4.0 // indirect
17+
go.uber.org/multierr v1.9.0 // indirect
18+
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee // indirect
19+
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
20+
golang.org/x/mod v0.9.0 // indirect
1421
golang.org/x/net v0.7.0 // indirect
22+
golang.org/x/sys v0.6.0 // indirect
23+
golang.org/x/text v0.8.0 // indirect
24+
golang.org/x/tools v0.6.0 // indirect
25+
gopkg.in/yaml.v2 v2.4.0 // indirect
26+
honnef.co/go/tools v0.4.2 // indirect
1527
)

0 commit comments

Comments
 (0)