Skip to content

Commit

Permalink
Fix log (#21)
Browse files Browse the repository at this point in the history
* Fix log

* Add test

* bugfix
  • Loading branch information
ginokent authored Dec 1, 2020
1 parent d2c550d commit dd3bdaa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ROOT_DIR := $(shell git rev-parse --show-toplevel)
MAIN_DIR := ${ROOT_DIR}
COVERAGE_FILE := ${ROOT_DIR}/coverage.txt
COVERAGE_HTML := ${ROOT_DIR}/coverage.html
TEST_CMD := go test -v -race -cover -coverprofile=${COVERAGE_FILE} ./...
TEST_CMD := GOTEST=true go test -v -race -cover -coverprofile=${COVERAGE_FILE} ./...

OPEN_CMD := $(shell if command -v explorer.exe 1>/dev/null; then echo "explorer.exe"; elif uname -s | grep -q Darwin; then echo "open"; else echo "echo"; fi)

Expand Down
32 changes: 26 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func main() {
ctx := context.Background()

if err := Run(ctx); err != nil {
log.Fatalf("Run: %v\n", err)
errorln("Run: " + err.Error())
exit(1)
}
}

Expand Down Expand Up @@ -96,7 +97,7 @@ func Run(ctx context.Context) error {
}
defer func() {
if err := client.Close(); err != nil {
log.Printf("client.Close: %v\n", err)
warnln("client.Close: " + err.Error())
}
}()

Expand Down Expand Up @@ -133,7 +134,7 @@ package bqtableschema
for _, table := range tables {
structCode, pkgs, err := generateTableSchemaCode(ctx, table)
if err != nil {
log.Printf("generateTableSchemaCode: %v\n", err)
warnln("generateTableSchemaCode: " + err.Error())
continue
}

Expand Down Expand Up @@ -267,18 +268,18 @@ func getOptOrEnvOrDefault(optName, optValue, envName, defaultValue string) (stri
}

if optValue != "" {
log.Println("use option value: -" + optName + "=" + optValue)
infoln("use option value: -" + optName + "=" + optValue)
return optValue, nil
}

envValue := os.Getenv(envName)
if envValue != "" {
log.Println("use environment variable: " + envName + "=" + envValue)
infoln("use environment variable: " + envName + "=" + envValue)
return envValue, nil
}

if defaultValue != "" {
log.Println("use default option value: -" + optName + "=" + defaultValue)
infoln("use default option value: -" + optName + "=" + defaultValue)
return defaultValue, nil
}

Expand All @@ -292,6 +293,25 @@ func capitalizeInitial(s string) (capitalized string) {
return strings.ToUpper(s[:1]) + s[1:]
}

func infoln(content string) {
log.Println("INFO: " + content)
}

func warnln(content string) {
log.Println("WARNING: " + content)
}

func errorln(content string) {
log.Println("ERROR: " + content)
}

func exit(code int) {
if os.Getenv("GOTEST") == "true" {
return
}
os.Exit(code)
}

// NOTE(djeeno): ref. https://github.com/googleapis/google-cloud-go/blob/f37f118c87d4d0a77a554515a430ae06e5852294/bigquery/schema.go#L216
var typeOfByteSlice = reflect.TypeOf([]byte{})

Expand Down
37 changes: 34 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,10 @@ func Test_getAllTables_OK(t *testing.T) {

func Test_getAllTables_NG(t *testing.T) {
var backupValue string

v, exist := os.LookupEnv(envNameGoogleApplicationCredentials)
if exist {
backupValue = v
}

_ = os.Setenv(envNameGoogleApplicationCredentials, testGoogleApplicationCredentials)

var (
Expand All @@ -263,7 +261,6 @@ func Test_getAllTables_NG(t *testing.T) {
_ = os.Setenv(envNameGoogleApplicationCredentials, backupValue)
return
}

_ = os.Unsetenv(envNameGoogleApplicationCredentials)
}

Expand Down Expand Up @@ -351,6 +348,40 @@ func Test_capitalizeInitial(t *testing.T) {
}
}

func Test_infoln(t *testing.T) {
infoln("test")
}

func Test_warnln(t *testing.T) {
warnln("test")
}

func Test_errorln(t *testing.T) {
errorln("test")
}

func Test_exit(t *testing.T) {
var (
envNameGoTest = "GOTEST"
envValueGoTest = "true"
)

var backupValue string
v, exist := os.LookupEnv(envNameGoTest)
if exist {
backupValue = v
}
_ = os.Setenv(envNameGoTest, envValueGoTest)

exit(1)

if exist {
_ = os.Setenv(envNameGoTest, backupValue)
return
}
_ = os.Unsetenv(envNameGoTest)
}

func Test_bigqueryFieldTypeToGoType(t *testing.T) {
var (
supportedBigqueryFieldTypes = map[bigquery.FieldType]string{
Expand Down

0 comments on commit dd3bdaa

Please sign in to comment.