Skip to content
Draft
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
83 changes: 83 additions & 0 deletions cmd/explorer.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 6 additions & 0 deletions internal/explorer/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package explorer

import "embed"

//go:embed templates/base.html
var Explorer embed.FS
358 changes: 358 additions & 0 deletions internal/explorer/templates/base.html

Large diffs are not rendered by default.

Loading