feat: Dodane parsowanie pliku pgpass
pgpass jest prsowany i przy uruchomieniu psql podawane jest wybrane hasło pasujące do połaczenia
This commit is contained in:
51
psql/psql.go
51
psql/psql.go
@ -10,15 +10,16 @@ import (
|
||||
"runtime"
|
||||
|
||||
"baal.ar76.eu/x/pub/multisql/cfg"
|
||||
"baal.ar76.eu/x/pub/multisql/pass"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
outdir string
|
||||
scripts []string
|
||||
passfile string
|
||||
psqlExe string
|
||||
conn cfg.Connection
|
||||
verbose bool
|
||||
outdir string
|
||||
scripts []string
|
||||
pasdb *pass.PassDb
|
||||
psqlExe string
|
||||
conn cfg.Connection
|
||||
verbose bool
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
@ -26,32 +27,22 @@ type Result struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
func Create(passfile string, outdir string, scripts []string, conn cfg.Connection, psqlExe string, verbose bool) (instnace Instance) {
|
||||
func Create(passdb *pass.PassDb, outdir string, scripts []string, conn cfg.Connection, psqlExe string, verbose bool) (instnace Instance) {
|
||||
|
||||
instnace = Instance{
|
||||
outdir: outdir,
|
||||
scripts: scripts,
|
||||
passfile: passfile,
|
||||
conn: conn,
|
||||
psqlExe: psqlExe,
|
||||
verbose: verbose,
|
||||
outdir: outdir,
|
||||
scripts: scripts,
|
||||
pasdb: passdb,
|
||||
conn: conn,
|
||||
psqlExe: psqlExe,
|
||||
verbose: verbose,
|
||||
}
|
||||
return instnace
|
||||
}
|
||||
|
||||
func (self Instance) Exec(result chan<- Result) {
|
||||
absPassFile := ""
|
||||
if self.passfile != "" {
|
||||
var err error
|
||||
absPassFile, err = filepath.Abs(self.passfile)
|
||||
if err != nil {
|
||||
result <- Result{
|
||||
Script: "*",
|
||||
Err: fmt.Errorf("problem ze zbudowaniem ściężki do pliku haseł: %w", err),
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
connString := fmt.Sprintf("host=%s port=%d dbname=%s user=%s", self.conn.Host, self.conn.Port, self.conn.DbName, self.conn.User)
|
||||
|
||||
for _, scr := range self.scripts {
|
||||
@ -65,7 +56,8 @@ func (self Instance) Exec(result chan<- Result) {
|
||||
}
|
||||
continue
|
||||
}
|
||||
err = self.cmdPsql(outdir, connString, scr, absPassFile)
|
||||
password := self.pasdb.FindPassword(self.conn.Host, self.conn.Port, self.conn.DbName, self.conn.User)
|
||||
err = self.cmdPsql(outdir, connString, scr, password)
|
||||
result <- Result{
|
||||
Script: scr,
|
||||
Err: err,
|
||||
@ -74,7 +66,7 @@ func (self Instance) Exec(result chan<- Result) {
|
||||
close(result)
|
||||
}
|
||||
|
||||
func (self Instance) cmdPsql(outdir string, connString string, script string, passfile string) error {
|
||||
func (self Instance) cmdPsql(outdir string, connString string, script string, password string) error {
|
||||
absScript, err := filepath.Abs(script)
|
||||
if err != nil {
|
||||
return fmt.Errorf("problem ze zbudowaniem ściezki do skryptu: %w", err)
|
||||
@ -112,13 +104,12 @@ func (self Instance) cmdPsql(outdir string, connString string, script string, pa
|
||||
cmd.Stdout = fhOut
|
||||
cmd.Dir = outdir
|
||||
|
||||
if passfile != "" {
|
||||
if password != "" {
|
||||
env := os.Environ()
|
||||
env = append(env, "PGPASSFILE=" + passfile)
|
||||
env = append(env, "PGPASSWORD="+password)
|
||||
cmd.Env = env
|
||||
}
|
||||
|
||||
|
||||
if self.verbose {
|
||||
log.Printf("Uruchamiam %s", cmd.String())
|
||||
}
|
||||
|
Reference in New Issue
Block a user