package control_case import ( "context" "fmt" "os" "os/exec" "path/filepath" "time" "github.com/BurntSushi/toml" "github.com/otiai10/copy" "gorm.io/gorm" ) type ControlCase struct { gorm.Model Name string `json:"name"` ParamsID uint `json:"params_id"` Params Params `json:"params"` Status string `json:"status"` } type Params struct { ID uint `gorm:"primaryKey" json:"id" toml:"-" csv:"id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"` Rel float64 `json:"rel" toml:"rel" csv:"rel"` RelC float64 `json:"rel_c" toml:"rel_c" csv:"rel_c"` Le float64 `json:"le" toml:"le" csv:"le"` Pr float64 `json:"pr" toml:"pr" csv:"pr"` Pe float64 `json:"pe" toml:"pe" csv:"pe"` Ma float64 `json:"ma" toml:"ma" csv:"ma"` InitialCondition string `json:"initial_condition" toml:"-" csv:"initial_condition"` Time float64 `json:"time" toml:"time" csv:"time"` FolderPath string `json:"folder_path" toml:"-" csv:"-"` } func (p *Params) ToTOML(filename string) error { file, err := os.Create(filename) if err != nil { return err } defer file.Close() encoder := toml.NewEncoder(file) return encoder.Encode(p) } func (p *Params) Exist() (bool, error) { folderPath := filepath.Join("./", fmt.Sprintf("%d", p.ID)) return exists(folderPath) } func (p *Params) CreateFolder() error { BIN_PATH := "./bio-convect" folderPath := filepath.Join("./", fmt.Sprintf("%d", p.ID)) if err := os.MkdirAll(folderPath, os.ModePerm); err != nil { return err } fmt.Printf("folder: %s\n", folderPath) execPath := filepath.Join("./", folderPath, BIN_PATH) err := copy.Copy(BIN_PATH, execPath) if err != nil { return err } if err := os.Chmod(execPath, 0o755); err != nil { return err } if p.InitialCondition != "" && p.InitialCondition != "E" { startPath := filepath.Join("./", folderPath, "storage.h5") err = copy.Copy(p.InitialCondition, startPath) if err != nil { return err } } if err := p.ToTOML(filepath.Join(folderPath, "config.toml")); err != nil { return err } p.FolderPath = folderPath return nil } func (p *Params) Run(ctx context.Context) error { BIN_PATH := "./bio-convect" cmd := exec.CommandContext(ctx, BIN_PATH) cmd.Dir = p.FolderPath //cmd.Stdout = os.Stdout //cmd.Stderr = os.Stderr fmt.Printf("before run") err := cmd.Run() fmt.Printf("afrer run") if err != nil { return err } return nil }