Skip to content

Commit

Permalink
feat: v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Golino committed Sep 2, 2023
1 parent 681e8dc commit 384e880
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export PROJECT_ID=PROJECT_ID
export GCS_BUCKET_NAME=GCS_BUCKET_NAME

export PSQL_HOST=PSQL_HOST
export PSQL_PORT=PSQL_PORT
export PSQL_DBNAME=PSQL_DBNAME
export PSQL_USER=PSQL_USER
export PSQL_PASSWORD=PSQL_PASSWORD

source ./.envrc
17 changes: 6 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ package main

import (
"github.com/lucasgolino/psync"
"os"
"github.com/lucasgolino/psync/config"
"github.com/lucasgolino/psync/drivers/psql"
)

func main() {
var psync psync.Psync
cfg := config.Load()

f, err := os.Open("./samples/sample-10.file")
if err != nil {
panic("failed to read file")
}
defer f.Close()
driver := psql.NewDriver()
run := psync.NewRoutine(cfg, driver)

psync.Routine()
if err = psync.WriteFile("test.txt", f); err != nil {
panic("failed to write file")
}
run.Run()
}
4 changes: 2 additions & 2 deletions config.go → config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package psync
package config

import (
"os"
Expand All @@ -10,7 +10,7 @@ type Config struct {
Bucket string
}

func LoadConfig() Config {
func Load() Config {
config := Config{
ProjectID: os.Getenv("GCS_PROJECT_ID"),
Bucket: os.Getenv("GCS_BUCKET_NAME"),
Expand Down
76 changes: 76 additions & 0 deletions drivers/psql/psql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package psql

import (
"fmt"
pg "github.com/habx/pg-commands"
"os"
"strconv"
)

type Config struct {
Host string
Port int
DBName string
User string
Password string
}

type Psql struct {
Cfg Config
}

func NewDriver() *Psql {
driver := Psql{}
driver.config()

return &driver
}

func (p *Psql) config() {
var port int
var err error
var stringPort = os.Getenv("PSQL_PORT")

if port, err = strconv.Atoi(stringPort); err != nil {
port = 5432
}

p.Cfg = Config{
Host: os.Getenv("PSQL_HOST"),
Port: port,
DBName: os.Getenv("PSQL_DBNAME"),
User: os.Getenv("PSQL_USER"),
Password: os.Getenv("PSQL_PASSWORD"),
}
}

func (p *Psql) Get() (string, error) {
dump, err := pg.NewDump(&pg.Postgres{
Host: p.Cfg.Host,
Port: p.Cfg.Port,
DB: p.Cfg.DBName,
Username: p.Cfg.User,
Password: p.Cfg.Password,
})

if err != nil {
return "", fmt.Errorf("failed to create dump connection: %w", err)
}

dir := fmt.Sprintf("%s/", os.TempDir())
dump.SetPath(dir)

dumpExec := dump.Exec(pg.ExecOptions{
StreamPrint: false,
})

if dumpExec.Error != nil {
fmt.Printf("COMMAND: %s\n", dumpExec.FullCommand)
fmt.Printf("ERROR: %s\n", dumpExec.Output)
return "", fmt.Errorf("failed to dump database: %s", dumpExec.Error.Err)
}

fmt.Printf("Successfully dumped database %s\n", dumpExec.File)

return fmt.Sprintf("%s%s", dir, dump.GetFileName()), nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
cloud.google.com/go/storage v1.32.0
github.com/joho/godotenv v1.5.1
github.com/habx/pg-commands v0.6.1
)

require (
Expand Down
17 changes: 15 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-pg/pg/v10 v10.11.0 h1:CMKJqLgTrfpE/aOVeLdybezR2om071Vh38OLZjsyMI0=
github.com/go-pg/zerochecker v0.2.0 h1:pp7f72c3DobMWOb2ErtZsnrPaSvHd2W4o9//8HtF4mU=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
Expand Down Expand Up @@ -70,12 +72,17 @@ github.com/googleapis/enterprise-certificate-proxy v0.2.5 h1:UR4rDjcgpgEnqpIEvki
github.com/googleapis/enterprise-certificate-proxy v0.2.5/go.mod h1:RxW0N9901Cko1VOCW3SXCpWP+mlIEkk2tP7jnHy9a3w=
github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56etFpas=
github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/habx/pg-commands v0.6.1 h1:+9vo6+N/usIZ5rF6jIJle5Tjvf01B09i0FPfzIvgoIg=
github.com/habx/pg-commands v0.6.1/go.mod h1:PkBR8QOJKbIjv4r1NuOFrz+LyjsbiAtmQbuu6+w0SAA=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -84,6 +91,11 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
github.com/vmihailenco/bufpool v0.1.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6czKAd94=
github.com/vmihailenco/msgpack/v5 v5.3.4 h1:qMKAwOV+meBw2Y8k9cVwAy7qErtYCwBzZ2ellBfvnqc=
github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vbd1qPqc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
Expand Down Expand Up @@ -205,3 +217,4 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
mellium.im/sasl v0.3.1 h1:wE0LW6g7U83vhvxjC1IY8DnXM+EU095yeo8XClvCdfo=
5 changes: 5 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package psync

type Driver interface {
Get() (string, error)
}
49 changes: 42 additions & 7 deletions psync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,64 @@ import (
"cloud.google.com/go/storage"
"context"
"fmt"
"github.com/lucasgolino/psync/config"
"io"
"log"
"os"
"time"
)

type Psync struct {
func NewRoutine(config config.Config, driver Driver) *Runner {
return &Runner{
Driver: driver,
Config: config,
ctx: context.Background(),
}
}

type Runner struct {
ctx context.Context
Bucket *storage.BucketHandle
}
Config config.Config

func (p *Psync) Routine() {
config := LoadConfig()
p.ctx = context.Background()
Driver Driver
}

func (p *Runner) Run() error {
client, err := storage.NewClient(p.ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()

p.Bucket = client.Bucket(config.Bucket)
p.Bucket = client.Bucket(p.Config.Bucket)

fullFilePath, err := p.Driver.Get()
if err != nil {
fmt.Printf("failed: %v", err)
return err
}

r, err := os.Open(fullFilePath)
if err != nil {
fmt.Printf("failed to openfile: %v", err)
return err
}
defer r.Close()

if err = p.SyncFile(p.genFileName(), r); err != nil {
return err
}

return nil
}

func (p *Runner) genFileName() string {
date := time.Now()
return fmt.Sprintf("psync_%d_%d_%d_%d_%d_%d.sql.tar.gz", date.Year(), date.Month(), date.Day(), date.Hour(), date.Minute(), date.Second())
}

func (p *Psync) WriteFile(filename string, reader io.Reader) error {
func (p *Runner) SyncFile(filename string, reader io.Reader) error {
var obj = p.Bucket.Object(filename)
var writer = obj.NewWriter(p.ctx)

Expand Down

0 comments on commit 384e880

Please sign in to comment.