47 lines
732 B
Go
47 lines
732 B
Go
package importer
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
"sh.org.ru/pkg/config"
|
|
"sh.org.ru/pkg/db"
|
|
"sh.org.ru/pkg/model"
|
|
)
|
|
|
|
func Run(c *cli.Context) error {
|
|
configFile := c.String("config")
|
|
cfg, err := config.New(configFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
db := db.New(cfg.DB)
|
|
|
|
file := c.Args().First()
|
|
|
|
quotes := []string{}
|
|
|
|
fp, err := os.Open(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer fp.Close()
|
|
|
|
if err := json.NewDecoder(fp).Decode("es); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, text := range quotes {
|
|
q := &model.Quote{
|
|
Quote: text,
|
|
Approved: true,
|
|
Archive: true,
|
|
}
|
|
if _, err := db.NewInsert().Model(q).Exec(c.Context); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|