Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI option -mocks-directory #214

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func parseConfig() (c config.Config) {
flag.StringVar(&c.StaticFiles, "static-files", ".", "The location of the static files to serve (index.html, etc.)")
flag.IntVar(&c.HistoryMaxRetention, "history-retention", 0, "The maximum number of calls to keep in the history by sessions (0 = infinity)")
flag.StringVar(&c.PersistenceDirectory, "persistence-directory", "", "If defined, the directory where the sessions will be synchronized")
flag.StringVar(&c.MocksDirectory, "mocks-directory", "", "If defined, the directory use to load the mocks at server start")
flag.Parse()
return
}
Expand Down
9 changes: 9 additions & 0 deletions server/admin_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ func Serve(config config.Config) {
adminServerEngine.Use(recoverMiddleware(), loggerMiddleware(), middleware.Gzip())

mockServerEngine, mockServices := NewMockServer(config)

// Autoload mocks at start
if config.MocksDirectory != "" {
log.WithField("port", config.ConfigListenPort).Infof("Loading mocks from %s", config.MocksDirectory)
if err := mockServices.Load(config.MocksDirectory); err != nil {
log.WithField("port", config.ConfigListenPort).Fatal(err)
}
}

graphServices := services.NewGraph()
handler := handlers.NewAdmin(mockServices, graphServices)

Expand Down
1 change: 1 addition & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Config struct {
StaticFiles string
HistoryMaxRetention int
PersistenceDirectory string
MocksDirectory string
Build Build
}

Expand Down
61 changes: 61 additions & 0 deletions server/services/mocks.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package services

import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"sync"
"time"

"github.com/Thiht/smocker/server/types"
"github.com/teris-io/shortid"
"gopkg.in/yaml.v3"
)

var (
Expand All @@ -32,6 +36,7 @@ type Mocks interface {
GetSessions() types.Sessions
SetSessions(sessions types.Sessions)
Reset(force bool)
Load(path string) error
}

type mocks struct {
Expand Down Expand Up @@ -315,3 +320,59 @@ func (s *mocks) Reset(force bool) {

go s.persistence.StoreSessions(s.sessions.Clone())
}

func (s *mocks) Load(path string) error {
mockslist := make(map[string][]types.Mock)

files, err := ioutil.ReadDir(path)
if err != nil {
return fmt.Errorf("unable to find mocks files: %w ", err)
}

for _, file := range files {
ext := filepath.Ext(file.Name())

if ext != ".yml" && ext != ".yaml" && ext != ".json" {
continue
}

content, err := ioutil.ReadFile(filepath.Join(path, file.Name()))
if err != nil {
return fmt.Errorf("unable to read mock file %s: %w ", file.Name(), err)
}

var Ms []types.Mock

switch ext {
case ".yml", ".yaml":
err = yaml.Unmarshal(content, &Ms)
case ".json":
err = json.Unmarshal(content, &Ms)
}

if err != nil {
return fmt.Errorf("unable to parse mock file %s: %w ", file.Name(), err)
}

for _, mock := range Ms {
if err = mock.Validate(); err != nil {
return fmt.Errorf("unvalid mock file %s: %w ", file.Name(), err)
}
}

mockslist[file.Name()] = Ms
}

sessionID := s.GetLastSession().ID

for fileName, mocks := range mockslist {
for _, mock := range mocks {
m := mock
if _, err = s.AddMock(sessionID, &m); err != nil {
return fmt.Errorf("unable to add mock file %s: %w ", fileName, err)
}
}
}

return nil
}