-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (138 loc) · 3.66 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
// Package main (gorearrange.go) :
// This file is included all commands and options.
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"syscall"
"golang.org/x/crypto/ssh/terminal"
rearrange "github.com/tanaikech/go-rearrange"
"github.com/urfave/cli"
)
// const :
const (
appname = "gorearrange"
)
// main : Get command-line arguments.
func main() {
app := cli.NewApp()
app.Name = appname
app.Authors = []*cli.Author{
{Name: "tanaike [ https://github.com/tanaikech/gorearrange ] ", Email: "[email protected]"},
}
app.UsageText = "Manually rearrange text data."
app.Version = "1.0.3"
app.Flags = []cli.Flag{
&cli.IntFlag{
Name: "step, s",
Aliases: []string{"s"},
Value: 5,
Usage: "Number of steps for PageUp, PageDown.",
},
&cli.StringFlag{
Name: "inputfile, i",
Aliases: []string{"i"},
Value: "",
Usage: "Input text file that you want to rearrange.",
},
&cli.StringFlag{
Name: "outputfile, o",
Aliases: []string{"o"},
Value: "",
Usage: "Output rearranged text file.",
},
&cli.BoolFlag{
Name: "selectmode, select",
Aliases: []string{"select"},
Usage: "Use as select mode. In this case, users only select a value.",
},
&cli.BoolFlag{
Name: "indexmode, index",
Aliases: []string{"index"},
Usage: "After rearranging source data, the change of index for the source data is output.",
},
&cli.BoolFlag{
Name: "history, hi",
Aliases: []string{"hi"},
Usage: "Show history of selected data.",
},
}
app.Action = gorearrange
app.Run(os.Args)
}
// getValues : Get values from stdin and file.
func getValues(filepath string) []string {
var data []string
if terminal.IsTerminal(int(syscall.Stdin)) {
if len(filepath) == 0 {
fmt.Printf("Please input a text data file.\n\n $ cat filename | %s\n\n or \n\n $ %s -f filename\n\n Help is \"%s --help\"\n", appname, appname, appname)
os.Exit(0)
} else {
f, err := os.Open(filepath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Script '%s' is not found.\n", filepath)
os.Exit(1)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if scanner.Text() == "end" {
break
}
data = append(data, scanner.Text())
}
if scanner.Err() != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", scanner.Err())
os.Exit(1)
}
}
} else {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if scanner.Text() == "end" {
break
}
data = append(data, scanner.Text())
}
if scanner.Err() != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", scanner.Err())
os.Exit(1)
}
}
if len(data) == 0 {
fmt.Fprintf(os.Stderr, "Error: No data. Please check help.\n\n $ %s help\n\n", appname)
os.Exit(1)
}
return data
}
// gorearrange : Handler for gorearrange
func gorearrange(c *cli.Context) error {
result, history, err := rearrange.Do(getValues(c.String("inputfile")), c.Int("step"), c.Bool("selectmode"), c.Bool("indexmode"))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
outdata := strings.Join(result, "\n") + "\n"
if !c.Bool("selectmode") {
if len(c.String("outputfile")) == 0 {
w := bufio.NewWriter(os.Stdout)
w.WriteString(outdata)
w.Flush()
} else {
ioutil.WriteFile(c.String("outputfile"), []byte(outdata), os.ModePerm)
}
if c.Bool("history") {
r, _ := json.Marshal(history)
fmt.Printf("\n%+v\n", string(r))
}
} else {
w := bufio.NewWriter(os.Stdout)
w.WriteString(history[len(history)-1].Value + "\n")
w.Flush()
}
return nil
}