Mercurial > mercurial > hgweb_golang.cgi
comparison src/entai_app/entai_app.go @ 2:451c99c1d9de
implement intaractive mode.
| author | pyon@macmini |
|---|---|
| date | Thu, 13 Oct 2016 06:05:06 +0900 |
| parents | 3dafd57af3b1 |
| children | ca866a38a6a0 |
comparison
equal
deleted
inserted
replaced
| 1:3dafd57af3b1 | 2:451c99c1d9de |
|---|---|
| 1 /****************************************************************************/ | 1 /****************************************************************************/ |
| 2 /* Entai_app.go ( ver 0.2 ) */ | 2 /* Entai_app.go ( ver 0.3 ) */ |
| 3 /* Last Change: 2016-10-06 Thu 00:49:10. */ | 3 /* Last Change: 2016-10-13 Thu 06:04:09. */ |
| 4 /****************************************************************************/ | 4 /****************************************************************************/ |
| 5 | 5 |
| 6 package main | 6 package main |
| 7 | 7 |
| 8 import ( | 8 import ( |
| 15 "time" | 15 "time" |
| 16 | 16 |
| 17 "entai" | 17 "entai" |
| 18 ) | 18 ) |
| 19 | 19 |
| 20 var ver = "0.3" | |
| 21 var e entai.Entai | |
| 22 | |
| 20 func main() { | 23 func main() { |
| 21 | 24 |
| 22 var entai entai.Entai | 25 e.Create() |
| 23 | 26 |
| 24 // arguments | 27 // arguments |
| 25 h := flag.Bool( "h", false, "help" ) | 28 h := flag.Bool( "h", false, "help" ) |
| 26 r := flag.Bool( "r", false, "print rate" ) | 29 r := flag.Bool( "r", false, "print rate" ) |
| 27 l := flag.Bool( "l", false, "1-liner mode" ) | 30 l := flag.Bool( "l", false, "1-liner mode" ) |
| 33 p := flag.Int( "p", 8080, "listen port in server mode" ) | 36 p := flag.Int( "p", 8080, "listen port in server mode" ) |
| 34 | 37 |
| 35 flag.Parse() | 38 flag.Parse() |
| 36 | 39 |
| 37 if flag.NFlag() == 0 { | 40 if flag.NFlag() == 0 { |
| 38 PrintTile() | 41 printTile() |
| 39 flag.PrintDefaults() | 42 flag.PrintDefaults() |
| 40 fmt.Fprint( os.Stderr, "\n" ) | 43 fmt.Fprint( os.Stderr, "\n" ) |
| 41 os.Exit( 1 ) | 44 os.Exit( 1 ) |
| 42 } | 45 } |
| 43 | 46 |
| 44 if *h { | 47 if *h { |
| 45 PrintTile() | 48 printTile() |
| 46 fmt.Fprint( os.Stderr, "1-liner mode:\n" ) | 49 fmt.Fprint( os.Stderr, "1-liner mode:\n" ) |
| 47 fmt.Fprint( os.Stderr, "> entai_app -l 20160731 20161224 30000\n\n" ) | 50 fmt.Fprint( os.Stderr, "> entai_app -l 20160731 20161224 30000\n\n" ) |
| 48 fmt.Fprint( os.Stderr, "> entai_app -i -l 20160731 20161224 30000\n\n" ) | 51 fmt.Fprint( os.Stderr, "> entai_app -i -l 20160731 20161224 30000\n\n" ) |
| 49 fmt.Fprint( os.Stderr, "> entai_app -i -v -l 20160731 20161224 30000\n\n" ) | 52 fmt.Fprint( os.Stderr, "> entai_app -i -v -l 20160731 20161224 30000\n\n" ) |
| 50 fmt.Fprint( os.Stderr, "server mode:\n" ) | 53 fmt.Fprint( os.Stderr, "server mode:\n" ) |
| 51 fmt.Fprint( os.Stderr, "> entai_app -s -p 3000\n" ) | 54 fmt.Fprint( os.Stderr, "> entai_app -s -p 3000\n" ) |
| 52 os.Exit( 0 ) | 55 os.Exit( 0 ) |
| 53 } | 56 } |
| 54 | 57 |
| 55 if *r { | 58 if *r { |
| 56 PrintTile() | 59 printTile() |
| 57 fmt.Fprint( os.Stderr, entai.GetRate() ) | 60 fmt.Fprint( os.Stderr, e.GetRate() ) |
| 58 os.Exit( 0 ) | 61 os.Exit( 0 ) |
| 59 } | 62 } |
| 60 | 63 |
| 61 if *s { | 64 if *s { |
| 62 *p = 8080 | 65 *p = 8080 |
| 63 fmt.Fprint( os.Stderr, "server mode is not implemented.\n" ) | 66 fmt.Fprint( os.Stderr, "server mode is not implemented.\n" ) |
| 64 os.Exit( 0 ) | 67 os.Exit( 0 ) |
| 65 } | 68 } |
| 66 | 69 |
| 67 if *d { | 70 if *d { |
| 68 PrintTile() | 71 printTile() |
| 69 //layout := "20060102" | 72 fmt.Print( e.GetRate() ) |
| 70 //for { | 73 fmt.Print( "-----------\n\n" ) |
| 71 //} | 74 |
| 72 os.Exit( 0 ) | 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 } | |
| 73 } | 98 } |
| 74 | 99 |
| 75 if *l { | 100 if *l { |
| 76 layout := "20060102" | |
| 77 | |
| 78 d, err := time.Parse( layout, flag.Arg(0) ); if err != nil { | |
| 79 fmt.Fprint( os.Stderr, "bad date format.\n" ) | |
| 80 os.Exit( 1 ) | |
| 81 } | |
| 82 p, err := time.Parse( layout, flag.Arg(1) ); if err != nil { | |
| 83 fmt.Fprint( os.Stderr, "bad date format.\n" ) | |
| 84 os.Exit( 1 ) | |
| 85 } | |
| 86 | |
| 87 t, err := strconv.Atoi( flag.Arg(2) ) | |
| 88 if err != nil || t < 0 { | |
| 89 fmt.Fprint( os.Stderr, "bad money format.\n" ) | |
| 90 os.Exit( 1 ) | |
| 91 } | |
| 92 | |
| 93 if *i { | 101 if *i { |
| 94 fmt.Printf( "%v,%v,%d,", d.Format( layout ), p.Format( layout ), t ) | 102 fmt.Printf( "%s,%s,%s,", flag.Arg(0), flag.Arg(1), flag.Arg(2) ) |
| 95 } | 103 } |
| 96 | 104 |
| 97 // MAIN | 105 result, detail, err := processEntai( flag.Arg(0), flag.Arg(1), flag.Arg(2) ) |
| 98 entai.Set( d, p, t ) | 106 if err != nil { |
| 99 result, detail := entai.Result() | 107 fmt.Fprintf( os.Stderr, "%s : %v\n", detail, err ) |
| 100 // | 108 os.Exit( 1 ) |
| 101 | 109 } |
| 102 fmt.Print( result ) | 110 fmt.Print( result ) |
| 103 | 111 |
| 104 if *v { | 112 if *v { |
| 105 fmt.Print( ",", detail ) | 113 fmt.Print( ",", detail ) |
| 106 } | 114 } |
| 107 fmt.Print( "\n" ) | 115 fmt.Print( "\n" ) |
| 116 | |
| 108 os.Exit( 0 ) | 117 os.Exit( 0 ) |
| 109 } | 118 } |
| 110 | 119 |
| 111 if *b { | 120 if *b { |
| 121 | |
| 112 files := flag.Args() | 122 files := flag.Args() |
| 123 | |
| 113 if len( files ) == 0 { | 124 if len( files ) == 0 { |
| 114 fmt.Fprint( os.Stderr, "no input file.\n" ) | 125 fmt.Fprint( os.Stderr, "no input file.\n" ) |
| 115 os.Exit( 1 ) | 126 os.Exit( 1 ) |
| 116 } else { | 127 } else { |
| 128 | |
| 117 for _, file := range files { | 129 for _, file := range files { |
| 118 f, err := os.Open( file ); if err != nil { | 130 f, err := os.Open( file ); if err != nil { |
| 119 fmt.Fprintf( os.Stderr, "cannot open file.[%v]\n", err ) | 131 fmt.Fprintf( os.Stderr, "cannot open file.[%v]\n", err ) |
| 120 os.Exit( 1 ) | 132 os.Exit( 1 ) |
| 121 } | 133 } |
| 134 | |
| 122 input := bufio.NewScanner( f ) | 135 input := bufio.NewScanner( f ) |
| 123 b := bufio.NewWriter( os.Stdout ) | 136 b := bufio.NewWriter( os.Stdout ) |
| 124 for input.Scan() { | 137 for input.Scan() { |
| 125 s := strings.Split( input.Text(), "," ) | 138 s := strings.Split( input.Text(), "," ) |
| 126 fmt.Fprint( b, s[0] ) | 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 ) | |
| 127 } | 153 } |
| 128 b.Flush() | 154 b.Flush() |
| 129 f.Close() | 155 f.Close() |
| 130 } | 156 } |
| 131 os.Exit( 0 ) | 157 os.Exit( 0 ) |
| 132 } | 158 } |
| 133 } | 159 } |
| 134 } | 160 } |
| 135 | 161 |
| 136 func PrintTile() { | 162 func processEntai( due, paid, tax string ) ( int, string, error ) { |
| 137 fmt.Fprint( os.Stderr, "\n==================================================\n" ) | 163 d, msg, err := validDate( due ); if err != nil { |
| 138 fmt.Fprint( os.Stderr, " Entai_app ver 0.2 ( 2016.10.09 ) - since 2016\n" ) | 164 return -1, msg, err |
| 139 fmt.Fprint( os.Stderr, "==================================================\n\n" ) | 165 } |
| 140 } | 166 p, msg, err := validDate( paid ); if err != nil { |
| 141 | 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 | |
| 178 } | |
| 179 | |
| 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 |
