Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Download swagger.yaml from s3 and git
Browse files Browse the repository at this point in the history
  • Loading branch information
Svetlin Ralchev authored and iamralch committed Nov 26, 2019
1 parent dddc936 commit 2cac88c
Show file tree
Hide file tree
Showing 15 changed files with 507 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ generator does not support for now. The following features are not supported:

- [x] Golang generator (in testing phase)
- [ ] Enable a mock server for given OpenAPI specification
- [ ] Download the OpenAPI specification from different sources (local, s3, git and etc.)
- [x] Download the OpenAPI specification from different sources (local, s3, git and etc.)
- [ ] Support for Dictionaries, Hash Maps and Associative Arrays in Golang
- [ ] Support for `application/xml` and `application/x-www-form-urlencoded`
- [ ] Improve the OpenAPI validation reports
Expand Down
37 changes: 37 additions & 0 deletions async/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package async_test

import (
"context"
"sync"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestAsync(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Async Suite")
}

type Runner struct {
Error error
Count int
Mutex sync.RWMutex
}

func (r *Runner) Run(ctx context.Context) error {
r.Mutex.Lock()
r.Count++
r.Mutex.Unlock()

Expect(ctx).NotTo(BeNil())
return r.Error
}

func (r *Runner) Execution() int {
r.Mutex.RLock()
defer r.Mutex.RUnlock()

return r.Count
}
69 changes: 69 additions & 0 deletions async/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package async

import (
"context"
"sync"
)

// TaskFunc is the task's func
type TaskFunc func(ctx context.Context) error

// Task represents a task
type Task struct {
ctx context.Context
cancel func()
err error
wg *sync.WaitGroup
exec TaskFunc
data []interface{}
}

// NewTask creates a new task
func NewTask(fn TaskFunc, data ...interface{}) *Task {
ctx, cancel := context.WithCancel(context.Background())
return &Task{
ctx: ctx,
cancel: cancel,
wg: &sync.WaitGroup{},
exec: fn,
data: data,
}
}

// Data returns the tasks data
func (t *Task) Data() interface{} {
switch len(t.data) {
case 0:
return nil
case 1:
return t.data[0]
default:
return t.data
}
}

// Run runs the task
func (t *Task) Run() {
t.wg.Add(1)

go func() {
if err := t.exec(t.ctx); err != nil {
t.err = err
}

t.wg.Done()
}()
}

// Stop stops the task
func (t *Task) Stop() error {
t.cancel()
t.wg.Wait()
return t.err
}

// Wait waits the task to be stopped
func (t *Task) Wait() error {
t.wg.Wait()
return t.err
}
113 changes: 113 additions & 0 deletions async/task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package async_test

import (
"fmt"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/phogolabs/stride/async"
)

var _ = Describe("Task", func() {
Describe("NewTask", func() {
It("creates a task successfully", func() {
runner := &Runner{}

task := async.NewTask(runner.Run, "root")
Expect(task).NotTo(BeNil())
})
})

Describe("Run", func() {
It("starts a task successfully", func() {
runner := &Runner{}

task := async.NewTask(runner.Run, "root")
task.Run()

Eventually(runner.Execution).Should(Equal(1))
})
})

Describe("Data", func() {
It("returns the associated data", func() {
runner := &Runner{}

task := async.NewTask(runner.Run, "root")
Expect(task.Data()).To(Equal("root"))
})

Context("when no data is provided", func() {
It("returns nil", func() {
runner := &Runner{}

task := async.NewTask(runner.Run)
Expect(task.Data()).To(BeNil())
})
})

Context("when more than one item is provided", func() {
It("returns the associated data", func() {
runner := &Runner{}

task := async.NewTask(runner.Run, "root", "guest")
Expect(task.Data()).To(HaveLen(2))
Expect(task.Data()).To(ContainElement("root"))
Expect(task.Data()).To(ContainElement("guest"))
})
})
})

Describe("Wait", func() {
It("waits for the function successfully", func() {
runner := &Runner{}

task := async.NewTask(runner.Run, "root")
task.Run()

Expect(task.Wait()).To(Succeed())
Expect(runner.Count).To(Equal(1))
})

Context("when the runner fails", func() {
It("returns an error", func() {
runner := &Runner{
Error: fmt.Errorf("oh no"),
}

task := async.NewTask(runner.Run, "root")
task.Run()

Expect(task.Wait()).To(MatchError("oh no"))
Expect(runner.Count).To(Equal(1))
})
})
})

Describe("Stop", func() {
It("stops the function successfully", func() {
runner := &Runner{}

task := async.NewTask(runner.Run, "root")
task.Run()

Expect(task.Stop()).To(Succeed())
Expect(runner.Count).To(Equal(1))
})
})

Context("when the runner fails", func() {
It("returns an error", func() {
runner := &Runner{
Error: fmt.Errorf("oh no"),
}

task := async.NewTask(runner.Run, "root")
task.Run()

Expect(task.Stop()).To(MatchError("oh no"))
Expect(runner.Count).To(Equal(1))
})
})
})
28 changes: 28 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"fmt"

"github.com/phogolabs/cli"
"github.com/phogolabs/stride/torrent"
)

