github.com/dennys-bd/goals@v0.0.0-20210328114421-251a004d41e3/core/project.go (about)

     1  package core
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/BurntSushi/toml"
     9  	errs "github.com/dennys-bd/goals/shortcuts/errors"
    10  	"github.com/rs/cors"
    11  )
    12  
    13  // Project model
    14  type Project struct {
    15  	AbsPath    string `toml:"abs_path"`
    16  	ImportPath string `toml:"import_path"`
    17  	Name       string
    18  	GoVersion  string `toml:"go_version"`
    19  	AppMode    string `toml:"app_mode"`
    20  	Config     config `toml:"config"`
    21  }
    22  
    23  type config struct {
    24  	Port     int  `toml:"port"`
    25  	Graphiql bool `toml:"graphilql"`
    26  	verbose  bool `toml:"-"`
    27  }
    28  
    29  //ResolverPath is the path to package resolver
    30  func (p Project) ResolverPath() string {
    31  	if p.AbsPath == "" {
    32  		return ""
    33  	}
    34  	return filepath.Join(p.AbsPath, "app/resolver")
    35  }
    36  
    37  //ScalarPath is the path to package scalar
    38  func (p Project) ScalarPath() string {
    39  	if p.AbsPath == "" {
    40  		return ""
    41  	}
    42  	return filepath.Join(p.AbsPath, "app/scalar")
    43  }
    44  
    45  //ModelPath is the path to package model
    46  func (p Project) ModelPath() string {
    47  	if p.AbsPath == "" {
    48  		return ""
    49  	}
    50  	return filepath.Join(p.AbsPath, "app/model")
    51  }
    52  
    53  //SchemaPath is the path to package schema
    54  func (p Project) SchemaPath() string {
    55  	if p.AbsPath == "" {
    56  		return ""
    57  	}
    58  	return filepath.Join(p.AbsPath, "app/schema")
    59  }
    60  
    61  //LibPath is the path to package lib
    62  func (p Project) LibPath() string {
    63  	if p.AbsPath == "" {
    64  		return ""
    65  	}
    66  	return filepath.Join(p.AbsPath, "lib")
    67  }
    68  
    69  //ConfigPath is the path to package lib
    70  func (p Project) ConfigPath() string {
    71  	if p.AbsPath == "" {
    72  		return ""
    73  	}
    74  	return filepath.Join(p.AbsPath, "config")
    75  }
    76  
    77  func (p Project) LoadDotEnv() {
    78  	loadDotEnv(p)
    79  }
    80  
    81  type goalsToml struct {
    82  	Project Project
    83  	Cors    *cors.Options
    84  }
    85  
    86  func recreateProjectFromGoals() (Project, *cors.Options) {
    87  	wd, err := os.Getwd()
    88  	errs.CheckEx(err)
    89  
    90  	data, err := ioutil.ReadFile(filepath.Join(wd, "config/Goals.toml"))
    91  	if err != nil {
    92  		errs.Ex("This is not a goals project")
    93  	}
    94  
    95  	p, c, err := recreateProject(string(data))
    96  	errs.CheckEx(err)
    97  
    98  	p.AbsPath = wd
    99  
   100  	return p, c
   101  }
   102  
   103  func recreateProject(projectString string) (Project, *cors.Options, error) {
   104  	var m goalsToml
   105  	_, err := toml.Decode(projectString, &m)
   106  	return m.Project, m.Cors, err
   107  }