Skip to content

Commit b0b87d2

Browse files
XenoPhexChris Hendrix
authored and
Chris Hendrix
committed
Reorganize package locations
[#117750485] Signed-off-by: Chris Hendrix <[email protected]>
1 parent df38b34 commit b0b87d2

9 files changed

+95
-127
lines changed

clipr.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
"os"
8+
"sort"
9+
10+
"github.com/cloudfoundry-incubator/cli-plugin-repo/web"
11+
"github.com/tedsuo/rata"
12+
"gopkg.in/yaml.v2"
13+
)
14+
15+
type CLIPR struct {
16+
Port int `short:"p" long:"port" default:"8080" description:"Port that the plugin repo listens on"`
17+
RepoIndexPath string `short:"f" long:"filepath" default:"repo-index.yml" description:"Path to repo-index file"`
18+
}
19+
20+
func (cmd *CLIPR) Execute(args []string) error {
21+
logger := os.Stdout //turn this into a logger soon
22+
23+
var plugins web.PluginsJson
24+
25+
b, err := ioutil.ReadFile(cmd.RepoIndexPath)
26+
if err != nil {
27+
return err
28+
}
29+
30+
err = yaml.Unmarshal(b, &plugins)
31+
if err != nil {
32+
return err
33+
}
34+
35+
sort.Sort(plugins)
36+
37+
handlers := map[string]http.Handler{
38+
Index: http.FileServer(http.Dir("ui")),
39+
List: web.NewListPluginsHandler(plugins, logger),
40+
}
41+
42+
router, err := rata.NewRouter(Routes, handlers)
43+
44+
if err != nil {
45+
return err
46+
}
47+
48+
err = http.ListenAndServe(cmd.bindAddr(), router)
49+
50+
return err
51+
}
52+
53+
func (cmd *CLIPR) bindAddr() string {
54+
return fmt.Sprintf(":%d", cmd.Port)
55+
}

main.go

-66
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,9 @@ import (
44
"fmt"
55
"os"
66

7-
"net/http"
8-
9-
"io/ioutil"
10-
11-
"github.com/cloudfoundry-incubator/cli-plugin-repo/models"
12-
"github.com/cloudfoundry-incubator/cli-plugin-repo/server"
137
"github.com/jessevdk/go-flags"
14-
"github.com/tedsuo/rata"
15-
"gopkg.in/yaml.v2"
16-
"sort"
178
)
189

19-
type CLIPR struct {
20-
Port int `short:"p" long:"port" default:"8080" description:"Port that the plugin repo listens on"`
21-
RepoIndexPath string `short:"f" long:"filepath" default:"repo-index.yml" description:"Path to repo-index file"`
22-
}
23-
2410
func main() {
2511
cmd := &CLIPR{}
2612

@@ -38,55 +24,3 @@ func main() {
3824
os.Exit(1)
3925
}
4026
}
41-
42-
func (cmd *CLIPR) Execute(args []string) error {
43-
logger := os.Stdout //turn this into a logger soon
44-
45-
var plugins models.PluginsJson
46-
47-
b, err := ioutil.ReadFile(cmd.RepoIndexPath)
48-
if err != nil {
49-
return err
50-
}
51-
52-
err = yaml.Unmarshal(b, &plugins)
53-
if err != nil {
54-
return err
55-
}
56-
57-
sort.Sort(plugins)
58-
handles := server.NewServerHandles(plugins, logger)
59-
60-
handlers := map[string]http.Handler{
61-
Index: http.FileServer(http.Dir("ui")),
62-
List: http.HandlerFunc(handles.ListPlugins),
63-
}
64-
65-
router, err := rata.NewRouter(Routes, handlers)
66-
67-
if err != nil {
68-
return err
69-
}
70-
71-
err = http.ListenAndServe(cmd.bindAddr(), router)
72-
73-
return err
74-
}
75-
76-
func (cmd *CLIPR) bindAddr() string {
77-
return fmt.Sprintf(":%d", cmd.Port)
78-
}
79-
80-
const (
81-
Index = "Index"
82-
List = "List"
83-
)
84-
85-
var Routes = rata.Routes([]rata.Route{
86-
{Path: "/", Method: "GET", Name: Index},
87-
{Path: "/js/:file", Method: "GET", Name: Index},
88-
{Path: "/css/:file", Method: "GET", Name: Index},
89-
{Path: "/font/:file", Method: "GET", Name: Index},
90-
{Path: "/images/:file", Method: "GET", Name: Index},
91-
{Path: "/list", Method: "GET", Name: List},
92-
})

main_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"os/exec"
1010
"strconv"
1111

12-
"github.com/cloudfoundry-incubator/cli-plugin-repo/models"
12+
"github.com/cloudfoundry-incubator/cli-plugin-repo/web"
1313
"github.com/onsi/gomega/gexec"
1414
"github.com/onsi/gomega/types"
1515
"gopkg.in/yaml.v2"
@@ -33,7 +33,7 @@ var _ = SynchronizedAfterSuite(func() {}, func() {
3333

3434
var _ = Describe("Database", func() {
3535
It("correctly parses the current repo-index.yml", func() {
36-
var plugins models.PluginsJson
36+
var plugins web.PluginsJson
3737

3838
b, err := ioutil.ReadFile("repo-index.yml")
3939
Expect(err).NotTo(HaveOccurred())

models/models_suite_test.go

-13
This file was deleted.

routes.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "github.com/tedsuo/rata"
4+
5+
const (
6+
Index = "Index"
7+
List = "List"
8+
)
9+
10+
var Routes = rata.Routes([]rata.Route{
11+
{Path: "/", Method: "GET", Name: Index},
12+
{Path: "/js/:file", Method: "GET", Name: Index},
13+
{Path: "/css/:file", Method: "GET", Name: Index},
14+
{Path: "/font/:file", Method: "GET", Name: Index},
15+
{Path: "/images/:file", Method: "GET", Name: Index},
16+
{Path: "/list", Method: "GET", Name: List},
17+
})

server/handles.go

-32
This file was deleted.

server/server_suite_test.go

-13
This file was deleted.

web/list_plugins_handler.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package web
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"net/http"
7+
)
8+
9+
func NewListPluginsHandler(plugins PluginsJson, logger io.Writer) http.Handler {
10+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
11+
b, err := json.Marshal(plugins)
12+
if err != nil {
13+
logger.Write([]byte("Error marshalling plugin models: " + err.Error()))
14+
return
15+
}
16+
17+
w.Header().Set("Content-Type", "application/json")
18+
w.Write(b)
19+
})
20+
}

models/plugins.go web/plugins.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package models
1+
package web
22

33
import "time"
44

0 commit comments

Comments
 (0)