1- // Copyright © 2016 The Go MSSQL Runner Authors
2- //
3- // Licensed under the Apache License, Version 2.0 (the "License");
4- // you may not use this file except in compliance with the License.
5- // You may obtain a copy of the License at
6- //
7- // http://www.apache.org/licenses/LICENSE-2.0
8- //
9- // Unless required by applicable law or agreed to in writing, software
10- // distributed under the License is distributed on an "AS IS" BASIS,
11- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12- // See the License for the specific language governing permissions and
13- // limitations under the License.
14-
151package cmd
162
173import (
184 "fmt"
19- "log "
5+ "io "
206 "os"
217 "time"
228
23- "io"
24-
259 "github.com/coolhanddev/go-mssql-runner/pkg/config"
2610 "github.com/coolhanddev/go-mssql-runner/pkg/mssql"
11+ log "github.com/sirupsen/logrus"
2712 "github.com/spf13/cobra"
2813)
2914
@@ -39,6 +24,8 @@ var logLevel string
3924var cn config.MssqlCn
4025var logToFile string
4126var logFileName os.File
27+ var logFormat string
28+ var encryptCn bool
4229
4330// startCmd represents the start command
4431var startCmd = & cobra.Command {
@@ -83,63 +70,90 @@ Different log levels can be set via the -l flag.
837063 full logging
8471
8572` ,
86- Run : func (cmd * cobra.Command , args []string ) {
87- //check if required parameters are passed
88- if userName == "" || password == "" || server == "" || database == "" || configFile == "" {
89- //if required parameters are not met, check environment variables
90- if os .Getenv ("GOSQLR_CONFIGFILE" ) == "" || os .Getenv ("GOSQLR_USERNAME" ) == "" ||
91- os .Getenv ("GOSQLR_PASSWORD" ) == "" || os .Getenv ("GOSQLR_SERVER" ) == "" ||
92- os .Getenv ("GOSQLR_DATABASE" ) == "" {
93-
94- fmt .Println ("Please pass in the required values. " )
95- fmt .Println ("go-mssql-runner start -h for more information." )
96- os .Exit (- 1 )
97- } else if os .Getenv ("GOSQLR_CONFIGFILE" ) != "" && os .Getenv ("GOSQLR_USERNAME" ) != "" &&
98- os .Getenv ("GOSQLR_PASSWORD" ) != "" && os .Getenv ("GOSQLR_SERVER" ) != "" &&
99- os .Getenv ("GOSQLR_DATABASE" ) != "" {
100- configFile = os .Getenv ("GOSQLR_CONFIGFILE" )
101- userName = os .Getenv ("GOSQLR_USERNAME" )
102- password = os .Getenv ("GOSQLR_PASSWORD" )
103- server = os .Getenv ("GOSQLR_SERVER" )
104- database = os .Getenv ("GOSQLR_DATABASE" )
105- }
73+ Run : start ,
74+ }
75+
76+ func start (cmd * cobra.Command , args []string ) {
77+ if server == "" || database == "" || configFile == "" {
78+ //if required parameters are not met, check environment variables
79+ if os .Getenv ("GOSQLR_CONFIGFILE" ) == "" || os .Getenv ("GOSQLR_SERVER" ) == "" || os .Getenv ("GOSQLR_DATABASE" ) == "" {
80+ fmt .Println ("Please pass in the required values. " )
81+ fmt .Println ("go-mssql-runner start -h for more information." )
82+ os .Exit (1 )
83+ } else if os .Getenv ("GOSQLR_CONFIGFILE" ) != "" && os .Getenv ("GOSQLR_USERNAME" ) != "" &&
84+ os .Getenv ("GOSQLR_PASSWORD" ) != "" && os .Getenv ("GOSQLR_SERVER" ) != "" &&
85+ os .Getenv ("GOSQLR_DATABASE" ) != "" {
86+ configFile = os .Getenv ("GOSQLR_CONFIGFILE" )
87+ userName = os .Getenv ("GOSQLR_USERNAME" )
88+ password = os .Getenv ("GOSQLR_PASSWORD" )
89+ server = os .Getenv ("GOSQLR_SERVER" )
90+ database = os .Getenv ("GOSQLR_DATABASE" )
10691 }
107- cn .UserName = userName
108- cn .Password = password
109- cn .Server = server
110- cn .Database = database
111- cn .Port = port
112- cn .AppName = appName
113- cn .CnTimeout = cnTimeout
114- cn .LogLevel = logLevel
115- startTime := time .Now ()
116- //set up logging. we want to log both to stdout and to a file
117- if logToFile != "" {
118- //if file already exists then append. log rotation done manually by user
119- logFileName , err := os .OpenFile (logToFile , os .O_CREATE | os .O_RDWR | os .O_APPEND , 0666 )
120- if err != nil {
121- log .Fatal (err )
122- }
123- mw := io .MultiWriter (os .Stdout , logFileName )
124- log .SetOutput (mw )
92+ }
93+ cn .UserName = userName
94+ cn .Password = password
95+ cn .Server = server
96+ cn .Database = database
97+ cn .Port = port
98+ cn .AppName = appName
99+ cn .CnTimeout = cnTimeout
100+ cn .LogLevel = logLevel
101+ cn .Encrypt = encryptCn
102+ startTime := time .Now ()
103+ initLogging ()
104+ defer logFileName .Close ()
105+ newDbPool (cn )
106+ loadConfig ()
107+ execScripts ()
108+ elapsed := time .Since (startTime )
109+ if logToFile != "" {
110+ log .WithFields (log.Fields {"log_file" : logToFile }).Info ("Log file created" )
111+ }
112+ log .Info ("Total time elapsed" , "=" , elapsed )
113+ }
114+
115+ func newDbPool (c config.MssqlCn ) {
116+ log .WithFields (log.Fields {"server" : cn .Server , "database" : cn .Database }).Info ("opening database" )
117+ mssql .NewPool (config .GetCnString (c ))
118+ }
119+
120+ func initLogging () {
121+ //we want to log both to stdout and to a file
122+ if logFormat == "JSON" {
123+ log .SetFormatter (& log.JSONFormatter {TimestampFormat : "01-02-2006 15:04:05" })
124+ } else {
125+ log .SetFormatter (& log.TextFormatter {TimestampFormat : "01-02-2006 15:04:05" , FullTimestamp : true })
126+ }
127+ if logToFile != "" {
128+ //if file already exists then append. log rotation done manually by user
129+ logFileName , err := os .OpenFile (logToFile , os .O_CREATE | os .O_RDWR | os .O_APPEND , 0666 )
130+ if err != nil {
131+ log .Fatal (err )
125132 }
133+ mw := io .MultiWriter (os .Stdout , logFileName )
134+ log .SetOutput (mw )
135+ }
136+ }
126137
127- log .Println ("Opening database" , "=" , cn .Server , "/" , cn .Database )
128- mssql .OpenCn (config .GetCnString (cn ))
129- log .Println ("Loading configuration" , "=" , configFile )
130- config .ReadConfig (configFile )
131- log .Println ("================================================" )
132- log .Println ("Executing schema scripts" )
133- mssql .RunScripts (config .GetSchemaScripts ())
134- log .Println ("================================================" )
135- log .Println ("Executing process scripts" )
136- mssql .RunScripts (config .GetProcessScripts ())
137- elapsed := time .Since (startTime )
138- log .Println ("Total time elapsed" , "=" , elapsed )
139- logFileName .Close ()
140- },
138+ func loadConfig () {
139+ log .WithFields (log.Fields {"config_file" : configFile }).Info ("loading configuration" )
140+ config .ReadConfig (configFile )
141141}
142142
143+ func execScripts () {
144+ log .Info ("================================================" )
145+ log .Info ("Executing schema scripts" )
146+ _ , err := mssql .RunScripts (config .GetSchemaScripts ())
147+ if err != nil {
148+ log .Fatal (err )
149+ }
150+ log .Info ("================================================" )
151+ log .Info ("Executing process scripts" )
152+ _ , err = mssql .RunScripts (config .GetProcessScripts ())
153+ if err != nil {
154+ log .Fatal (err )
155+ }
156+ }
143157func init () {
144158 RootCmd .AddCommand (startCmd )
145159
@@ -163,4 +177,6 @@ func init() {
163177 startCmd .Flags ().StringVarP (& appName , "appname" , "a" , "go-mssql-runner" , "App name to show in db calls. Useful for SQL Profiler" )
164178 startCmd .Flags ().StringVarP (& logLevel , "loglevel" , "l" , "0" , logLevelMsg )
165179 startCmd .Flags ().StringVarP (& logToFile , "logfile" , "" , "" , "File to write log to" )
180+ startCmd .Flags ().StringVarP (& logFormat , "logformat" , "" , "text" , "Format of log: JSON or text" )
181+ startCmd .Flags ().BoolVarP (& encryptCn , "encrypt-cn" , "e" , false , "Encrypt SQL Server connection" )
166182}
0 commit comments