2022-05-06 23:41:52 +02:00
|
|
|
//go:build mage
|
|
|
|
// +build mage
|
2020-07-05 01:02:57 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-06 23:41:52 +02:00
|
|
|
"io"
|
2020-07-16 01:20:08 +02:00
|
|
|
"net/http"
|
2020-07-05 01:02:57 +02:00
|
|
|
"os"
|
2020-07-17 05:58:46 +02:00
|
|
|
|
2022-05-06 23:41:52 +02:00
|
|
|
"github.com/jordanknott/taskcafe/internal/config"
|
2020-07-17 05:58:46 +02:00
|
|
|
"github.com/magefile/mage/mg"
|
|
|
|
"github.com/magefile/mage/sh"
|
2022-05-06 23:41:52 +02:00
|
|
|
"github.com/spf13/viper"
|
2020-07-05 01:02:57 +02:00
|
|
|
)
|
|
|
|
|
2020-12-30 02:37:14 +01:00
|
|
|
const (
|
2022-05-06 23:41:52 +02:00
|
|
|
SqlcTag = "v1.11.0"
|
|
|
|
SqlcUrl = "https://github.com/kyleconroy/sqlc"
|
|
|
|
GqlgenTag = "v0.16.0"
|
|
|
|
GqlgenUrl = "https://github.com/99designs/gqlgen"
|
2020-12-30 02:37:14 +01:00
|
|
|
)
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
var Aliases = map[string]interface{}{
|
2022-05-06 23:41:52 +02:00
|
|
|
"dev:sql": Dev.SqlCompile,
|
2020-07-19 23:10:07 +02:00
|
|
|
}
|
|
|
|
|
2022-05-06 23:41:52 +02:00
|
|
|
type Dev mg.Namespace
|
2020-07-16 01:20:08 +02:00
|
|
|
|
2022-05-06 23:41:52 +02:00
|
|
|
func init() {
|
|
|
|
viper.AddConfigPath("./conf")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
viper.AddConfigPath("/etc/taskcafe")
|
|
|
|
viper.SetConfigName("taskcafe")
|
|
|
|
config.InitDefaults()
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err == nil {
|
|
|
|
return
|
2020-12-30 02:37:14 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-06 23:41:52 +02:00
|
|
|
func (Dev) Pgcli() error {
|
|
|
|
config, err := config.GetAppConfig()
|
2020-07-05 01:02:57 +02:00
|
|
|
if err != nil {
|
2022-05-06 23:41:52 +02:00
|
|
|
return err
|
2020-07-05 01:02:57 +02:00
|
|
|
}
|
2022-05-06 23:41:52 +02:00
|
|
|
return sh.RunV("pgcli", config.Database.GetDatabaseStandardUri())
|
2020-07-18 22:34:33 +02:00
|
|
|
}
|
2020-07-19 00:28:25 +02:00
|
|
|
|
2022-05-06 23:41:52 +02:00
|
|
|
func (Dev) SqlCompile() error {
|
|
|
|
return sh.RunV("./scripts/sqlc", "generate")
|
2020-07-17 05:58:46 +02:00
|
|
|
}
|
2020-07-19 00:28:25 +02:00
|
|
|
|
2022-05-06 23:41:52 +02:00
|
|
|
func (Dev) InstallScripts() error {
|
|
|
|
err := os.Mkdir("scripts", os.FileMode(0755))
|
2021-09-13 20:07:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-06 23:41:52 +02:00
|
|
|
err = sh.RunV("git", "clone", "-b", SqlcTag, "--depth", "1", SqlcUrl, "scripts/sqlc-repo")
|
2021-09-13 20:07:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-06 23:41:52 +02:00
|
|
|
os.Chdir("scripts/sqlc-repo")
|
|
|
|
err = sh.RunV("go", "build", "-o", "../sqlc", "cmd/sqlc/main.go")
|
2021-09-13 20:07:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-06 23:41:52 +02:00
|
|
|
os.Chdir("../..")
|
|
|
|
err = sh.RunV("git", "clone", "-b", GqlgenTag, "--depth", "1", GqlgenUrl, "scripts/gqlgen-repo")
|
2021-09-13 20:07:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-06 23:41:52 +02:00
|
|
|
os.Chdir("scripts/gqlgen-repo")
|
|
|
|
err = sh.RunV("go", "build", "-o", "../gqlgen", "main.go")
|
2021-09-13 20:07:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-06 23:41:52 +02:00
|
|
|
_ = sh.Rm("scripts/sqlc-repo")
|
|
|
|
_ = sh.Rm("scripts/gqlgen-repo")
|
2021-09-13 20:07:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:41:52 +02:00
|
|
|
func downloadFile(url string, filepath string) error {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-30 02:37:14 +01:00
|
|
|
}
|
2022-05-06 23:41:52 +02:00
|
|
|
defer resp.Body.Close()
|
|
|
|
out, err := os.Create(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
return err
|
2020-12-30 02:37:14 +01:00
|
|
|
}
|