/
action.go
65 lines (55 loc) · 1.17 KB
/
action.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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"github.com/urfave/cli/v2"
)
func doConvert(c *cli.Context) error {
cfg, err := loadConfigToml()
if err != nil {
return err
}
ltype := c.String("layout")
if ltype == "" {
return errors.New("layout option is Required")
}
if lay, ok := cfg.Layouts[ltype]; ok {
switch c.Args().Len() {
case 0:
scanner := bufio.NewScanner(stdCLI.inputStream)
for scanner.Scan() {
fmt.Fprintln(stdCLI.outStream, convCSV(scanner.Text(), lay.Length))
}
case 1:
in, err := os.Open(c.Args().First())
if err != nil {
return err
}
defer in.Close()
scanner := bufio.NewScanner(in)
for scanner.Scan() {
fmt.Fprintln(stdCLI.outStream, convCSV(scanner.Text(), lay.Length))
}
default:
return errors.New("error : Invalid number of argument")
}
} else {
return errors.New(ltype + " is not defined filelayout")
}
return nil
}
func convCSV(str string, layout []int) (ret string) {
const camma = ","
if len(str) == 0 {
return ""
}
from := 0
for _, digit := range layout {
ret += string([]rune(str)[from:from+digit]) + camma
from += digit
}
return strings.TrimRight(ret, camma)
}