This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
1069 lines (919 loc) · 21.1 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// +build go1.5
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"go/ast"
"go/doc"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/golang-china/godocu/docu"
)
const mode = ast.FilterFuncDuplicates |
ast.FilterUnassociatedComments | ast.FilterImportDuplicates
const usage = `Usage:
godocu command [arguments] source [target]
The commands are:
diff compare the source and target, all difference output
first compare the source and target, the first difference output
tree compare different directory structure of the source and target
code prints a formatted string to target as Go source code
tmpl prints documentation from template
list generate godocu style documents list
merge merge source doc to target
replace replace the target untranslated section in source translated section
The source are:
package import path or absolute path
the path to a Go source file
The target are:
the directory as an absolute base path for compare or prints
The arguments are:
-file string
template file for tmpl
-gopath string
specifies GOPATH (default $GOPATH)
-goroot string
specifies GOROOT (default $GOROOT)
-lang string
the lang pattern for the output file, form like en or zh_CN
-p string
package filtering, "package"|"main"|"test" (default "package")
-u
show unexported symbols as well as exported
`
func flagUsage(err string) {
fmt.Fprintln(os.Stderr, usage)
if err != "" {
log.Fatal(err)
}
os.Exit(2)
}
func flagParse() (command, source, target, lib, lang, file string, u bool) {
var gopath string
flag.StringVar(&file, "file", "", "")
flag.StringVar(&docu.GOROOT, "goroot", docu.GOROOT, "")
flag.StringVar(&gopath, "gopath", os.Getenv("GOPATH"), "")
flag.StringVar(&lang, "lang", "", "")
flag.StringVar(&lib, "p", "package", "")
flag.BoolVar(&u, "u", false, "")
if len(os.Args) < 3 {
flagUsage("")
}
args := make([]string, len(os.Args[2:]))
j := 0
for i := 2; i < len(os.Args); i++ {
if os.Args[i][0] == '-' && os.Args[i] != "--" {
args[j] = os.Args[i]
j++
}
}
for i := 2; i < len(os.Args); i++ {
if os.Args[i][0] != '-' || os.Args[i] == "--" {
args[j] = os.Args[i]
j++
}
}
err := flag.CommandLine.Parse(args)
if err != nil {
flagUsage(err.Error())
}
const libs = "package test main "
if pos := strings.Index(libs, lib); pos == -1 || libs[pos+len(lib)] != ' ' {
flagUsage("-p must be one of package,test,main. but got" + lib)
}
args = flag.Args()
if len(args) == 0 || len(args) > 2 {
flagUsage("")
}
command = os.Args[1]
source = args[0]
if len(args) == 2 {
target = args[1]
}
docu.GOROOT, err = filepath.Abs(docu.GOROOT)
if err != nil {
flagUsage("invalid goroot: " + err.Error())
}
if gopath != os.Getenv("GOPATH") {
docu.GOPATHS = filepath.SplitList(gopath)
for i, path := range docu.GOPATHS {
docu.GOPATHS[i], err = filepath.Abs(path)
if err != nil {
flagUsage("invalid gopath: " + err.Error())
}
}
}
lang = docu.LangNormal(lang)
return
}
// 多文档输出分割线
var sp = "\n\n" + strings.Repeat("/", 80) + "\n\n"
func skipOSArch(f func(string) bool) func(string) bool {
return func(name string) bool {
if !f(name) {
return false
}
if docu.IsNormalName(name) {
return true
}
goos, goarch, _ := docu.OSArchTest(name)
return (goos == "" || goos == "linux") && (goarch == "" || goarch == "amd64")
}
}
func genNameFilter(lib, lang string) func(string) bool {
if lang == "" {
switch lib {
case "package":
return skipOSArch(docu.PackageFilter)
case "test":
return skipOSArch(docu.TestFilter)
case "main":
return skipOSArch(docu.MainFilter)
}
panic("BUG")
}
switch lib {
case "package":
return skipOSArch(docu.GenNameFilter("doc_" + lang + ".go"))
case "test":
return skipOSArch(docu.GenNameFilter("test_" + lang + ".go"))
case "main":
return skipOSArch(docu.GenNameFilter("main_" + lang + ".go"))
}
panic("BUG")
}
func genFileName(lib, lang, ext string) string {
if lang == "" {
return ""
}
switch lib {
case "package":
return "doc_" + lang + ext
case "test":
return "test_" + lang + ext
case "main":
return "main_" + lang + ext
}
panic("BUG")
}
func createFile(path, name string) (file *os.File, err error) {
if name == "" || path == "" {
return os.Stdout, nil
}
if err = os.MkdirAll(path, 0777); err == nil {
file, err = os.OpenFile(filepath.Join(path, name),
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
}
return
}
func main() {
const cmds = "code tmpl list diff first tree merge replace "
var err error
var info os.FileInfo
command, source, target, lib, lang, file, u := flagParse()
pos := strings.Index(cmds, command)
if pos == -1 || cmds[pos+len(command)] != ' ' || target == "" && pos > 14 {
fmt.Fprintln(os.Stderr, usage)
log.Fatal("invalid command or target")
}
sub := strings.HasSuffix(source, "...")
if sub {
source = source[:len(source)-3]
}
if source == "" {
source = docu.GOROOT + docu.SrcElem[:4]
} else {
source = docu.Abs(source)
}
if info, err = os.Stat(source); err != nil {
flagUsage(err.Error())
} else if command == "tree" && !info.IsDir() {
flagUsage("source must be existing directory")
}
// 计算导入路径的偏移量
offset := posForImport(source)
if offset == -1 {
flagUsage("invalid source: " + source)
} else if target == "--" {
// 同目录输出
target = source[:offset]
} else {
target = docu.Abs(target)
}
if command == "tree" {
sub = true
if info, err = os.Stat(target); err != nil || !info.IsDir() {
flagUsage("target must be existing directory")
}
}
ch := make(chan interface{})
go walkPath(ch, sub, source)
switch command {
case "code":
err = codeMode(ch, offset, target, lib, lang, u)
case "tree":
// 对比目录结构
var d1 bool
prefix := fmt.Sprintf("source: %s\ntarget: %s\n\nsource target path\n",
source, target)
d1, err = treeMode(prefix, " path none ", " path file ", ch, source, target)
if err != nil {
break
}
close(ch)
if d1 {
prefix = ""
}
if offset < len(source) {
target = filepath.Join(target, source[offset:])
source = source[:offset]
}
ch = make(chan interface{})
go walkPath(ch, sub, target)
_, err = treeMode(prefix, " none path ", " file path ", ch, target, source)
case "first", "diff":
err = diffMode(command, ch, offset, target, lib, lang, u)
case "merge":
err = mergeMode(ch, offset, target, lib, lang)
case "replace":
err = replaceMode(ch, offset, target, lib, lang)
case "list":
err = listMode(ch, offset, target, lib, lang)
case "tmpl":
tpl := template.New("Godocu").Funcs(docu.FuncsMap)
if file != "" {
tpl, err = tpl.ParseFiles(file)
} else {
tpl, err = tpl.Parse(docu.DefaultTemplate)
}
if err != nil {
<-ch
break
}
err = tmplMode(tpl, ch, offset, target, lib, lang, u)
}
close(ch)
if err != nil {
log.Fatal(err)
}
}
// 模板
func tmplMode(tmpl *template.Template, ch chan interface{},
offset int, target, lib, lang string, u bool) (err error) {
var buf bytes.Buffer
var ok bool
var key, source, dst string
var paths string
var output *os.File
out := false
du := docu.NewData()
du.Docu = docu.New()
du.Docu.Filter = genNameFilter(lib, "")
tu := docu.New()
if target != "" {
tu.Filter = genNameFilter(lib, lang)
}
for i := <-ch; i != nil; i = <-ch {
if err, ok = i.(error); !ok {
source = i.(string)
paths, err = du.Parse(source, nil)
}
if err != nil {
break
}
if len(paths) == 0 {
ch <- nil
continue
}
key = paths
if !u {
if target != "" {
// 计算目标路径, 第一个可能是单文件
if dst == "" && strings.HasSuffix(source, ".go") {
source = filepath.Dir(source)
}
dst = filepath.Join(target, source[offset:])
// 以目标过滤源
paths, err = tu.Parse(dst, nil)
if os.IsNotExist(err) {
err = nil
}
if err != nil {
break
}
dis := tu.MergePackageFiles(key)
if dis != nil && paths == key {
du.SetFilter(docu.SortDecl(dis.Decls).Filter)
} else {
du.SetFilter(docu.ExportedFileFilter)
}
} else {
du.SetFilter(docu.ExportedFileFilter)
}
}
buf.Truncate(0)
du.Key = key
if err = tmpl.Execute(&buf, du); err != nil {
break
}
if du.Ext == "" {
continue
}
output, err = createFile(dst, genFileName(lib, lang, du.Ext))
if err != nil {
break
}
if out && target == "" {
_, err = os.Stdout.WriteString(sp)
}
out = true
if err == nil {
_, err = output.Write(buf.Bytes())
}
if output != os.Stdout {
output.Close()
}
if err != nil {
break
}
ch <- nil
}
return
}
// walkPath 通道类型约定:
// nil 结束
// string 待处理绝对路径
// error 处理错误
func walkPath(ch chan interface{}, sub bool, source string) {
if strings.HasSuffix(source, ".go") {
ch <- source
<-ch
ch <- nil
return
}
docu.WalkPath(source, func(path string, _ os.FileInfo, err error) error {
if err == nil {
ch <- path
} else {
ch <- err
}
i := <-ch
if i != nil || !sub || err != nil {
return io.EOF
}
return nil
})
ch <- nil
}
func codeMode(ch chan interface{},
offset int, target, lib, lang string, u bool) (err error) {
var ok bool
var key, source, dst string
var paths string
output := os.Stdout
out := false
du := docu.New()
du.Filter = genNameFilter(lib, "")
tu := docu.New()
if target != "" {
tu.Filter = genNameFilter(lib, lang)
}
fname := genFileName(lib, lang, ".go")
for i := <-ch; i != nil; i = <-ch {
if err, ok = i.(error); !ok {
source = i.(string)
paths, err = du.Parse(source, nil)
}
if docu.IsMultiplePkgError(err) {
err = nil
ch <- nil
continue
}
if err != nil {
break
}
if len(paths) == 0 {
ch <- nil
continue
}
key = paths
file := du.MergePackageFiles(key)
file.Unresolved = nil
if target != "" {
// 计算目标路径, 第一个可能是单文件
if dst == "" && strings.HasSuffix(source, ".go") {
source = filepath.Dir(source)
}
dst = filepath.Join(target, source[offset:])
}
if !u {
if target != "" {
// 以目标过滤源
paths, err = tu.Parse(dst, nil)
if os.IsNotExist(err) {
err = nil
}
if err != nil {
break
}
dis := tu.MergePackageFiles(key)
if dis != nil && paths == key {
docu.SortDecl(dis.Decls).Filter(file)
} else {
docu.ExportedFileFilter(file)
}
// 自动提取第一个 lang, 只是为了过滤
if dis != nil && lang == "" {
lang = tu.NormalLang(key)
tu.Filter = genNameFilter(lib, tu.NormalLang(key))
lang = "."
}
} else {
docu.ExportedFileFilter(file)
}
}
if target != "" && lang != "" && lang != "." {
output, err = createFile(dst, fname)
}
if err != nil {
break
}
if out && target == "" {
_, err = os.Stdout.WriteString(sp)
}
out = true
if err == nil {
err = docu.Fprint(output, file)
}
if output != os.Stdout {
output.Close()
}
if err != nil {
break
}
ch <- nil
}
return
}
func diffMode(command string, ch chan interface{},
offset int, target, lib, lang string, u bool) (err error) {
var ok, diff bool
var key, source string
var paths string
var output *os.File
fileDiff := docu.FirstDiff
if command == "diff" {
fileDiff = docu.Diff
}
du, tu := docu.New(), docu.New()
du.Filter = genNameFilter(lib, "")
tu.Filter = genNameFilter(lib, lang)
for i := <-ch; i != nil; i = <-ch {
if err, ok = i.(error); !ok {
source = i.(string)
paths, err = du.Parse(source, nil)
}
if docu.IsMultiplePkgError(err) {
err = nil
ch <- nil
continue
}
if err != nil {
break
}
if len(paths) == 0 {
ch <- nil
continue
}
// 只对比相同的包. 不能有错.
key = paths
if strings.HasSuffix(source, ".go") {
source = filepath.Dir(source)
}
paths, err = tu.Parse(filepath.Join(target, source[offset:]), nil)
if os.IsNotExist(err) {
err = nil
}
if err != nil {
break
}
if len(paths) == 0 {
ch <- nil
continue
}
diff, err = docu.TextDiff(output, "package "+key, "package "+paths)
if err != nil {
break
}
if diff {
ch <- nil
io.WriteString(output, sp)
continue
}
src, dis := du.MergePackageFiles(key), tu.MergePackageFiles(key)
// 自动提取第一个 lang, 只是为了过滤
if dis != nil && lang == "" {
lang = tu.NormalLang(key)
tu.Filter = genNameFilter(lib, lang)
if lang == "" {
lang = "."
}
}
if lang != "." {
docu.SortDecl(dis.Decls).Filter(src)
} else if !u {
docu.ExportedFileFilter(src)
docu.ExportedFileFilter(dis)
}
diff, err = fileDiff(output, src, dis)
if diff && err == nil {
_, err = io.WriteString(output, "FROM: package "+key)
}
if err != nil {
break
}
ch <- nil
}
return
}
// posForImport 计算 import paths 开始的偏移量
func posForImport(s string) (pos int) {
if strings.HasSuffix(s, ".go") {
s = filepath.Dir(s)
}
if strings.HasSuffix(s, docu.SrcElem[:4]) {
return len(s) + 1
}
pos = strings.Index(s, docu.SrcElem)
if pos != -1 {
pos += len(docu.SrcElem)
return
}
for _, wh := range docu.Warehouse {
pos = strings.Index(s, wh.Host)
if pos == -1 {
continue
}
if s[pos-1] == os.PathSeparator && s[pos+len(wh.Host)] == os.PathSeparator {
return pos
}
}
return -1
}
func treeMode(prefix, prenone, prefile string, ch chan interface{}, source, target string) (diff bool, err error) {
var fi os.FileInfo
pos := posForImport(source)
if pos == -1 {
<-ch
err = errors.New("invalid path: " + source)
return
}
output := os.Stdout
for i := <-ch; i != nil; i = <-ch {
err, _ = i.(error)
if err != nil {
break
}
source = i.(string)
source = source[pos:]
fi, err = os.Stat(filepath.Join(target, source))
if os.IsNotExist(err) {
_, err = fmt.Fprintln(output, prefix+prenone, source)
if err != nil {
break
}
diff, err, prefix = true, nil, ""
} else if err == nil && !fi.IsDir() { // 虽然不大能
_, err = fmt.Fprintln(output, prefix+prefile, source)
if err != nil {
break
}
diff, prefix = true, ""
}
if err != nil {
break
}
ch <- nil
}
return
}
func mergeMode(ch chan interface{},
offset int, target, lib, lang string) (err error) {
var ok bool
var key, source, dst string
var paths string
var output *os.File
out := false
du := docu.New()
du.Filter = genNameFilter(lib, "")
tu := docu.New()
tu.Filter = genNameFilter(lib, lang)
fname := genFileName(lib, lang, ".go")
// 以 target 限制为过滤条件, 因此允许所有
for i := <-ch; i != nil; i = <-ch {
if err, ok = i.(error); !ok {
source = i.(string)
paths, err = du.Parse(source, nil)
}
// if docu.IsMultiplePkgError(err) {
// err = nil
// ch <- nil
// continue
// }
if err != nil {
break
}
if len(paths) == 0 {
ch <- nil
continue
}
key = paths
// 计算目标路径, 第一个可能是单文件
if dst == "" && strings.HasSuffix(source, ".go") {
source = filepath.Dir(source)
}
dst = filepath.Join(target, source[offset:])
paths, err = tu.Parse(filepath.Join(dst, fname), nil)
if os.IsNotExist(err) {
err = nil
}
if err != nil {
break
}
if key != paths {
ch <- nil
continue
}
dis := tu.MergePackageFiles(key)
// 自动提取第一个 lang, 只是为了过滤
if lang == "" {
lang = tu.NormalLang(key)
if lang == "" {
err = errors.New("missing argument lang")
break
}
tu.Filter = genNameFilter(lib, lang)
output = os.Stdout
fname = genFileName(lib, lang, ".go")
lang = "."
}
src := du.MergePackageFiles(key)
// src 为输出结果, 用目标过滤源
docu.SortDecl(dis.Decls).Filter(src)
if !docu.EqualComment(src.Doc, dis.Doc) {
docu.MergeDoc(dis.Doc, src.Doc)
}
docu.MergeDeclsDoc(dis.Decls, src.Decls)
if lang != "." {
output, err = createFile(dst, fname)
}
if err != nil {
break
}
if lang == "." {
if out {
_, err = os.Stdout.WriteString(sp)
}
out = true
}
if err == nil {
src.Unresolved = nil // 防止万一 src 为 godocu
err = docu.Fprint(output, src)
}
if output != os.Stdout {
output.Close()
}
if err != nil {
break
}
ch <- nil
}
return
}
func replaceMode(ch chan interface{},
offset int, target, lib, lang string) (err error) {
var ok bool
var key, source, dst string
var paths string
output := os.Stdout
out := false
du := docu.New()
du.Filter = genNameFilter(lib, lang)
tu := docu.New()
tu.Filter = du.Filter
fname := genFileName(lib, lang, ".go")
// 以 target 限制为过滤条件, 因此允许所有
for i := <-ch; i != nil; i = <-ch {
if err, ok = i.(error); !ok {
source = i.(string)
paths, err = du.Parse(source, nil)
}
// if docu.IsMultiplePkgError(err) {
// err = nil
// ch <- nil
// continue
// }
if err != nil {
break
}
if len(paths) == 0 {
ch <- nil
continue
}
key = paths
// 计算目标路径, 第一个可能是单文件
if dst == "" && strings.HasSuffix(source, ".go") {
source = filepath.Dir(source)
}
dst = filepath.Join(target, source[offset:])
paths, err = tu.Parse(filepath.Join(dst, fname), nil)
if os.IsNotExist(err) {
err = nil
}
if err != nil {
break
}
dis := tu.MergePackageFiles(key)
if dis == nil || paths != key {
ch <- nil
continue
}
src := du.MergePackageFiles(key)
if !docu.IsGodocuFile(src) || !docu.IsGodocuFile(dis) {
err = errors.New("source and target must be GodocuStyle documents")
break
}
// 自动提取第一个 lang, 只是为了过滤
if lang == "" {
lang = du.NormalLang(key)
if lang == "" || lang != tu.NormalLang(key) {
err = errors.New("missing argument lang")
break
}
du.Filter = genNameFilter(lib, lang)
tu.Filter = du.Filter
fname = genFileName(lib, lang, ".go")
lang = "."
}
docu.Replace(dis, src)
if len(dis.Imports) == 0 {
dis.Imports = src.Imports
}
if lang != "." {
output, err = createFile(dst, fname)
}
if err != nil {
break
}
if lang == "." {
if out {
_, err = os.Stdout.WriteString(sp)
}
out = true
}
if err == nil {
err = docu.Fprint(output, dis)
}
if output != os.Stdout {
output.Close()
}
if err != nil {
break
}
ch <- nil
}
return
}
func listMode(ch chan interface{}, offset int, target, lib, lang string) (err error) {
var ok bool
var source string
var paths string
var list docu.List
var bs []byte
du := docu.New()
du.Filter = genNameFilter(lib, lang)
list.Filename = genFileName(lib, lang, ".go")
if target != "" {
info, err := os.Stat(target)
if err != nil {
return err
}
if info.IsDir() {
target = filepath.Join(target, "golist.json")
} else if !strings.HasSuffix(info.Name(), ".json") {
return errors.New("invalid target")
}
bs, _ = ioutil.ReadFile(target)
// 提取现有属性
if bs != nil {
err = json.Unmarshal(bs, &list)
if err != nil {
return err
}
}
if list.Readme == "" {
list.Readme = docu.LookReadme(filepath.Dir(target))
} else {
info, err := os.Lstat(filepath.Join(filepath.Dir(target), list.Readme))
if err != nil || info.IsDir() {
return errors.New("invalid readme file: " + list.Readme)
}
}
list.Package = nil
if lang == "" {
lang = docu.LangOf(list.Filename)
list.Filename = genFileName(lib, lang, ".go")
du.Filter = genNameFilter(lib, lang)
lang = "."
}
}
for i := <-ch; i != nil; i = <-ch {
if err, ok = i.(error); !ok {
source = i.(string)
paths, err = du.Parse(source, nil)
}
if err != nil {
break
}
if len(paths) == 0 {
// 简单预测官方包
if list.Repo == "" {
if filepath.Base(source) == "archive" {
list.Repo = "github.com/golang/go"
}
}
ch <- nil
continue
}
key := paths