0
|
1 /****************************************************************************/
|
2
|
2 /* Entai_app.go ( ver 0.3 ) */
|
|
3 /* Last Change: 2016-10-13 Thu 06:04:09. */
|
0
|
4 /****************************************************************************/
|
|
5
|
|
6 package main
|
|
7
|
|
8 import (
|
1
|
9 "bufio"
|
0
|
10 "flag"
|
|
11 "fmt"
|
|
12 "os"
|
|
13 "strconv"
|
1
|
14 "strings"
|
0
|
15 "time"
|
|
16
|
|
17 "entai"
|
|
18 )
|
|
19
|
2
|
20 var ver = "0.3"
|
|
21 var e entai.Entai
|
|
22
|
0
|
23 func main() {
|
|
24
|
2
|
25 e.Create()
|
0
|
26
|
|
27 // arguments
|
|
28 h := flag.Bool( "h", false, "help" )
|
|
29 r := flag.Bool( "r", false, "print rate" )
|
|
30 l := flag.Bool( "l", false, "1-liner mode" )
|
1
|
31 b := flag.Bool( "b", false, "batch mode" )
|
0
|
32 v := flag.Bool( "v", false, "print verbose in 1-liner mode")
|
1
|
33 i := flag.Bool( "i", false, "print input value in 1-liner mode")
|
|
34 d := flag.Bool( "d", false, "intaracitve mode")
|
0
|
35 s := flag.Bool( "s", false, "server mode" )
|
1
|
36 p := flag.Int( "p", 8080, "listen port in server mode" )
|
0
|
37
|
|
38 flag.Parse()
|
|
39
|
|
40 if flag.NFlag() == 0 {
|
2
|
41 printTile()
|
0
|
42 flag.PrintDefaults()
|
|
43 fmt.Fprint( os.Stderr, "\n" )
|
|
44 os.Exit( 1 )
|
|
45 }
|
|
46
|
|
47 if *h {
|
2
|
48 printTile()
|
0
|
49 fmt.Fprint( os.Stderr, "1-liner mode:\n" )
|
|
50 fmt.Fprint( os.Stderr, "> entai_app -l 20160731 20161224 30000\n\n" )
|
|
51 fmt.Fprint( os.Stderr, "> entai_app -i -l 20160731 20161224 30000\n\n" )
|
|
52 fmt.Fprint( os.Stderr, "> entai_app -i -v -l 20160731 20161224 30000\n\n" )
|
|
53 fmt.Fprint( os.Stderr, "server mode:\n" )
|
1
|
54 fmt.Fprint( os.Stderr, "> entai_app -s -p 3000\n" )
|
0
|
55 os.Exit( 0 )
|
|
56 }
|
|
57
|
|
58 if *r {
|
2
|
59 printTile()
|
|
60 fmt.Fprint( os.Stderr, e.GetRate() )
|
0
|
61 os.Exit( 0 )
|
|
62 }
|
|
63
|
|
64 if *s {
|
|
65 *p = 8080
|
|
66 fmt.Fprint( os.Stderr, "server mode is not implemented.\n" )
|
|
67 os.Exit( 0 )
|
|
68 }
|
|
69
|
1
|
70 if *d {
|
2
|
71 printTile()
|
|
72 fmt.Print( e.GetRate() )
|
|
73 fmt.Print( "-----------\n\n" )
|
|
74
|
|
75 for {
|
|
76 fmt.Print( "Input Tax > " )
|
|
77 input := bufio.NewScanner( os.Stdin )
|
|
78 input.Scan()
|
|
79 t := input.Text()
|
|
80
|
|
81 fmt.Print( "Input Due > " )
|
|
82 input = bufio.NewScanner( os.Stdin )
|
|
83 input.Scan()
|
|
84 d := input.Text()
|
|
85
|
|
86 fmt.Print( "Input Paid > " )
|
|
87 input = bufio.NewScanner( os.Stdin )
|
|
88 input.Scan()
|
|
89 p := input.Text()
|
|
90
|
|
91 result, _, err := processEntai( d, p, t )
|
|
92 if err != nil {
|
|
93 fmt.Printf( "%v\n", err )
|
|
94 }
|
|
95 fmt.Printf( " = %d\n\n", result )
|
|
96
|
|
97 }
|
1
|
98 }
|
|
99
|
0
|
100 if *l {
|
2
|
101 if *i {
|
|
102 fmt.Printf( "%s,%s,%s,", flag.Arg(0), flag.Arg(1), flag.Arg(2) )
|
0
|
103 }
|
|
104
|
2
|
105 result, detail, err := processEntai( flag.Arg(0), flag.Arg(1), flag.Arg(2) )
|
|
106 if err != nil {
|
|
107 fmt.Fprintf( os.Stderr, "%s : %v\n", detail, err )
|
|
108 os.Exit( 1 )
|
|
109 }
|
0
|
110 fmt.Print( result )
|
|
111
|
|
112 if *v {
|
|
113 fmt.Print( ",", detail )
|
|
114 }
|
|
115 fmt.Print( "\n" )
|
2
|
116
|
1
|
117 os.Exit( 0 )
|
|
118 }
|
|
119
|
|
120 if *b {
|
2
|
121
|
1
|
122 files := flag.Args()
|
2
|
123
|
1
|
124 if len( files ) == 0 {
|
|
125 fmt.Fprint( os.Stderr, "no input file.\n" )
|
|
126 os.Exit( 1 )
|
|
127 } else {
|
2
|
128
|
1
|
129 for _, file := range files {
|
|
130 f, err := os.Open( file ); if err != nil {
|
|
131 fmt.Fprintf( os.Stderr, "cannot open file.[%v]\n", err )
|
|
132 os.Exit( 1 )
|
|
133 }
|
2
|
134
|
1
|
135 input := bufio.NewScanner( f )
|
|
136 b := bufio.NewWriter( os.Stdout )
|
|
137 for input.Scan() {
|
|
138 s := strings.Split( input.Text(), "," )
|
2
|
139 if strings.HasPrefix( input.Text(), "#" ) {
|
|
140 if strings.Contains( input.Text(), "@@" ) {
|
|
141 buf := strings.Replace( input.Text(), "#", "", 1 )
|
|
142 buf = strings.Replace( buf, "@@", "", 1 )
|
|
143 fmt.Fprintf( b, "%s\n", buf )
|
|
144 }
|
|
145 continue
|
|
146 }
|
|
147 result, detail, err := processEntai( s[0], s[1], s[2] )
|
|
148 if err != nil {
|
|
149 fmt.Fprintf( os.Stderr, "%s : %v\n", detail, err )
|
|
150 os.Exit( 1 )
|
|
151 }
|
|
152 fmt.Fprintf( b, "%s,%s,%s,%d,%s\n", s[0], s[1], s[2], result, detail )
|
1
|
153 }
|
|
154 b.Flush()
|
|
155 f.Close()
|
|
156 }
|
|
157 os.Exit( 0 )
|
|
158 }
|
0
|
159 }
|
|
160 }
|
|
161
|
2
|
162 func processEntai( due, paid, tax string ) ( int, string, error ) {
|
|
163 d, msg, err := validDate( due ); if err != nil {
|
|
164 return -1, msg, err
|
|
165 }
|
|
166 p, msg, err := validDate( paid ); if err != nil {
|
|
167 return -1, msg, err
|
|
168 }
|
|
169
|
|
170 t, msg, err := validInt( tax ); if err != nil {
|
|
171 return -1, msg, err
|
|
172 }
|
|
173
|
|
174 e.Set( d, p, t )
|
|
175 result, detail := e.Result()
|
|
176
|
|
177 return result, detail, nil
|
0
|
178 }
|
|
179
|
2
|
180 func validDate( s string ) ( time.Time, string, error ) {
|
|
181 layout := "20060102"
|
|
182 t, err := time.Parse( layout, s ); if err != nil {
|
|
183 return t, "bad date format.", err
|
|
184 }
|
|
185 return t, "", nil
|
|
186 }
|
|
187
|
|
188 func validInt( s string ) ( int, string, error ) {
|
|
189 i, err := strconv.Atoi( s )
|
|
190 if err != nil || i < 0 {
|
|
191 return -1, "bad money format.", err
|
|
192 }
|
|
193 return i, "", nil
|
|
194 }
|
|
195
|
|
196 func printTile() {
|
|
197 fmt.Fprint( os.Stderr, "\n==================================================\n" )
|
|
198 fmt.Fprintf( os.Stderr, " Entai_app ver %s ( 2016.10.09 ) - since 2016\n", ver )
|
|
199 fmt.Fprint( os.Stderr, "==================================================\n\n" )
|
|
200 }
|
|
201
|