feat: #6 [5] Add must and rename functions in package file

This commit is contained in:
Paul Pan 2024-01-06 17:31:00 +08:00
parent fd85b603dc
commit 7f0f897181
Signed by: Paul
GPG Key ID: D639BDF5BA578AF4
9 changed files with 48 additions and 23 deletions

View File

@ -29,10 +29,10 @@ func (h *handler) Judge(_ context.Context, t *asynq.Task) error {
// 1. write user code // 1. write user code
userCode := filepath.Join(runner.UserDir, user, fmt.Sprintf("%s.%s", user, p.Submission.Language)) userCode := filepath.Join(runner.UserDir, user, fmt.Sprintf("%s.%s", user, p.Submission.Language))
if !file.FileTouch(userCode) { if !file.Touch(userCode) {
return e.InternalError, 0, systemError return e.InternalError, 0, systemError
} }
err := file.FileWrite(userCode, []byte(p.Submission.Code)) err := file.Write(userCode, []byte(p.Submission.Code))
if err != nil { if err != nil {
return e.InternalError, 0, systemError return e.InternalError, 0, systemError
} }

View File

@ -19,7 +19,7 @@ type Service interface {
func NewService(i *do.Injector) (Service, error) { func NewService(i *do.Injector) (Service, error) {
cliCtx := do.MustInvoke[*cli.Context](i) cliCtx := do.MustInvoke[*cli.Context](i)
data, err := file.FileRead(cliCtx.String("config")) data, err := file.Read(cliCtx.String("config"))
if err != nil { if err != nil {
log.Printf("Failed to setup config: %s\n", err.Error()) log.Printf("Failed to setup config: %s\n", err.Error())
return nil, err return nil, err

View File

@ -57,10 +57,10 @@ func (s *service) checkAndExecute(version uint, user string, lang string, script
func (s *service) ProblemExists(version uint) bool { func (s *service) ProblemExists(version uint) bool {
problemPath := filepath.Join(ProblemDir, fmt.Sprintf("%d", version)) problemPath := filepath.Join(ProblemDir, fmt.Sprintf("%d", version))
return file.FileExist(problemPath) return file.Exist(problemPath)
} }
func (s *service) userExists(user string, name string) bool { func (s *service) userExists(user string, name string) bool {
userPath := filepath.Join(UserDir, user, name) userPath := filepath.Join(UserDir, user, name)
return file.FileExist(userPath) return file.Exist(userPath)
} }

View File

@ -20,7 +20,7 @@ func (s *service) Compile(version uint, user string, lang string) (JudgeStatus,
msg = utils.If(err == nil, msg, nil) msg = utils.If(err == nil, msg, nil)
msgText := string(msg) msgText := string(msg)
if !file.FileExist(target) || file.FileEmpty(target) { if !file.Exist(targetFile) || file.Empty(targetFile) {
return JudgeStatus{ return JudgeStatus{
Message: "compile failed", Message: "compile failed",
Tasks: []TaskStatus{{Verdict: VerdictCompileError, Message: msgText}}}, Tasks: []TaskStatus{{Verdict: VerdictCompileError, Message: msgText}}},

View File

@ -21,7 +21,7 @@ func (s *service) loadImage(cfg *depConfig) e.Status {
err := utils.NewTryErr(). err := utils.NewTryErr().
Try(func() error { Try(func() error {
// import from tarball // import from tarball
if !file.FileExist(cfg.tarball) { if !file.Exist(cfg.tarball) {
return errors.New("tarball not exists") return errors.New("tarball not exists")
} }
return s.execute("bash", "-c", fmt.Sprintf("gzip -d -c %s | podman load", cfg.tarball)) return s.execute("bash", "-c", fmt.Sprintf("gzip -d -c %s | podman load", cfg.tarball))
@ -32,7 +32,7 @@ func (s *service) loadImage(cfg *depConfig) e.Status {
}). }).
Or(func() error { Or(func() error {
// build from dockerfile // build from dockerfile
if !file.FileExist(cfg.dockerfile) { if !file.Exist(cfg.dockerfile) {
return errors.New("dockerfile not exists") return errors.New("dockerfile not exists")
} }
return s.execute("podman", "build", "-f", cfg.dockerfile, "-t", cfg.image, ".") return s.execute("podman", "build", "-f", cfg.dockerfile, "-t", cfg.image, ".")
@ -53,7 +53,7 @@ func (s *service) EnsureDeps(force bool) e.Status {
// check mark // check mark
if force { if force {
_ = os.Remove(mark) _ = os.Remove(mark)
} else if file.FileExist(mark) { } else if file.Exist(mark) {
return e.Success return e.Success
} }

View File

@ -39,22 +39,22 @@ func (s *service) prebuild(version uint, force bool) e.Status {
mark := filepath.Join(ProblemDir, fmt.Sprintf("%d", version), ".mark.prebuild") mark := filepath.Join(ProblemDir, fmt.Sprintf("%d", version), ".mark.prebuild")
if force { if force {
_ = os.Remove(mark) _ = os.Remove(mark)
} else if file.FileExist(mark) { } else if file.Exist(mark) {
return e.Success return e.Success
} }
prebuildScript := filepath.Join(ProblemDir, fmt.Sprintf("%d", version), "judge", "prebuild.Makefile") prebuildScript := filepath.Join(ProblemDir, fmt.Sprintf("%d", version), "judge", "prebuild.Makefile")
if !file.FileExist(prebuildScript) { if !file.Exist(prebuildScript) {
s.log.Info("prebuild script not found", zap.String("path", prebuildScript), zap.Uint("version", version)) s.log.Info("prebuild script not found", zap.String("path", prebuildScript), zap.Uint("version", version))
return e.Success return e.Success
} }
dataPath := filepath.Join(ProblemDir, fmt.Sprintf("%d", version), "data") dataDir := filepath.Join(ProblemDir, fmt.Sprintf("%d", version), "data")
judgePath := filepath.Join(ProblemDir, fmt.Sprintf("%d", version), "judge") judgeDir := filepath.Join(ProblemDir, fmt.Sprintf("%d", version), "judge")
args := []string{ args := []string{
"-v", dataPath + ":/woj/problem/data", "-v", dataDir + ":/woj/problem/data",
"-v", judgePath + ":/woj/problem/judge", "-v", judgeDir + ":/woj/problem/judge",
"-e", "PREFIX=/woj/problem", "-e", "PREFIX=/woj/problem",
"git.0x7f.app/woj/ubuntu-full:latest", "git.0x7f.app/woj/ubuntu-full:latest",
"sh", "-c", "cd /woj/problem/judge && make -f prebuild.Makefile prebuild && touch .mark.prebuild", "sh", "-c", "cd /woj/problem/judge && make -f prebuild.Makefile prebuild && touch .mark.prebuild",

View File

@ -57,7 +57,7 @@ func (t *TaskStatus) getInfoText(infoFile string) *TaskStatus {
} }
var err error var err error
t.infoText, err = file.FileRead(infoFile) t.infoText, err = file.Read(infoFile)
if err != nil { if err != nil {
t.Verdict = VerdictSystemError t.Verdict = VerdictSystemError
t.Message = "cannot read info file" t.Message = "cannot read info file"
@ -128,7 +128,7 @@ func (t *TaskStatus) getJudgeText(judgeFile string) *TaskStatus {
return t return t
} }
j, err := file.FileRead(judgeFile) j, err := file.Read(judgeFile)
if err != nil { if err != nil {
t.Verdict = VerdictSystemError t.Verdict = VerdictSystemError
t.Message = "cannot read judge file" t.Message = "cannot read judge file"

View File

@ -7,7 +7,7 @@ import (
"path/filepath" "path/filepath"
) )
func FileRead(filePath string) ([]byte, error) { func Read(filePath string) ([]byte, error) {
f, err := os.Open(filePath) f, err := os.Open(filePath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -15,16 +15,16 @@ func FileRead(filePath string) ([]byte, error) {
return io.ReadAll(f) return io.ReadAll(f)
} }
func FileWrite(filePath string, content []byte) error { func Write(filePath string, content []byte) error {
return os.WriteFile(filePath, content, 0644) return os.WriteFile(filePath, content, 0644)
} }
func FileExist(filePath string) bool { func Exist(filePath string) bool {
_, err := os.Stat(filePath) _, err := os.Stat(filePath)
return utils.If(err == nil || os.IsExist(err), true, false) return utils.If(err == nil || os.IsExist(err), true, false)
} }
func FileEmpty(filePath string) bool { func Empty(filePath string) bool {
stat, err := os.Stat(filePath) stat, err := os.Stat(filePath)
if err != nil { if err != nil {
return true return true
@ -32,9 +32,14 @@ func FileEmpty(filePath string) bool {
return stat.Size() == 0 return stat.Size() == 0
} }
func FileTouch(filePath string) bool { func Touch(filePath string) bool {
err := TouchErr(filePath)
return utils.If(err == nil, true, false)
}
func TouchErr(filePath string) error {
base := filepath.Dir(filePath) base := filepath.Dir(filePath)
_ = os.MkdirAll(base, 0755) _ = os.MkdirAll(base, 0755)
_, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0644) _, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0644)
return utils.If(err == nil, true, false) return err
} }

20
pkg/utils/must.go Normal file
View File

@ -0,0 +1,20 @@
package utils
type MustChain struct {
err error
}
func NewMust() *MustChain {
return &MustChain{}
}
func (c *MustChain) Do(callback func() error) *MustChain {
if c.err == nil {
c.err = callback()
}
return c
}
func (c *MustChain) Done() error {
return c.err
}