Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Add hashicorp/go-hclog for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
zikani03 committed Jun 16, 2019
1 parent 2c46fd7 commit 731fb66
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
13 changes: 13 additions & 0 deletions cmd/pg_reload.go
Expand Up @@ -2,15 +2,18 @@ package cmd

import (
"fmt"
"github.com/hashicorp/go-hclog"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/zikani03/pg_reloaded/pg_reloaded"
"os"
"path"
)

var cfgFile string
var config = &pg_reloaded.Config{}
var logger hclog.Logger
var psqlPath string
var logFile string

Expand All @@ -26,6 +29,7 @@ func init() {
}

func initConfig() {
var home string
// Don't forget to read config either from cfgFile or from home directory!
if cfgFile != "" {
// Use config file from the flag.
Expand Down Expand Up @@ -53,6 +57,15 @@ func initConfig() {
fmt.Println("Can't read config:", err)
os.Exit(1)
}

logpath := path.Join(home, "pg_reloaded.log")
if config.LogPath != "" {
logpath = path.Join(config.LogPath, "pg_reloaded.log")
}
logger = hclog.New(&hclog.LoggerOptions{
Name: logpath,
Level: hclog.LevelFromString("DEBUG"),
})
}

var rootCmd = &cobra.Command{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -2,6 +2,7 @@ module github.com/zikani03/pg_reloaded

require (
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f // indirect
github.com/hashicorp/go-hclog v0.9.2
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -40,6 +40,8 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 h1:UDMh68UUwekSh5iP2OMhRRZJiiBccgV7axzUG8vi56c=
Expand Down
53 changes: 27 additions & 26 deletions pg_reloaded/commands.go
Expand Up @@ -3,6 +3,7 @@ package pg_reloaded
import (
"errors"
"fmt"
"github.com/hashicorp/go-hclog"
"os"
"os/exec"
"path"
Expand All @@ -17,15 +18,15 @@ func RunRestoreDatabase(psqlDir, username, database, host string, port int, file
cmd := exec.Command(command(psqlDir, "psql"), args...)
// cmd.Dir = db.Source.GetDir()
cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", password))
// appLogger.Debug("Dropping database.",
// "username", username,
// "database", database)
hclog.Default().Debug("Restoring database.",
"username", username,
"database", database)
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
if err != nil || !cmd.ProcessState.Success() {
// appLogger.Error("Failed to run 'pg_restore'.",
// "error", err,
// "output", string(output))
hclog.Default().Error("Failed to run 'pg_restore'.",
"error", err,
"output", string(output))
return err
}
return RunPsql(psqlDir, username, database, host, port, file, password)
Expand All @@ -39,17 +40,17 @@ func RunDropDatabase(psqlDir, username, database, host string, port int, passwor
args := dropDatabaseArgs(username, database, host, port)
fmt.Println("Running", command(psqlDir, "psql"), args)
cmd := exec.Command(command(psqlDir, "psql"), args...)
// cmd.Dir = db.Source.GetDir()

cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", password))
// appLogger.Debug("Dropping database.",
// "username", username,
// "database", database)
hclog.Default().Debug("Dropping database.",
"username", username,
"database", database)
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
if err != nil || !cmd.ProcessState.Success() {
// appLogger.Error("Failed to run 'pg_restore'.",
// "error", err,
// "output", string(output))
hclog.Default().Error("Failed to run 'pg_restore'.",
"error", err,
"output", string(output))
return err
}

Expand All @@ -61,17 +62,17 @@ func RunPgRestore(psqlDir, username, database, host string, port int, file, pass
args := append(psqlArgs(username, database, host, port), file)
fmt.Println("Running", command(psqlDir, "pg_restore"), args)
cmd := exec.Command(command(psqlDir, "pg_restore"), args...)
// cmd.Dir = db.Source.GetDir()

cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", password))
// appLogger.Info("Running restore via pg_restore.",
// "database", database,
// "file", file,
// "username", username)
hclog.Default().Debug("Running restore via pg_restore.",
"database", database,
"file", file,
"username", username)
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
if err != nil || !cmd.ProcessState.Success() {
// appLogger.Error("Failed to run 'pg_restore'.",
// "error", err, "output", string(output))
hclog.Default().Error("Failed to run 'pg_restore'.",
"error", err, "output", string(output))
return err
}

Expand All @@ -85,15 +86,15 @@ func RunPsql(psqlDir, username, database, host string, port int, file, password
cmd := exec.Command(command(psqlDir, "psql"), args...)
// cmd.Dir = db.Source.GetDir()
cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", password))
// appLogger.Info("Running restore via psql.",
// "database", database,
// "file", file,
// "username", username)
hclog.Default().Debug("Running restore via psql.",
"database", database,
"file", file,
"username", username)
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
if err != nil || !cmd.ProcessState.Success() {
// appLogger.Error("Failed to run 'psql'.",
// "error", err, "output", string(output))
hclog.Default().Error("Failed to run 'psql'.",
"error", err, "output", string(output))
return err
}

Expand Down

0 comments on commit 731fb66

Please sign in to comment.