diff --git a/cmd/explorer.go b/cmd/explorer.go new file mode 100644 index 00000000..3a85f5e1 --- /dev/null +++ b/cmd/explorer.go @@ -0,0 +1,83 @@ +package cmd + +import ( + "context" + "encoding/json" + "fmt" + "html/template" + "net/http" + + "github.com/gorilla/mux" + "github.com/spf13/cobra" + "github.com/uselagoon/lagoon-cli/internal/explorer" + lclient "github.com/uselagoon/machinery/api/lagoon/client" +) + +var explorerCmd = &cobra.Command{ + Use: "explorer", + Short: "Open GraphQL Explorer", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + validateToken(lagoonCLIConfig.Current) // get a new token if the current one is invalid + }, + RunE: func(cmd *cobra.Command, args []string) error { + port, err := cmd.Flags().GetString("port") + if err != nil { + return err + } + return server(port) + }, +} + +func init() { + explorerCmd.Flags().String("port", "8091", "Branch name to deploy") +} + +func server(port string) error { + r := mux.NewRouter() + r.HandleFunc("/", serveExplorer) + r.HandleFunc("/query", runQuery).Methods("POST") + + fmt.Printf("Open http://localhost:%s in your browser to start using the explorer", port) + err := http.ListenAndServe(fmt.Sprintf(":%s", port), r) + if err != nil { + return err + } + return nil +} + +func runQuery(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + result := "" + if err != nil { + result = err.Error() + } + raw := r.FormValue("raw") + if raw != "" { + validateToken(lagoonCLIConfig.Current) + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + "", + "", + &token, + false) + rawResp, err := lc.ProcessRaw(context.TODO(), raw, nil) + if err != nil { + result = err.Error() + } else { + resp, err := json.MarshalIndent(rawResp, "", "\t") + if err != nil { + result = err.Error() + } else { + result = string(resp) + } + } + } + _, _ = w.Write([]byte(result)) +} + +func serveExplorer(w http.ResponseWriter, r *http.Request) { + tmpl, _ := template.New("").ParseFS(explorer.Explorer, "templates/base.html") + _ = tmpl.ExecuteTemplate(w, "base", nil) +} diff --git a/cmd/root.go b/cmd/root.go index 76f30b3a..77391bf9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -205,6 +205,7 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e rootCmd.AddCommand(rawCmd) rootCmd.AddCommand(resetPasswordCmd) rootCmd.AddCommand(logsCmd) + rootCmd.AddCommand(explorerCmd) } // version/build information command diff --git a/go.mod b/go.mod index fc34a9cf..85f31187 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/Masterminds/semver/v3 v3.4.0 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/google/go-github/v66 v66.0.0 + github.com/gorilla/mux v1.8.1 github.com/guregu/null v4.0.0+incompatible github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc github.com/jedib0t/go-pretty/v6 v6.6.8 diff --git a/go.sum b/go.sum index 1fc28d4e..c7611c67 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,8 @@ github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/guregu/null v4.0.0+incompatible h1:4zw0ckM7ECd6FNNddc3Fu4aty9nTlpkkzH7dPn4/4Gw= github.com/guregu/null v4.0.0+incompatible/go.mod h1:ePGpQaN9cw0tj45IR5E5ehMvsFlLlQZAkkOXZurJ3NM= github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= diff --git a/internal/explorer/fs.go b/internal/explorer/fs.go new file mode 100644 index 00000000..7b6cb291 --- /dev/null +++ b/internal/explorer/fs.go @@ -0,0 +1,6 @@ +package explorer + +import "embed" + +//go:embed templates/base.html +var Explorer embed.FS diff --git a/internal/explorer/templates/base.html b/internal/explorer/templates/base.html new file mode 100644 index 00000000..41ca7911 --- /dev/null +++ b/internal/explorer/templates/base.html @@ -0,0 +1,358 @@ +{{define "base"}} + + +
+ + + ++ +
++ + +
+