Skip to content

Commit

Permalink
feat: export projectconfig for ext use
Browse files Browse the repository at this point in the history
  • Loading branch information
jrschumacher committed Sep 9, 2024
1 parent 498a668 commit 130bcb4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 42 deletions.
7 changes: 3 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions handleprivaterepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"path"
"reflect"

"github.com/jrschumacher/doctl-serverless-go/pkg/projectconfig"
)

const ProjectCfgFile = "project"
Expand Down Expand Up @@ -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)))
}

Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions pkg/projectconfig/main.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 10 additions & 1 deletion types.go → pkg/projectconfig/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package projectconfig

type TriggerType string

Expand Down Expand Up @@ -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
}
23 changes: 0 additions & 23 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package main

import (
"errors"
"os"
"path"
"strings"

"gopkg.in/yaml.v3"
)

func pkgPrefix(s ...string) func(string) string {
Expand All @@ -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
}
9 changes: 0 additions & 9 deletions whisk.go

This file was deleted.

0 comments on commit 130bcb4

Please sign in to comment.