diff --git a/errors.go b/errors.go index a0542d9..e6ed3bd 100644 --- a/errors.go +++ b/errors.go @@ -5,10 +5,9 @@ import ( "log" ) -var ErrCfgNotFound = fmt.Errorf("could not find config file") -var ErrCfgParse = fmt.Errorf("error parsing config file") -var ErrParseProjectCfg = fmt.Errorf("error parsing project.yaml") -var ErrParseDeployTemplateCfg = fmt.Errorf("error parsing deploy.template.yaml") +var ErrNoProjectCfg = fmt.Errorf("no project.[yaml|yml] found") +var ErrParseProjectCfg = fmt.Errorf("error parsing project.[yaml|yml]") +var ErrParseDeployTemplateCfg = fmt.Errorf("error parsing deploy.template.[yaml|yml]") var ErrNotMonorepo = fmt.Errorf("not a digitalocean serverless monorepo") var ErrNoPackagesFound = fmt.Errorf("no packages found") diff --git a/handleprivaterepo.go b/handleprivaterepo.go index 7fdfdf4..c669cb0 100644 --- a/handleprivaterepo.go +++ b/handleprivaterepo.go @@ -10,10 +10,11 @@ import ( "path" "strings" + "github.com/jrschumacher/doctl-serverless-go/pkg/projectconfig" "golang.org/x/mod/modfile" ) -func cleanPrivateRepo(_ ProjectSpec) goPackageFunc { +func cleanPrivateRepo(_ *projectconfig.ProjectSpec) goPackageFunc { log.Print("cleaning private repos...") return func(pkgDirName, actDirName string) error { prefix := pkgPrefix(pkgDirName, actDirName) @@ -80,7 +81,7 @@ func cleanPrivateRepo(_ ProjectSpec) goPackageFunc { } } -func clonePrivateRepo(projectCfg ProjectSpec) goPackageFunc { +func clonePrivateRepo(projectCfg *projectconfig.ProjectSpec) goPackageFunc { log.Print("checking for private repos...") // check if GOPRIVATE is set in projectCfg diff --git a/main.go b/main.go index e6b3c1e..5e350ff 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "os" "path" "reflect" + + "github.com/jrschumacher/doctl-serverless-go/pkg/projectconfig" ) const ProjectCfgFile = "project" @@ -50,10 +52,23 @@ func main() { log.Fatal(errors.Join(ErrNotMonorepo, err)) } + // check project.yaml exists + var projectCfgFile string + for _, ext := range CfgExt { + f := path.Join(monorepoPath, ProjectCfgFile+ext) + if _, err := os.Stat(f); err == nil { + projectCfgFile = f + break + } + } + if projectCfgFile == "" { + log.Fatal(errors.Join(ErrNotMonorepo, errors.Join(ErrNoProjectCfg, err))) + } + // read project yaml log.Print("parsing project.yaml... ") - var projectCfg ProjectSpec - if err := parseCfg(monorepoPath, ProjectCfgFile, &projectCfg); err != nil { + projectCfg, err := projectconfig.Parse(projectCfgFile) + if err != nil { log.Fatal(errors.Join(ErrParseProjectCfg, errors.Join(ErrNotMonorepo, err))) } @@ -90,7 +105,7 @@ func exitInvalidUsage() { os.Exit(1) } -func forEveryPackage(pkgsDirName string, projectCfg ProjectSpec, fns ...goPackageFunc) errMap { +func forEveryPackage(pkgsDirName string, projectCfg *projectconfig.ProjectSpec, fns ...goPackageFunc) errMap { pkgErrs := make(map[string]error) for _, pkg := range projectCfg.Packages { scope := pkg.Name diff --git a/pkg/projectconfig/main.go b/pkg/projectconfig/main.go new file mode 100644 index 0000000..8cec774 --- /dev/null +++ b/pkg/projectconfig/main.go @@ -0,0 +1,27 @@ +package projectconfig + +import ( + "errors" + "fmt" + "os" + + "gopkg.in/yaml.v3" +) + +var ErrNotFound = fmt.Errorf("could not find config file") +var ErrParse = fmt.Errorf("error parsing config file") + +func Parse(cfgPath string) (*ProjectSpec, error) { + cfg := &ProjectSpec{} + b, err := os.ReadFile(cfgPath) + if err != nil { + return nil, errors.Join(ErrNotFound, err) + } + + // parse yaml + err = yaml.Unmarshal(b, cfg) + if err != nil { + return nil, errors.Join(ErrParse, err) + } + return cfg, nil +} diff --git a/types.go b/pkg/projectconfig/types.go similarity index 89% rename from types.go rename to pkg/projectconfig/types.go index 31a0027..cd3fd09 100644 --- a/types.go +++ b/pkg/projectconfig/types.go @@ -1,4 +1,4 @@ -package main +package projectconfig type TriggerType string @@ -60,3 +60,12 @@ type ProjectSpec struct { Parameters map[string]string `yaml:"parameters,omitempty"` // Parameters to apply to all packages in the project Environment map[string]string `yaml:"environment,omitempty"` // Environment to apply to all packages in the project } + +// Whisk Limits +// https://github.com/apache/openwhisk-client-go/blob/13fc65f65684e04f401fee67b231b370c53b3dcd/whisk/shared.go#L96-L101 +type Limits struct { + Timeout int `yaml:"timeout,omitempty"` // in seconds + Memory int `yaml:"memory,omitempty"` // in MB + Logs int `yaml:"logs,omitempty"` // in MB + Concurrency int `yaml:"concurrency,omitempty"` // number of concurrent invocations allowed +} diff --git a/util.go b/util.go index 1ef151c..147fa0d 100644 --- a/util.go +++ b/util.go @@ -1,12 +1,8 @@ package main import ( - "errors" - "os" "path" "strings" - - "gopkg.in/yaml.v3" ) func pkgPrefix(s ...string) func(string) string { @@ -18,22 +14,3 @@ func pkgPrefix(s ...string) func(string) string { return "[" + strings.Join(s, "/") + "] " + m } } - -func parseCfg(monorepoPath, cfgPath string, cfg interface{}) error { - for _, ext := range CfgExt { - b, err := os.ReadFile(path.Join(monorepoPath, cfgPath+ext)) - if err == nil { - // check if cfg is nil and return - if cfg == nil { - return nil - } - // parse yaml - err = yaml.Unmarshal(b, cfg) - if err != nil { - return errors.Join(ErrCfgParse, err) - } - return nil - } - } - return ErrCfgNotFound -} diff --git a/whisk.go b/whisk.go deleted file mode 100644 index e949e50..0000000 --- a/whisk.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -// https://github.com/apache/openwhisk-client-go/blob/13fc65f65684e04f401fee67b231b370c53b3dcd/whisk/shared.go#L96-L101 -type Limits struct { - Timeout int `yaml:"timeout,omitempty"` // in seconds - Memory int `yaml:"memory,omitempty"` // in MB - Logs int `yaml:"logs,omitempty"` // in MB - Concurrency int `yaml:"concurrency,omitempty"` // number of concurrent invocations allowed -}