got terminal dimensions
This commit is contained in:
parent
0f465b827f
commit
51892e26fe
28
cmd/root.go
28
cmd/root.go
@ -18,11 +18,11 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"pgm/config"
|
"pgm/ini"
|
||||||
"pgm/logger"
|
|
||||||
|
|
||||||
homedir "github.com/mitchellh/go-homedir"
|
homedir "github.com/mitchellh/go-homedir"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
@ -58,7 +58,7 @@ func init() {
|
|||||||
var homeDirectory string
|
var homeDirectory string
|
||||||
homeDirectory, err := os.UserHomeDir()
|
homeDirectory, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger("[ERROR] unable to init with home directory")
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
cobra.OnInitialize(InitConfig)
|
cobra.OnInitialize(InitConfig)
|
||||||
|
|
||||||
@ -73,9 +73,8 @@ func init() {
|
|||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
customPgm := viper.GetString("pgm_home_overide")
|
customPgm := viper.GetString("pgm_home_overide")
|
||||||
if len(customPgm) > 0 {
|
if len(customPgm) > 0 {
|
||||||
logger.Logger("[LOG] custom pgm overide set at: " + customPgm)
|
|
||||||
if filepath.IsAbs(customPgm) != true {
|
if filepath.IsAbs(customPgm) != true {
|
||||||
logger.Logger("[ERROR] failed to add non absolute path to viper")
|
log.Fatal(err)
|
||||||
} else {
|
} else {
|
||||||
viper.AddConfigPath(customPgm)
|
viper.AddConfigPath(customPgm)
|
||||||
pgmDir := viper.GetString("pgm_dir")
|
pgmDir := viper.GetString("pgm_dir")
|
||||||
@ -90,17 +89,14 @@ func init() {
|
|||||||
viper.SetConfigName(".pgm")
|
viper.SetConfigName(".pgm")
|
||||||
viper.SetDefault("homeDir", homeDirectory)
|
viper.SetDefault("homeDir", homeDirectory)
|
||||||
err = viper.ReadInConfig()
|
err = viper.ReadInConfig()
|
||||||
if err != nil {
|
pgmDir := viper.GetString("pgm_home")
|
||||||
logger.Logger("[ERROR] viper hostCfgFile issue" + err.Error())
|
|
||||||
}
|
|
||||||
pgmDir := viper.GetString("pgm_dir")
|
|
||||||
logsDir := viper.GetString("logs_dir")
|
logsDir := viper.GetString("logs_dir")
|
||||||
hostsDir := viper.GetString("hosts_dir")
|
hostsDir := viper.GetString("hosts_dir")
|
||||||
cfgDir := viper.GetString("cfg_dir")
|
cfgDir := viper.GetString("cfg_dir")
|
||||||
viper.SetDefault("pgmDir", homeDirectory + pgmDir)
|
viper.SetDefault("pgmDir", homeDirectory + "/" + pgmDir)
|
||||||
viper.SetDefault("hostsDir", homeDirectory + pgmDir + "/" + hostsDir)
|
viper.SetDefault("hostsDir", homeDirectory + "/" + pgmDir + "/" + hostsDir)
|
||||||
viper.SetDefault("cfgDir", homeDirectory + pgmDir + "/" + cfgDir)
|
viper.SetDefault("cfgDir", homeDirectory + "/" + pgmDir + "/" + cfgDir)
|
||||||
viper.SetDefault("logsDir", homeDirectory + pgmDir + "/" + logsDir)
|
viper.SetDefault("logsDir", homeDirectory + "/" + pgmDir + "/" + logsDir)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -133,12 +129,8 @@ func InitConfig() {
|
|||||||
pgm = viper.GetString("pgmDir")
|
pgm = viper.GetString("pgmDir")
|
||||||
s := []string{hosts, cfg, logs, pgm}
|
s := []string{hosts, cfg, logs, pgm}
|
||||||
|
|
||||||
config.IsInit(s)
|
ini.IsInit(s)
|
||||||
|
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
viper.AutomaticEnv() // read in environment variables that match
|
||||||
|
|
||||||
// If a hostCfgFile file is found, read it in.
|
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
|
||||||
logger.Logger("[LOG] Using hostCfgFile file:"+ viper.ConfigFileUsed())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,66 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"pgm/logger"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// checks to make sure things look like we want locally.
|
|
||||||
func IsInit(s []string) {
|
|
||||||
var isCfg []bool
|
|
||||||
var dirs []string
|
|
||||||
|
|
||||||
// if there is no $HOME/.pgm dir
|
|
||||||
if _, err := os.Stat(homeDirectory + "/.pgm"); os.IsNotExist(err) {
|
|
||||||
err = os.Mkdir(homeDirectory+"/.pgm/", 0755)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// creates logs, src, and hosts directories.
|
|
||||||
for _, f := range dirs {
|
|
||||||
if len(f) > 0 {
|
|
||||||
err := os.MkdirAll(homeDirectory+"/"+f, 0755)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
isCfg = append(isCfg, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, b := range isCfg {
|
|
||||||
if b == false {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// repeated method to get config data
|
|
||||||
func ReadConfig() (*viper.Viper, error) {
|
|
||||||
var filename string
|
|
||||||
var h string
|
|
||||||
h, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
lgd := logger.Logger("failed to open config " + err.Error())
|
|
||||||
if lgd == true {
|
|
||||||
fmt.Println("failed to read config, failure was logged")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
filename = h + "/" + ".pgm.yaml"
|
|
||||||
v := viper.New()
|
|
||||||
|
|
||||||
v.AutomaticEnv()
|
|
||||||
if err := v.ReadInConfig(); err == nil {
|
|
||||||
v.SetConfigFile(filename)
|
|
||||||
return v, err
|
|
||||||
}
|
|
||||||
return v, nil
|
|
||||||
}
|
|
||||||
10
data/data.go
10
data/data.go
@ -8,7 +8,7 @@ import (
|
|||||||
"pgm/logger"
|
"pgm/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Simple config file for connecting to databases.
|
// Simple ini file for connecting to databases.
|
||||||
type HostDetails struct {
|
type HostDetails struct {
|
||||||
Secret string `json:"secret"`
|
Secret string `json:"secret"`
|
||||||
Hostname string `json:"hostname"`
|
Hostname string `json:"hostname"`
|
||||||
@ -41,7 +41,7 @@ func ReadHosts() []string {
|
|||||||
return pgee
|
return pgee
|
||||||
}
|
}
|
||||||
|
|
||||||
// handles interacting with the config file.
|
// handles interacting with the ini file.
|
||||||
func ViperPgmConfig(k string, a string) (bool, string) {
|
func ViperPgmConfig(k string, a string) (bool, string) {
|
||||||
var h string
|
var h string
|
||||||
var s string
|
var s string
|
||||||
@ -53,9 +53,9 @@ func ViperPgmConfig(k string, a string) (bool, string) {
|
|||||||
viper.SetConfigName(".pgm")
|
viper.SetConfigName(".pgm")
|
||||||
viper.SetConfigType("yaml")
|
viper.SetConfigType("yaml")
|
||||||
viper.AddConfigPath(h)
|
viper.AddConfigPath(h)
|
||||||
err = viper.ReadInConfig() // Find and read the config file
|
err = viper.ReadInConfig() // Find and read the ini file
|
||||||
if err != nil { // Handle errors reading the config file
|
if err != nil { // Handle errors reading the ini file
|
||||||
logger.Logger("[ERROR] failed to load viper config: " + err.Error())
|
logger.Logger("[ERROR] failed to load viper ini: " + err.Error())
|
||||||
fmt.Println("error occurred and was logged.")
|
fmt.Println("error occurred and was logged.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|||||||
1
go.mod
1
go.mod
@ -19,6 +19,7 @@ require (
|
|||||||
github.com/spf13/cobra v1.1.1
|
github.com/spf13/cobra v1.1.1
|
||||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||||
github.com/spf13/viper v1.7.1
|
github.com/spf13/viper v1.7.1
|
||||||
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
|
||||||
golang.org/x/sys v0.0.0-20201024132449-ef9fd89ba245 // indirect
|
golang.org/x/sys v0.0.0-20201024132449-ef9fd89ba245 // indirect
|
||||||
gopkg.in/ini.v1 v1.62.0 // indirect
|
gopkg.in/ini.v1 v1.62.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||||
|
|||||||
19
ini/init.go
Normal file
19
ini/init.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package ini
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// checks to make sure things look like we want locally.
|
||||||
|
func IsInit(s []string) {
|
||||||
|
for _, i := range s {
|
||||||
|
err := os.MkdirAll(i, 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error occurred writing config files, may not have correct permissions")
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -1,6 +1,7 @@
|
|||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/spf13/viper"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@ -21,6 +22,12 @@ func Logger(s string) bool {
|
|||||||
os.Remove("/var/tmp/PGMLOGTEST")
|
os.Remove("/var/tmp/PGMLOGTEST")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
var fp string
|
||||||
|
fp = viper.GetString("logsDir")
|
||||||
|
if _, err := os.Stat(fp + "/log"); os.IsNotExist(err) {
|
||||||
|
// path/to/whatever does not exist
|
||||||
|
os.Create( fp + "/log")
|
||||||
|
}
|
||||||
file, err := os.OpenFile(h + "/.pgm/logs/log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
file, err := os.OpenFile(h + "/.pgm/logs/log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
106
look/look.go
106
look/look.go
@ -1,23 +1,127 @@
|
|||||||
package look
|
package look
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
//"database/sql"
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/charmbracelet/glamour"
|
||||||
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
|
"github.com/muesli/termenv"
|
||||||
|
tp "pgm/templates"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
//"log"
|
||||||
"pgm/data"
|
"pgm/data"
|
||||||
|
//db2 "pgm/db"
|
||||||
"pgm/logger"
|
"pgm/logger"
|
||||||
|
"time"
|
||||||
|
//"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var term = termenv.ColorProfile()
|
||||||
|
|
||||||
|
var (
|
||||||
|
columns = []string{"name", "company"}
|
||||||
|
rows = []string{"bob", "fart co"}
|
||||||
|
colCt = 2
|
||||||
|
sql_test = "SELECT usename, count(usename) FROM pg_stat_activity where usename is not null group by usename"
|
||||||
|
)
|
||||||
|
|
||||||
|
type tickMsg time.Time
|
||||||
|
|
||||||
|
|
||||||
|
func (e screen) Init() tea.Cmd {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type screen struct {
|
||||||
|
viewport viewport.Model
|
||||||
|
}
|
||||||
|
|
||||||
// the default view for a user once they load a database is called the look.
|
// the default view for a user once they load a database is called the look.
|
||||||
// the look loads
|
// the look loads
|
||||||
func Look(f string) {
|
func Look(f string) {
|
||||||
var connectionDetails data.HostDetails
|
var connectionDetails data.HostDetails
|
||||||
connectionDetails = loadHost(f)
|
connectionDetails = loadHost(f)
|
||||||
fmt.Println(connectionDetails)
|
_ = connectionDetails
|
||||||
|
|
||||||
|
|
||||||
|
var t tp.Tbl
|
||||||
|
t = tp.Tbl{columns, colCt, rows, ""}
|
||||||
|
tp.Table(t)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//model, err := newExample()
|
||||||
|
//if err != nil {
|
||||||
|
// fmt.Println("Could not intialize Bubble Tea model:", err)
|
||||||
|
// os.Exit(1)
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//if err := tea.NewProgram(model).Start(); err != nil {
|
||||||
|
// fmt.Println("Bummer, there's been an error:", err)
|
||||||
|
// os.Exit(1)
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e screen) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case tea.WindowSizeMsg:
|
||||||
|
e.viewport.Width = msg.Width
|
||||||
|
return e, nil
|
||||||
|
|
||||||
|
case tea.KeyMsg:
|
||||||
|
switch msg.String() {
|
||||||
|
case "q", "ctrl+c":
|
||||||
|
return e, tea.Quit
|
||||||
|
default:
|
||||||
|
vp, _ := viewport.Update(msg, e.viewport)
|
||||||
|
e.viewport = vp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e screen) View() string {
|
||||||
|
return viewport.View(e.viewport) + e.helpView()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e screen) helpView() string {
|
||||||
|
return termenv.String("\n ↑/↓: Navigate • q: Quit\n").Foreground(term.Color("241")).String()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func newExample() (*screen, error) {
|
||||||
|
vp := viewport.Model{Width: 78, Height: 10}
|
||||||
|
renderer, err := glamour.NewTermRenderer(glamour.WithStylePath("notty"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var t tp.Tbl
|
||||||
|
t = tp.Tbl{columns, colCt, rows, ""}
|
||||||
|
tp.Table(t)
|
||||||
|
|
||||||
|
|
||||||
|
str, err := renderer.Render(t.View)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
vp.SetContent(str)
|
||||||
|
|
||||||
|
return &screen{
|
||||||
|
viewport: vp,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// gets information for host from hostDir
|
// gets information for host from hostDir
|
||||||
func loadHost(f string) data.HostDetails {
|
func loadHost(f string) data.HostDetails {
|
||||||
|
|||||||
@ -3,7 +3,7 @@ package main_test
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"pgm/config"
|
"pgm/ini"
|
||||||
"pgm/data"
|
"pgm/data"
|
||||||
"pgm/db"
|
"pgm/db"
|
||||||
"pgm/logger"
|
"pgm/logger"
|
||||||
@ -27,10 +27,10 @@ func TestLoadConfig(t *testing.T) {
|
|||||||
var v *viper.Viper
|
var v *viper.Viper
|
||||||
|
|
||||||
var want error = nil
|
var want error = nil
|
||||||
v, got := config.ReadConfig()
|
v, got := ini.ReadConfig()
|
||||||
_ = v
|
_ = v
|
||||||
if want != got {
|
if want != got {
|
||||||
t.Error("load config reaturned a non nil error")
|
t.Error("load ini reaturned a non nil error")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
71
templates/templates.go
Normal file
71
templates/templates.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package templates
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
type Tbl struct {
|
||||||
|
Column []string
|
||||||
|
Colct int
|
||||||
|
Row []string
|
||||||
|
View string
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// consumes columns
|
||||||
|
func Table(t Tbl){
|
||||||
|
_ = t
|
||||||
|
w, h, err := terminal.GetSize(1)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Println(w)
|
||||||
|
fmt.Println(h)
|
||||||
|
//populate(&t)
|
||||||
|
//return t
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func populate(t *Tbl) *Tbl{
|
||||||
|
var v string
|
||||||
|
for i, c := range t.Column {
|
||||||
|
var col string
|
||||||
|
var coll string
|
||||||
|
col = "| %s |"
|
||||||
|
coll = "| %s |\n"
|
||||||
|
var header string
|
||||||
|
if i == len(t.Column) -1 {
|
||||||
|
fmt.Println()
|
||||||
|
header = fmt.Sprintf(coll, c)
|
||||||
|
v = v + header
|
||||||
|
} else {
|
||||||
|
header = fmt.Sprintf(col, c)
|
||||||
|
v = v + header
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
for i, c := range t.Row {
|
||||||
|
if i == len(t.Row[0]) {
|
||||||
|
colnl := "\n| %s |"
|
||||||
|
var header string
|
||||||
|
header = fmt.Sprintf(colnl, c)
|
||||||
|
v = v + header
|
||||||
|
} else {
|
||||||
|
cl := "| %s |"
|
||||||
|
var h string
|
||||||
|
h = fmt.Sprintf(cl, c)
|
||||||
|
v = v + h
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
t.View = v
|
||||||
|
return t
|
||||||
|
|
||||||
|
}
|
||||||
4
templates/test.json
Normal file
4
templates/test.json
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"sql": "SELECT usename, count(usename) FROM pg_stat_activity group by usename",
|
||||||
|
"columns": 2
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user