Skip to content

Commit 6892064

Browse files
committed
標準出力 対応
1 parent 35685c2 commit 6892064

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

cmd/tmpl/main.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func main() {
4444
flag.StringVar(&pathCsv, "c", "", "[must]csv path")
4545
flag.StringVar(&encodeTemplate, "te", defaultEncoding, "template encoding")
4646
flag.StringVar(&encodeCsv, "ce", defaultEncoding, "csv encoding")
47-
flag.StringVar(&output, "o", "./", "output path or file")
47+
flag.StringVar(&output, "o", "./", "output path or file ( - as standard IO)")
4848
flag.IntVar(&nameCol, "n", -1, "Name column no")
4949
flag.BoolVar(&tsv, "tsv", false, "TSV:Tab-Separated Values")
5050
flag.BoolVar(&regex, "regex", false, "regex")
@@ -128,29 +128,37 @@ func writeFile(pathOrFile string, pathTemlate string, ch chan tmpl.Result) error
128128
var name string
129129
var ext string
130130
var dir string
131+
var filename string
131132
stat, err := os.Stat(pathOrFile)
132133

133134
if err == nil && stat.IsDir() == true {
134135
dir = pathOrFile
135136
_, name = filepath.Split(pathTemlate)
136137
name = strings.Split(name, ".")[0]
137138
ext = filepath.Ext(pathTemlate)
139+
} else if pathOrFile == "-" {
140+
138141
} else {
139142
dir, name = filepath.Split(pathOrFile)
140143
name = strings.Split(name, ".")[0]
141144
ext = filepath.Ext(pathOrFile)
142145
}
143146
for res := range ch {
147+
filename = ""
144148
if res.Err != nil {
145149
return res.Err
146150
}
147151
idx := fmt.Sprintf("%0"+strconv.Itoa(int(math.Floor(math.Log10(float64(res.Total)))))+"d", res.No)
148152
if res.Name != "" {
149-
name = res.Name + ext
153+
filename = res.Name + ext
154+
} else {
155+
filename = name + "_" + idx + ext
156+
}
157+
if pathOrFile != "-" {
158+
ioutil.WriteFile(filepath.Join(dir, filename), []byte(res.Str), os.ModePerm)
150159
} else {
151-
name = name + "_" + idx + ext
160+
fmt.Println(res.Str)
152161
}
153-
ioutil.WriteFile(filepath.Join(dir, name), []byte(res.Str), os.ModePerm)
154162
}
155163
return nil
156164
}

0 commit comments

Comments
 (0)