From 2a0ba60429f835424c3af3d6a6a6c57b25522e69 Mon Sep 17 00:00:00 2001 From: Bryce Palmer Date: Wed, 15 Nov 2023 14:36:09 -0500 Subject: [PATCH] (feature): remote dashboard config capability (#27) * (feature): remote dashboard config use Signed-off-by: Bryce Palmer * add to readme Signed-off-by: Bryce Palmer --------- Signed-off-by: Bryce Palmer --- README.md | 2 ++ internal/cli/root.go | 30 ++++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a8d7118..c83499f 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,8 @@ The `test.json` file contains samples for each of the different panel types that `buoy` uses https://github.com/tidwall/gjson for the path evaluation and extracting of values from resources. Please consult their documentation for valid path syntax. +You can also specify a remote reference to a dashboard configuration file. It must be a valid URL and the response must be the raw YAML or JSON contents of the file. + ## Controls - `ctrl+c`, `q`, `esc` will quit the program and exit the tui - `tab` will switch the active tab to the one to the right of the currently active tab diff --git a/internal/cli/root.go b/internal/cli/root.go index df6ea95..f91764a 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -3,7 +3,10 @@ package cli import ( "encoding/json" "fmt" + "io" "log" + "net/http" + "net/url" "os" "path/filepath" @@ -17,7 +20,7 @@ import ( ) var rootCommand = &cobra.Command{ - Use: "buoy [file.json]", + Use: "buoy [config]", Short: "declarative kubernetes dashboard in the terminal", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -29,14 +32,29 @@ func init() { rootCommand.AddCommand(versionCommand) } -func run(file string) error { - ext := filepath.Ext(file) - raw, err := os.ReadFile(file) +func run(path string) error { + var raw []byte + var ext string + u, err := url.ParseRequestURI(path) if err != nil { - log.Fatalf("reading file: %s", err) + ext = filepath.Ext(path) + raw, err = os.ReadFile(path) + if err != nil { + log.Fatalf("reading local config: %s", err) + } + } else { + ext = filepath.Ext(u.Path) + resp, err := http.Get(u.String()) + if err != nil { + log.Fatalf("fetching remote config: %s", err) + } + defer resp.Body.Close() + raw, err = io.ReadAll(resp.Body) + if err != nil { + log.Fatalf("reading remote config: %s", err) + } } - fmt.Println(ext) dash := &types.Dashboard{} if ext == ".yaml" { err = yaml.Unmarshal(raw, dash)