func get(ctx *cli.Context, key string) (string, error) {
// get the spec async
task, err := torrent.GetAsync(ctx.String(key))
if err != nil {
return "", err
}

// make the task available within the app
ctx.Metadata["task"] = task

// wait the download to finish
if err = task.Wait(); err != nil {
// if there are some error stop
return "", err
}

path := fmt.Sprintf("%v", task.Data())
return path, nil
}
8 changes: 7 additions & 1 deletion cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ func (m *OpenAPIEditor) before(ctx *cli.Context) error {
}

func (m *OpenAPIEditor) edit(ctx *cli.Context) error {
// get the spec
path, err := get(ctx, "file-path")
if err != nil {
return err
}

var (
config = &service.EditorConfig{
Path: path,
Addr: ctx.String("listen-addr"),
Path: ctx.String("file-path"),
}
server = service.NewEditor(config)
)
Expand Down
9 changes: 8 additions & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,15 @@ func (m *OpenAPIGenerator) before(ctx *cli.Context) error {
}

func (m *OpenAPIGenerator) generate(ctx *cli.Context) error {
// get the spec
path, err := get(ctx, "file-path")
if err != nil {
return err
}

// generate the soec
generator := &service.Generator{
Path: ctx.String("file-path"),
Path: path,
Resolver: codedom.NewResolver(),
Generator: &golang.Generator{
Path: ctx.String("project-path"),
Expand Down
14 changes: 14 additions & 0 deletions cmd/stride/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"os"
"os/signal"

"github.com/phogolabs/cli"
"github.com/phogolabs/stride/async"
"github.com/phogolabs/stride/cmd"

_ "github.com/phogolabs/stride/template"
Expand Down Expand Up @@ -34,8 +36,20 @@ func main() {
Version: "1.0-beta-05",
Writer: os.Stdout,
ErrWriter: os.Stderr,
OnSignal: onSignal,
Commands: commands,
}

app.Run(os.Args)
}

func onSignal(ctx *cli.Context, term os.Signal) error {
if term == os.Interrupt {
if task, ok := ctx.Metadata["task"].(*async.Task); ok {
signal.Reset(term)
return task.Stop()
}
}

return nil
}
8 changes: 7 additions & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ func (m *OpenAPIValidator) before(ctx *cli.Context) error {
}

func (m *OpenAPIValidator) validate(ctx *cli.Context) error {
// get the spec
path, err := get(ctx, "file-path")
if err != nil {
return err
}

validator := &service.Validator{
Path: ctx.String("file-path"),
Path: path,
}

return validator.Validate()
Expand Down
8 changes: 7 additions & 1 deletion cmd/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ func (m *OpenAPIViewer) before(ctx *cli.Context) error {
}

func (m *OpenAPIViewer) view(ctx *cli.Context) error {
// get the spec
path, err := get(ctx, "file-path")
if err != nil {
return err
}

var (
config = &service.ViewerConfig{
Path: path,
Addr: ctx.String("listen-addr"),
Path: ctx.String("file-path"),
}
server = service.NewViewer(config)
)
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/getkin/kin-openapi v0.2.0
github.com/go-chi/chi v4.0.2+incompatible
github.com/go-openapi/inflect v0.19.0
github.com/golang/protobuf v1.3.2 // indirect
github.com/hashicorp/go-getter v1.4.0
github.com/onsi/ginkgo v1.10.3
github.com/onsi/gomega v1.7.1
github.com/phogolabs/cli v0.0.0-20191121081435-9112dfb9677e
Expand All @@ -17,7 +17,6 @@ require (
github.com/phogolabs/parcello v0.8.1
github.com/stretchr/testify v1.4.0 // indirect
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20191124021906-f5828fc9a103
gopkg.in/yaml.v2 v2.2.7 // indirect
)
Loading

0 comments on commit 2cac88c

Please sign in to comment.