taskcafe/magefile.go

96 lines
1.9 KiB
Go
Raw Normal View History

2022-05-06 23:41:52 +02:00
//go:build mage
// +build mage
package main
import (
2022-05-06 23:41:52 +02:00
"io"
2020-07-16 01:20:08 +02:00
"net/http"
"os"
2022-05-06 23:41:52 +02:00
"github.com/jordanknott/taskcafe/internal/config"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
2022-05-06 23:41:52 +02:00
"github.com/spf13/viper"
)
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"
)
var Aliases = map[string]interface{}{
2022-05-06 23:41:52 +02:00
"dev:sql": Dev.SqlCompile,
}
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
}
}
2022-05-06 23:41:52 +02:00
func (Dev) Pgcli() error {
config, err := config.GetAppConfig()
if err != nil {
2022-05-06 23:41:52 +02:00
return err
}
2022-05-06 23:41:52 +02:00
return sh.RunV("pgcli", config.Database.GetDatabaseStandardUri())
}
2022-05-06 23:41:52 +02:00
func (Dev) SqlCompile() error {
return sh.RunV("./scripts/sqlc", "generate")
}
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
}
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
}