Skip to content

Commit b960d08

Browse files
committed
Merge branch 'dbtest' into dev-1-1-0
2 parents edc19c3 + 5fc6f32 commit b960d08

File tree

18 files changed

+400
-164
lines changed

18 files changed

+400
-164
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ go-mssql-runner*
44
.vscode
55
debug
66
.idea
7-
logfile.txt
7+
logfile.txt
8+
release

Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM alpine:latest
2-
ADD . /gobin
3-
WORKDIR /gobin
4-
1+
#base image
2+
#TODO: add more documentation on how to create a project specific image
3+
FROM alpine
4+
COPY ./release/alpine-linux/go-mssql-runner .
5+
CMD ["./go-mssql-runner", "version"]

Makefile

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#TODO \
2+
1) Docker build \
3+
2) Build for alpine linux
4+
BINDIR := $(GOPATH)/bin
5+
BINARYFILE := go-mssql-runner
6+
GOMETALINTER := $(BIN_DIR)/gometalinter
7+
#Version is driven by git tags
8+
VERSION := $(shell git describe --tags)
9+
LINUXFOLDER := release/linux
10+
WINFOLDER := release/windows
11+
DARWINFOLDER := release/darwin
12+
ALPINEFOLDER := release/alpine-linux
13+
14+
all: test create-folders build
15+
16+
$(GOMETALINTER):
17+
go get -u github.com/alecthomas/gometalinter
18+
gometalinter --install &> /dev/null
19+
20+
lint: $(GOMETALINTER)
21+
gometalinter ./... --vendor
22+
23+
create-folders:
24+
mkdir -p $(LINUXFOLDER)
25+
mkdir -p $(WINFOLDER)
26+
mkdir -p $(DARWINFOLDER)
27+
mkdir -p $(ALPINEFOLDER)
28+
29+
test: update-package
30+
go test -v --cover ./...
31+
32+
update-package:
33+
go get -u github.com/denisenkom/go-mssqldb
34+
go get -u github.com/sirupsen/logrus
35+
go get -u github.com/spf13/cobra
36+
go get -u github.com/inconshreveable/mousetrap #for windows build
37+
38+
build: linux windows darwin alpine-linux
39+
40+
linux:
41+
@echo ------------------Building Linux binary------------------
42+
GOOS=linux GOARCH=amd64 go build -a -installsuffix -i -v -o $(LINUXFOLDER)/$(BINARYFILE) -ldflags="-X main.Version=$(VERSION)"
43+
@echo ------------------Completed building Linux binary------------------
44+
windows:
45+
@echo ------------------Building Windows binary------------------
46+
GOOS=windows GOARCH=amd64 go build -a -installsuffix -i -v -o $(WINFOLDER)/$(BINARYFILE).exe -ldflags="-X main.Version=$(VERSION)"
47+
@echo ------------------Completed building Windows binary------------------
48+
darwin:
49+
@echo ------------------Building Darwin binary------------------
50+
GOOS=darwin GOARCH=amd64 go build -a -installsuffix -i -v -o $(DARWINFOLDER)/$(BINARYFILE) -ldflags="-X main.Version=$(VERSION)"
51+
@echo ------------------Completed building Darwin binary------------------
52+
alpine-linux:
53+
@echo ------------------Building Alpine-Linux binary------------------
54+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix -v -o $(ALPINEFOLDER)/$(BINARYFILE) -ldflags="-X main.Version=$(VERSION)"
55+
@echo ------------------Completed building Alpine-Linux binary------------------

cmd/init.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/coolhanddev/go-mssql-runner/pkg/config"
11+
log "github.com/sirupsen/logrus"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
// initCmd represents the init command
16+
var initCmd = &cobra.Command{
17+
Use: "init",
18+
Short: "create a mssql.conf.json file in current directory",
19+
Long: ``,
20+
Run: initProj,
21+
}
22+
23+
func initProj(cmd *cobra.Command, args []string) {
24+
log.SetFormatter(&log.TextFormatter{TimestampFormat: "01-02-2006 15:04:05", FullTimestamp: true})
25+
createConfig()
26+
}
27+
28+
func createConfig() {
29+
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
fmt.Println(dir)
34+
cfgFile := config.PrjConfig{
35+
Name: "place project name here",
36+
Description: "place description here",
37+
Type: "place project type here",
38+
Version: "place version here",
39+
Scripts: config.CfgScripts{
40+
Schema: []string{"schema-example-1.sql"},
41+
Process: []string{"process-example-1.sql"},
42+
},
43+
}
44+
b, err := json.MarshalIndent(cfgFile, "", " ")
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
err = ioutil.WriteFile("mssqlrun.conf.json", b, 0666)
49+
if err != nil {
50+
log.Fatal(err)
51+
}
52+
log.Info("mssqlrun.conf.json created.")
53+
fmt.Println(string(b))
54+
}
55+
func init() {
56+
RootCmd.AddCommand(initCmd)
57+
58+
// Here you will define your flags and configuration settings.
59+
60+
// Cobra supports Persistent Flags which will work for this command
61+
// and all subcommands, e.g.:
62+
// initCmd.PersistentFlags().String("foo", "", "A help for foo")
63+
64+
// Cobra supports local flags which will only run when this command
65+
// is called directly, e.g.:
66+
// initCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
67+
}

cmd/root.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
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-
151
package cmd
162

173
import (

cmd/start.go

Lines changed: 85 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
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-
151
package cmd
162

173
import (
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
3924
var cn config.MssqlCn
4025
var logToFile string
4126
var logFileName os.File
27+
var logFormat string
28+
var encryptCn bool
4229

4330
// startCmd represents the start command
4431
var startCmd = &cobra.Command{
@@ -83,63 +70,90 @@ Different log levels can be set via the -l flag.
8370
63 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+
}
143157
func 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
}

cmd/version.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
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-
151
package cmd
162

173
import (
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)