-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (81 loc) · 2.16 KB
/
main.go
File metadata and controls
93 lines (81 loc) · 2.16 KB
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
package sqlbless
import (
"flag"
"fmt"
"io"
"os"
"runtime"
"unicode/utf8"
"github.com/hymkor/struct2flag"
"github.com/hymkor/sqlbless/dialect"
)
type Config struct {
Auto string `flag:"auto,autopilot"`
Term string `flag:"term,SQL terminator to use instead of semicolon"`
CrLf bool `flag:"crlf,Use CRLF"`
Null string `flag:"null,Set a string representing NULL"`
Tsv bool `flag:"tsv,Use TAB as seperator"`
FieldSeperator string `flag:"fs,Set field separator"`
Debug bool `flag:"debug,Print type in CSV"`
SubmitByEnter bool `flag:"submit-enter,Submit by [Enter] and insert a new line by [Ctrl]-[Enter]"`
Script string `flag:"f,script file"`
SpoolFilename string `flag:"spool,Spool filename"`
ReverseVideo bool `flag:"rv,Enable reverse-video display (invert foreground and background colors)"`
}
func (cfg *Config) comma() byte {
if cfg.Tsv {
return '\t'
}
if len(cfg.FieldSeperator) > 0 {
c, _ := utf8.DecodeRuneInString(cfg.FieldSeperator)
return byte(c)
}
return ','
}
func New() *Config {
return &Config{
FieldSeperator: ",",
Null: "\u2400",
Term: ";",
SpoolFilename: os.DevNull,
}
}
func (cfg *Config) Bind(fs *flag.FlagSet) *Config {
struct2flag.Bind(fs, cfg)
return cfg
}
func writeSignature(w io.Writer) {
fmt.Fprintf(w, "# SQL-Bless %s-%s-%s built with %s\n",
version, runtime.GOOS, runtime.GOARCH, runtime.Version())
}
func usage() {
w := flag.CommandLine.Output()
fmt.Fprintf(w, "Usage: %s {-options} [DRIVERNAME] DATASOURCENAME\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintln(w, "Example:")
dialect.Each(
func(_ string, d *dialect.Entry) bool {
fmt.Fprintf(w, " %s\n", d.Usage)
return true
},
)
}
func Run() error {
writeSignature(os.Stdout)
cfg := New().Bind(flag.CommandLine)
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) < 1 {
flag.Usage()
return nil
}
d, err := dialect.ReadDBInfoFromArgs(args)
if err != nil {
return fmt.Errorf("dialect.ReadDBInfoFromArgs: %w", err)
}
if cfg.Debug {
fmt.Fprintln(os.Stderr, d.DataSource)
}
return cfg.Run(d.Driver, d.DataSource, d.Dialect)
}