Skip to content

Commit 5f34e53

Browse files
committed
Write to output file given via flag
1 parent a5a6b4f commit 5f34e53

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ Usage
4141
-----
4242

4343
Call the rdf2smw binary, specifying a file with triples in n-triples or turtle
44-
format, with the `--infile` flag. Output is written to stdout, so you have to
45-
redirect it to a file of a chosen name:
44+
format, with the `--in` flag, and an output file in XML format with the
45+
`--out` flag, like so:
4646

4747
```bash
48-
./rdf2smw --infile triples.nt > semantic_mediawiki_pages.xml
48+
./rdf2smw --in triples.nt --out semantic_mediawiki_pages.xml
4949
```
5050

5151
The resulting XML file, can then be imported into MediaWiki / Semantic

main.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,20 @@ const (
2323
func main() {
2424
flowbase.InitLogInfo()
2525

26-
inFileName := flag.String("infile", "", "The input file name")
26+
inFileName := flag.String("in", "", "The input file name")
27+
outFileName := flag.String("out", "", "The output file name")
2728
flag.Parse()
29+
30+
doExit := false
2831
if *inFileName == "" {
29-
fmt.Println("No filename specified to --infile")
32+
fmt.Println("No filename specified to --in")
33+
doExit = true
34+
} else if *outFileName == "" {
35+
fmt.Println("No filename specified to --out")
36+
doExit = true
37+
}
38+
39+
if doExit {
3040
os.Exit(1)
3141
}
3242

@@ -72,8 +82,10 @@ func main() {
7282
xmlCreator := NewMWXMLCreator(useTemplates)
7383
pipeRunner.AddProcess(xmlCreator)
7484

75-
printer := NewStringPrinter()
76-
pipeRunner.AddProcess(printer)
85+
//printer := NewStringPrinter()
86+
//pipeRunner.AddProcess(printer)
87+
strFileWriter := NewStringFileWriter(*outFileName)
88+
pipeRunner.AddProcess(strFileWriter)
7789

7890
// ------------------------------------------
7991
// Connect network
@@ -760,6 +772,33 @@ func (p *StringPrinter) Run() {
760772
}
761773
}
762774

775+
// --------------------------------------------------------------------------------
776+
// String File Writer
777+
// --------------------------------------------------------------------------------
778+
779+
type StringFileWriter struct {
780+
In chan string
781+
fileName string
782+
}
783+
784+
func NewStringFileWriter(fileName string) *StringFileWriter {
785+
return &StringFileWriter{
786+
In: make(chan string, BUFSIZE),
787+
fileName: fileName,
788+
}
789+
}
790+
791+
func (p *StringFileWriter) Run() {
792+
fh, err := os.Create(p.fileName)
793+
if err != nil {
794+
panic("Could not create output file: " + err.Error())
795+
}
796+
defer fh.Close()
797+
for s := range p.In {
798+
fh.WriteString(s)
799+
}
800+
}
801+
763802
// --------------------------------------------------------------------------------
764803
// IP: RDFTriple
765804
// --------------------------------------------------------------------------------

0 commit comments

Comments
 (0)