Refactoring project to be more similar to other go project.

This commit is contained in:
Renan DelValle 2020-05-07 10:09:45 -07:00
parent bbccabcd27
commit d7db155d88
No known key found for this signature in database
GPG key ID: C240AD6D6F443EC9
10 changed files with 153 additions and 192 deletions

View file

@ -15,13 +15,10 @@
package cmd
import (
"os"
"strings"
yaml "gopkg.in/yaml.v2"
"github.com/aurora-scheduler/australis/internal"
realis "github.com/aurora-scheduler/gorealis/v2"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -36,101 +33,9 @@ var createCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
}
type URI struct {
URI string `yaml:"uri"`
Extract bool `yaml:"extract"`
Cache bool `yaml:"cache"`
}
type Executor struct {
Name string `yaml:"name"`
Data string `yaml:"data"`
}
type ThermosProcess struct {
Name string `yaml:"name"`
Cmd string `yaml:"cmd"`
}
type DockerContainer struct {
Name string `yaml:"name"`
Tag string `yaml:"tag"`
}
type Container struct {
Docker *DockerContainer `yaml:"docker"`
}
type Job struct {
Environment string `yaml:"environment"`
Role string `yaml:"role"`
Name string `yaml:"name"`
CPU float64 `yaml:"cpu"`
RAM int64 `yaml:"ram"`
Disk int64 `yaml:"disk"`
Executor Executor `yaml:"executor"`
Instances int32 `yaml:"instances"`
URIs []URI `yaml:"uris"`
Metadata map[string]string `yaml:"labels"`
Service bool `yaml:"service"`
Thermos []ThermosProcess `yaml:",flow,omitempty"`
Container *Container `yaml:"container,omitempty"`
}
func (j *Job) Validate() bool {
if j.Name == "" {
return false
}
if j.Role == "" {
return false
}
if j.Environment == "" {
return false
}
if j.Instances <= 0 {
return false
}
if j.CPU <= 0.0 {
return false
}
if j.RAM <= 0 {
return false
}
if j.Disk <= 0 {
return false
}
return true
}
func unmarshalJob(filename string) (Job, error) {
job := Job{}
if jobsFile, err := os.Open(filename); err != nil {
return job, errors.Wrap(err, "unable to read the job config file")
} else {
if err := yaml.NewDecoder(jobsFile).Decode(&job); err != nil {
return job, errors.Wrap(err, "unable to parse job config file")
}
if !job.Validate() {
return job, errors.New("invalid job config")
}
}
return job, nil
}
func createJob(cmd *cobra.Command, args []string) {
job, err := unmarshalJob(args[0])
job, err := internal.UnmarshalJob(args[0])
if err != nil {
log.Fatalln(err)