summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormatthieu <matthieu.cneude@gmail.com>2021-04-23 14:38:28 +0200
committermatthieu <matthieu.cneude@gmail.com>2021-04-23 14:38:28 +0200
commit4dd55332eddeb5938455f7e3dbc88807f071bdad (patch)
tree67180daf426c13468e6601d460cb6a4467a1fbef
parent9c6bf55d4709e4ef31d60e58d22e7259f4d8a298 (diff)
Add possibility to edit the dashboard... directly in DevDash!
-rw-r--r--CHANGELOG.md9
-rw-r--r--cmd/config.go46
-rw-r--r--cmd/edit.go2
-rw-r--r--cmd/root.go12
-rw-r--r--internal/platform/termui.go15
-rw-r--r--internal/tui.go5
6 files changed, 78 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f16735a..f9152a4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
* Automatically redraw the dashboard when size of terminal change.
* Fix errors linked to Google JWT files.
* Command list - list the config available
+* Command version - Display the current version of DevDash
+* Add possibility to add sensible information in environment variable:
+ * DEVDASH_GITHUB_TOKEN (token for Github service)
+ * DEVDASH_GA_KEYFILE (keyfile for Google Analytics service)
+ * DEVDASH_GSC_KEYFILE (keyfile for Google Search Console service)
+* Add possibility to edit file with an editor of your choice
## [0.4.2] - 2020-02-25
@@ -138,6 +144,3 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
* Projects / Services / Widget system
* Display and Grid system
* YAML configuration system
-
-[0.1.1]: https://github.com/Phantas0s/devdash/releases/tag/v0.1.1
-[0.1.0]: https://github.com/Phantas0s/devdash/releases/tag/v0.1.0
diff --git a/cmd/config.go b/cmd/config.go
index cb10caa..56745ad 100644
--- a/cmd/config.go
+++ b/cmd/config.go
@@ -18,6 +18,7 @@ const (
// keys
kQuit = "C-c"
kHotReload = "C-r"
+ kEdit = "C-e"
)
type config struct {
@@ -28,6 +29,7 @@ type config struct {
type General struct {
Keys map[string]string `mapstructure:"keys"`
Refresh int64 `mapstructure:"refresh"`
+ Editor string `mapstructure:"editor"`
}
// RefreshTime return the duration before refreshing the data of all widgets, in seconds.
@@ -169,9 +171,10 @@ func dashPath() string {
return filepath.Join(xdg.ConfigHome, "devdash")
}
-func mapConfig(cfgFile string) config {
+// Map config and return it with the config path
+func mapConfig(cfgFile string) (config, string) {
if cfgFile == "" {
- cfgFile = defaultConfig(dashPath(), "default.yml")
+ cfgFile = createConfig(dashPath(), "default.yml", defaultConfig())
}
// viper.AddConfigPath(home)
@@ -204,7 +207,7 @@ func mapConfig(cfgFile string) config {
}
}
- return cfg
+ return cfg, viper.ConfigFileUsed()
}
func removeExt(filepath string) string {
@@ -216,7 +219,7 @@ func removeExt(filepath string) string {
return filepath
}
-func defaultConfig(path string, filename string) string {
+func createConfig(path string, filename string, template string) string {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0755)
}
@@ -227,7 +230,7 @@ func defaultConfig(path string, filename string) string {
defer file.Close()
if file != nil {
- _, err := file.Write([]byte(internal.DefaultTemplate()))
+ _, err := file.Write([]byte(template))
if err != nil {
panic(err)
}
@@ -270,3 +273,36 @@ func (c config) KHotReload() string {
return kHotReload
}
+
+func (c config) KEdit() string {
+ if ok := c.General.Keys["edit"]; ok != "" {
+ return c.General.Keys["edit"]
+ }
+
+ return kEdit
+}
+
+func defaultConfig() string {
+ return `---
+general:
+ refresh: 600
+ keys:
+ quit: "C-c"
+ hot_reload: "C-r"
+
+
+projects:
+ - name: Default dashboard located at $HOME/.config/devdash/default.yml
+ services:
+ monitor:
+ address: "https://thevaluable.dev"
+ widgets:
+ - row:
+ - col:
+ size: "M"
+ elements:
+ - name: mon.box_availability
+ options:
+ title: " thevaluable.dev status "
+ color: yellow`
+}
diff --git a/cmd/edit.go b/cmd/edit.go
index 755abe8..ef5308c 100644
--- a/cmd/edit.go
+++ b/cmd/edit.go
@@ -14,7 +14,7 @@ var editor string
func editCmd() *cobra.Command {
editCmd := &cobra.Command{
Use: "edit",
- Short: "edit dashboard",
+ Short: "Edit your dashboard with an shell editor",
Run: func(cmd *cobra.Command, args []string) {
edit(args)
},
diff --git a/cmd/root.go b/cmd/root.go
index 7357ae8..668ecc8 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -36,6 +36,8 @@ func init() {
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Debug Mode - doesn't display graph")
rootCmd.AddCommand(listCmd())
rootCmd.AddCommand(versionCmd())
+ rootCmd.AddCommand(editCmd())
+ rootCmd.AddCommand(generateCmd())
}
func Execute() {
@@ -58,11 +60,17 @@ func run(args []string) {
file = args[0]
}
- cfg := mapConfig(file)
+ cfg, file := mapConfig(file)
hotReload := make(chan time.Time)
tui.AddKHotReload(cfg.KHotReload(), hotReload)
tui.AddKQuit(cfg.KQuit())
+ editor := os.Getenv("EDITOR")
+ if cfg.General.Editor != "" {
+ editor = cfg.General.Editor
+ }
+ tui.AddKEdit(cfg.KEdit(), hotReload, file, editor)
+
// first display
build(file, tui)
@@ -89,7 +97,7 @@ func run(args []string) {
// build every services present in the configuration
func build(file string, tui *internal.Tui) {
- cfg := mapConfig(file)
+ cfg, _ := mapConfig(file)
for _, p := range cfg.Projects {
rows, sizes := p.OrderWidgets()
project := internal.NewProject(p.Name, p.NameOptions, rows, sizes, p.Themes, tui)
diff --git a/internal/platform/termui.go b/internal/platform/termui.go
index 6fba783..af01ef3 100644
--- a/internal/platform/termui.go
+++ b/internal/platform/termui.go
@@ -2,6 +2,8 @@ package platform
import (
"fmt"
+ "os"
+ "os/exec"
"time"
"github.com/Phantas0s/termui"
@@ -225,6 +227,19 @@ func (t *termUI) KHotReload(key string, c chan<- time.Time) {
})
}
+func (t *termUI) KEdit(key string, c chan<- time.Time, config string, editor string) {
+ termui.Handle(fmt.Sprintf("/sys/kbd/%s", key), func(e termui.Event) {
+ cmd := exec.Command(os.ExpandEnv(editor), config)
+ cmd.Stdin = os.Stdin
+ cmd.Stdout = os.Stdout
+ err := cmd.Run()
+ if err != nil {
+ fmt.Println(err)
+ }
+ c <- time.Now()
+ })
+}
+
// Loop termui to receive events.
func (t *termUI) Loop() {
termui.Loop()
diff --git a/internal/tui.go b/internal/tui.go
index ee5bb93..cf786c9 100644
--- a/internal/tui.go
+++ b/internal/tui.go
@@ -161,6 +161,7 @@ type drawer interface {
type keyManager interface {
KQuit(key string)
KHotReload(key string, c chan<- time.Time)
+ KEdit(key string, c chan<- time.Time, config string, editor string)
}
type looper interface {
@@ -525,6 +526,10 @@ func (t *Tui) AddKHotReload(key string, c chan<- time.Time) {
t.instance.KHotReload(key, c)
}
+func (t *Tui) AddKEdit(key string, c chan<- time.Time, config string, editor string) {
+ t.instance.KEdit(key, c, config, editor)
+}
+
// Loop the TUI to receive events.
func (t *Tui) Loop() {
t.instance.Loop()