From 5c92ab12012f2dead00cc82498c71eb692e52db3 Mon Sep 17 00:00:00 2001 From: Rodrigo Brito Date: Tue, 12 Feb 2019 13:29:31 -0200 Subject: [PATCH] temp --- go.mod | 1 + go.sum | 2 ++ main.go | 42 +++++++++++++++++++++++++++++++++--------- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 77daf88..bb07358 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/sirupsen/logrus v1.3.0 github.com/src-d/gcfg v1.3.0 // indirect github.com/stretchr/testify v1.2.2 + github.com/urfave/cli v1.20.0 // indirect github.com/xanzy/ssh-agent v0.2.0 // indirect go.opencensus.io v0.17.0 // indirect golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4 // indirect diff --git a/go.sum b/go.sum index 9a0b7a8..3307d37 100644 --- a/go.sum +++ b/go.sum @@ -68,6 +68,8 @@ github.com/src-d/gcfg v1.3.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jW github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= +github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro= github.com/xanzy/ssh-agent v0.2.0/go.mod h1:0NyE30eGUDliuLEHJgYte/zncp2zdTStcOnWhgSqHD8= go.opencensus.io v0.17.0 h1:2Cu88MYg+1LU+WVD+NWwYhyP0kKgRlN9QjWGaX0jKTE= diff --git a/main.go b/main.go index 1193886..f940fd3 100644 --- a/main.go +++ b/main.go @@ -2,15 +2,16 @@ package main import ( "context" + "fmt" "net/http" "os" - log "github.com/sirupsen/logrus" - "github.com/go-chi/chi" "github.com/rodrigo-brito/gocity/handle" "github.com/rodrigo-brito/gocity/handle/middlewares" "github.com/rodrigo-brito/gocity/lib" + log "github.com/sirupsen/logrus" + "github.com/urfave/cli" ) const EnvKeyGCS = "GOOGLE_APPLICATION_CREDENTIALS" @@ -20,7 +21,7 @@ func main() { router := chi.NewRouter() cache := lib.NewCache() - // Use Google Cloud Storage for cache, if available + // Use Google Cloud Storage for cache, if credentials available if credentials := os.Getenv(EnvKeyGCS); len(credentials) > 0 { var err error storage, err = lib.NewGCS(context.Background()) @@ -31,17 +32,40 @@ func main() { corsMiddleware := middlewares.GetCors("*") router.Use(corsMiddleware.Handler) - analyzer := handle.AnalyzerHandle{ Cache: cache, Storage: storage, } - router.Get("/api", analyzer.Handler) - router.Get("/health", handle.HealthCheck) + app := cli.NewApp() + app.Version = "1.0.0" + app.Description = "Code City metaphor for visualizing Go source code in 3D" + app.Author = "Rodrigo Brito" + + app.Commands = []cli.Command{ + { + Name: "server", + Description: "Start a local server to analyze projects", + Action: func(c *cli.Context) error { + router.Get("/api", analyzer.Handler) + router.Get("/health", handle.HealthCheck) + + log.Println("Server started at http://localhost:4000") + + return http.ListenAndServe(":4000", router) + }, + }, + { + Name: "open", + Description: "Open a given project in local server", + Action: func(c *cli.Context) error { + fmt.Println(c.Args().First()) + return nil + }, + }, + } - log.Println("Server started at http://localhost:4000") - if err := http.ListenAndServe(":4000", router); err != nil { - log.Error(err) + if err := app.Run(os.Args); err != nil { + log.Fatal(err) } }