You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
seaweed/sea.go

88 lines
1.8 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/***
Пакет seaweed предназначен для облегчения работы с БД sea
и weed.
Текущая версия: v1.0.7
***/
package seaweed
import (
"errors"
"flag"
"fmt"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/kataras/pio"
)
// глобальная переменная для логирования
var p = pio.NewTextPrinter("color", os.Stdout)
// ============= SEA ============
type Sea struct {
db *sqlx.DB
debug bool
}
// Connect to database and connect to weed
func Connect() (*Sea, error) {
debug := flag.Bool("debug", false, "enable debug mode")
fmt.Printf("debug: %v\n", *debug)
user := os.Getenv("SEA_USER")
if user == "" {
return nil, errors.New("не установлена переменная SEA_USER")
}
password := os.Getenv("SEA_PASSWORD")
if password == "" {
return nil, errors.New("не установлена переменная SEA_PASSWORD")
}
db, err := openDB(user, password)
if err != nil {
return nil, err
}
if *debug {
p.Println(pio.Rich("БД sea успешно открыта", pio.Blue))
}
sea := Sea{db: db, debug: *debug}
return &sea, nil
}
func openDB(user, password string) (*sqlx.DB, error) {
conn := fmt.Sprintf("%s:%s@(xigmanas:3306)/sea", user, password)
db, err := sqlx.Open("mysql", conn)
if err != nil {
return nil, err
}
return db, nil
}
// ============= ALBUM ============
// Album содержит информацию об активном альбоме
type Album struct {
Seria string
Cnt int
Hash string
}
// Albums возвращает список активных альбомов,
// без учета комиксов.
func (s *Sea) Albums() ([]Album, error) {
cmd := "call getAlbums()"
var albums []Album
err := s.db.Select(&albums, cmd)
if err != nil {
return nil, err
}
return albums, nil
}