-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
68 lines (55 loc) · 1.54 KB
/
import.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package command
import (
"context"
"errors"
"io"
"os"
"github.com/loghinalexandru/anchor/internal/command/util/parser"
"github.com/peterbourgon/ff/v4"
"github.com/virtualtam/netscape-go/v2"
)
const (
importName = "import"
importUsage = "anchor import [PATH]"
importShortHelp = "import bookmarks from a browser exported file"
importLongHelp = ` Imports all the bookmarks from a "NETSCAPE-Bookmark-file-1" file format setting up the appropriate labels
based on the folder structure that was previously in the browser.
On import, it formats all the invalid folder names because they will be reused as labels inside anchor.
Valid label names contain only lower case alphanumeric characters and hyphen.
`
)
var (
ErrInvalidImportFile = errors.New("invalid import file")
)
type importCmd struct{}
func (imp *importCmd) manifest(parent *ff.FlagSet) *ff.Command {
return &ff.Command{
Name: importName,
Usage: importUsage,
ShortHelp: importShortHelp,
LongHelp: importLongHelp,
Flags: ff.NewFlagSet("import").SetParent(parent),
Exec: func(ctx context.Context, args []string) error {
return imp.handle(ctx.(appContext), args)
},
}
}
func (*importCmd) handle(ctx appContext, args []string) error {
if len(args) == 0 {
return ErrInvalidImportFile
}
fh, err := os.Open(args[0])
if err != nil {
return err
}
content, err := io.ReadAll(fh)
if err != nil {
return err
}
doc, _ := netscape.Unmarshal(content)
err = parser.TraverseNode(ctx.path, nil, doc.Root)
if err != nil {
return err
}
return nil
}