omq/main.go
2023-11-17 20:34:20 +05:00

95 lines
2.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"github.com/rivo/tview"
)
func init() {
logger := slog.New(&Handler{os.Stderr, slog.LevelInfo})
slog.SetDefault(logger)
}
func main() {
configFilePath, scriptFilePath, exportFileFormat, encoding, err := getReportParams()
if err != nil {
log.Fatalln(err)
}
err = launch(configFilePath, scriptFilePath, exportFileFormat, encoding)
if err != nil {
slog.Error("Ошибка при выполнении", slog.Any("err", err))
fmt.Scanln()
return
}
}
func getReportParams() (configFilePath, scriptFilePath string, exportFileFormat ExportFormat, encoding Encoding, err error) {
exportFileFormatStr := ""
// Список файлов с SQL-скриптами
files, _ := filepath.Glob(filepath.Join(SQL_FILES_DIR, "*.sql"))
for i := range files {
files[i] = filepath.Base(files[i])
}
// Список файлов с настройками подключения к БД
configs, _ := filepath.Glob("*.ini")
configFileDropDown := tview.NewDropDown().
SetOptions(configs, nil).
SetLabel("Файл настроек серверов").
SetCurrentOption(0)
sqlFileDropDown := tview.NewDropDown().
SetOptions(files, nil).
SetLabel("Файл SQL-скрипта").
SetCurrentOption(0)
encodingDropDown := tview.NewDropDown().
SetOptions([]string{string(EncodingWin1251), string(EncodingUtf8)}, nil).
SetLabel("Кодировка CSV-файла")
encodingDropDown.SetDisabled(true)
encodingDropDown.SetCurrentOption(0)
formatDropDown := tview.NewDropDown().
SetOptions([]string{string(ExportFormatExcel), string(ExportFormatCsv)}, func(text string, index int) {
if text == string(ExportFormatCsv) {
encodingDropDown.SetDisabled(false)
return
}
encodingDropDown.SetDisabled(true)
}).
SetLabel("Формат выгрузки").SetCurrentOption(0)
form := tview.NewForm().
AddFormItem(configFileDropDown).
AddFormItem(sqlFileDropDown).
AddFormItem(formatDropDown).
AddFormItem(encodingDropDown)
form.SetTitle("Параметры").SetBorder(true)
app := tview.NewApplication()
grid := tview.NewGrid().SetRows(-1, 1).
AddItem(form, 0, 0, 1, 1, 0, 0, true).
AddItem(tview.NewButton("Запуск").SetSelectedFunc(func() {
_, scriptFilePath = sqlFileDropDown.GetCurrentOption()
_, exportFileFormatStr = formatDropDown.GetCurrentOption()
_, configFilePath = configFileDropDown.GetCurrentOption()
_, encodingStr := encodingDropDown.GetCurrentOption()
encoding = Encoding(encodingStr)
app.Stop()
}), 1, 0, 1, 1, 0, 0, false)
grid.SetBorderPadding(0, 1, 1, 1)
form.SetTitle("Параметры выгрузки").SetTitleAlign(tview.AlignLeft)
if err := app.SetRoot(grid, true).EnableMouse(true).Run(); err != nil {
return "", "", "", "", err
}
return configFilePath, filepath.Join(SQL_FILES_DIR, scriptFilePath), ExportFormat(exportFileFormatStr), encoding, nil
}