From f39351673dd3e1a3cf75e6460ec5c89d09f9e8d5 Mon Sep 17 00:00:00 2001 From: sboy99 Date: Thu, 9 Jan 2025 08:35:29 +0530 Subject: [PATCH] feat: storage selection and update config --- config/config.go | 5 ++- config/validator.go | 2 +- internal/config/service.go | 9 ++++++ internal/storage/file_storage.go | 52 ------------------------------- internal/storage/local_storage.go | 52 +++++++++++++++++++++++++++++++ internal/storage/storage.go | 2 +- internal/ui/ui.go | 14 +++++++++ main.go | 2 -- pkg/pg_dump/pg_dump.go | 16 ++++++++-- pkg/pg_dump/sql_util.go | 7 +++-- 10 files changed, 99 insertions(+), 62 deletions(-) delete mode 100644 internal/storage/file_storage.go create mode 100644 internal/storage/local_storage.go diff --git a/config/config.go b/config/config.go index c004f30..ae5bd00 100644 --- a/config/config.go +++ b/config/config.go @@ -40,7 +40,7 @@ const ( ) const ( - FILE StorageEnum = "file" + LOCAL StorageEnum = "local" CLOUD StorageEnum = "cloud" ) @@ -72,6 +72,9 @@ func Save(config *Config) error { viper.Set("db.port", config.DB.Port) viper.Set("db.username", config.DB.Username) viper.Set("db.password", config.DB.Password) + // Storage // + viper.Set("storage.type", config.Storage.Type) + viper.Set("storage.dest", config.Storage.Dest) if err := viper.WriteConfig(); err != nil { return err diff --git a/config/validator.go b/config/validator.go index e4f090e..a11cfca 100644 --- a/config/validator.go +++ b/config/validator.go @@ -11,7 +11,7 @@ func setDefaults() { viper.SetDefault("app.name", "go-vault") viper.SetDefault("app.version", "0.0.1") - viper.SetDefault("storage.type", "file") + viper.SetDefault("storage.type", "local") viper.SetDefault("storage.dest", "./generated") } diff --git a/internal/config/service.go b/internal/config/service.go index 6f09d0f..b26a83d 100644 --- a/internal/config/service.go +++ b/internal/config/service.go @@ -45,6 +45,12 @@ func (c *ConfigService) SetupConfig() { return } + storageType, err := ui.DisplaySelectStorageTypePrompt() + if err != nil { + logger.Error("Failed to display select storage prompt\nDetails: %v", err) + return + } + // Save config // cfg := config.GetConfig() cfg.DB.Type = dbType @@ -53,6 +59,9 @@ func (c *ConfigService) SetupConfig() { cfg.DB.Name = dbName cfg.DB.Username = dbUser cfg.DB.Password = dbPass + + cfg.Storage.Type = storageType + if err = config.Save(cfg); err != nil { logger.Error("Failed to save config\nDetails: %v", err) } diff --git a/internal/storage/file_storage.go b/internal/storage/file_storage.go deleted file mode 100644 index 2a16f28..0000000 --- a/internal/storage/file_storage.go +++ /dev/null @@ -1,52 +0,0 @@ -package storage - -import ( - "os" -) - -type FileStorage struct { - BasePath string -} - -func NewFileStorage(basePath string) *FileStorage { - return &FileStorage{BasePath: basePath} -} - -func (f *FileStorage) Save(filename string, data []byte) error { - file, err := os.Create(f.filePath(filename)) - if err != nil { - return err - } - defer file.Close() - - _, err = file.Write(data) - if err != nil { - return err - } - - return nil -} - -func (f *FileStorage) Load(filename string) ([]byte, error) { - file, err := os.Open(f.filePath(filename)) - if err != nil { - return nil, err - } - defer file.Close() - - data := make([]byte, 0) - _, err = file.Read(data) - if err != nil { - return nil, err - } - - return data, nil -} - -func (f *FileStorage) Delete(filename string) error { - return os.Remove(f.filePath(filename)) -} - -func (f *FileStorage) filePath(filename string) string { - return f.BasePath + "/" + filename -} diff --git a/internal/storage/local_storage.go b/internal/storage/local_storage.go new file mode 100644 index 0000000..5b828c8 --- /dev/null +++ b/internal/storage/local_storage.go @@ -0,0 +1,52 @@ +package storage + +import ( + "os" +) + +type LocalStorage struct { + BasePath string +} + +func NewLocalStorage(basePath string) *LocalStorage { + return &LocalStorage{BasePath: basePath} +} + +func (ls *LocalStorage) Save(filename string, data []byte) error { + file, err := os.Create(ls.filePath(filename)) + if err != nil { + return err + } + defer file.Close() + + _, err = file.Write(data) + if err != nil { + return err + } + + return nil +} + +func (ls *LocalStorage) Load(filename string) ([]byte, error) { + file, err := os.Open(ls.filePath(filename)) + if err != nil { + return nil, err + } + defer file.Close() + + data := make([]byte, 0) + _, err = file.Read(data) + if err != nil { + return nil, err + } + + return data, nil +} + +func (ls *LocalStorage) Delete(filename string) error { + return os.Remove(ls.filePath(filename)) +} + +func (ls *LocalStorage) filePath(filename string) string { + return ls.BasePath + "/" + filename +} diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 44d18c8..a1d5f39 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -16,7 +16,7 @@ type Storage struct { func NewStorage() *Storage { return &Storage{ storageMap: map[config.StorageEnum]IStorage{ - config.FILE: NewFileStorage(config.GetConfig().Storage.Dest), + config.LOCAL: NewLocalStorage(config.GetConfig().Storage.Dest), config.CLOUD: nil, }, } diff --git a/internal/ui/ui.go b/internal/ui/ui.go index 40bda47..1c4138a 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -93,3 +93,17 @@ func DisplayInputDatabasePasswordPrompt() (string, error) { } return result, nil } + +func DisplaySelectStorageTypePrompt() (config.StorageEnum, error) { + // Prompt for Storage selection // + prompt := promptui.Select{ + Label: "Select Storage", + Items: []config.StorageEnum{config.LOCAL, config.CLOUD}, + } + // Run the prompt // + _, result, err := prompt.Run() + if err != nil { + return "", err + } + return config.StorageEnum(result), nil +} diff --git a/main.go b/main.go index b50593c..3b2d840 100644 --- a/main.go +++ b/main.go @@ -12,8 +12,6 @@ func main() { // Logger logger.Init(logger.DEBUG) - logger.Info("args %v", os.Args) - // User Config // if len(os.Args) > 1 && os.Args[1] != "setup" { if err := config.Load(); err != nil { diff --git a/pkg/pg_dump/pg_dump.go b/pkg/pg_dump/pg_dump.go index 9b2c4ec..9f9a7ba 100644 --- a/pkg/pg_dump/pg_dump.go +++ b/pkg/pg_dump/pg_dump.go @@ -5,6 +5,7 @@ import ( "database/sql" "fmt" "strings" + "time" _ "github.com/lib/pq" ) @@ -36,8 +37,16 @@ func (p *PgDump) Dump() ([]byte, error) { } var dumpSql bytes.Buffer - dumpSql.WriteString(makeSqlComment("PostgreSQL database dump")) - dumpSql.WriteString(makeSqlComment(fmt.Sprintf("Database version: %s", dbVersion))) + dumpSql.WriteString(makeSqlComment( + "PostgreSQL database dump" + + "\n" + + fmt.Sprintf("Database version: %s", dbVersion) + + "\n" + + "Generated by go-vault" + + "\n" + + fmt.Sprintf("Generated at: %s", time.Now().Format(time.RFC3339)), + )) + dumpSql.WriteString("\n") dumpSql.WriteString("SET statement_timeout = 0;\n") dumpSql.WriteString("SET lock_timeout = 0;\n") dumpSql.WriteString("SET client_encoding = 'UTF8';\n") @@ -181,8 +190,9 @@ func (p *PgDump) generateCreateTableStatementsForTables(tables []string) (string continue } dumpSql.WriteString(createTableStatement) - dumpSql.WriteString("\n\n") + dumpSql.WriteString("\n") } + dumpSql.WriteString("\n") return dumpSql.String(), nil } diff --git a/pkg/pg_dump/sql_util.go b/pkg/pg_dump/sql_util.go index a65328a..5433730 100644 --- a/pkg/pg_dump/sql_util.go +++ b/pkg/pg_dump/sql_util.go @@ -1,9 +1,12 @@ package pgdump -import "fmt" +import ( + "fmt" + "strings" +) func makeSqlComment(comment string) string { - return fmt.Sprintf("--\n-- %s\n--\n", comment) + return fmt.Sprintf("--\n-- %s\n--\n", strings.Replace(comment, "\n", "\n-- ", -1)) } func getListSchemasQuery() string {