284 lines
6.2 KiB
Go
284 lines
6.2 KiB
Go
/*
|
|
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/muesli/termenv"
|
|
"github.com/spf13/cobra"
|
|
"io/ioutil"
|
|
"os"
|
|
"pgm/data"
|
|
"pgm/logger"
|
|
"pgm/look"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
)
|
|
|
|
type host struct {
|
|
tic int
|
|
t string
|
|
choices []string
|
|
choice int
|
|
chosen bool
|
|
Quitting bool
|
|
Frames int
|
|
File chan string
|
|
|
|
|
|
}
|
|
|
|
var (
|
|
term = termenv.ColorProfile()
|
|
subtle = makeFgStyle("241")
|
|
dot = colorFg(" • ", "236")
|
|
)
|
|
|
|
|
|
// billyCmd represents the billy command
|
|
var loadCmd = &cobra.Command{
|
|
Use: "load",
|
|
Short: "A brief description of your command",
|
|
Long: `A longer description that spans multiple lines and likely contains examples
|
|
and usage of using your command. For example:
|
|
|
|
Cobra is a CLI library for Go that empowers applications.
|
|
This application is a tool to generate the needed files
|
|
to quickly create a Cobra application.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
var title string
|
|
var pgHosts []string
|
|
|
|
var fn string
|
|
result := make(chan string, 1)
|
|
|
|
pgHosts = data.ReadHosts()
|
|
title = "choose db to load, \"l\" or enter will load your choice"
|
|
var hostChoice host
|
|
hostChoice = host{60, title, pgHosts, 0, false, false, 0, result }
|
|
if err := tea.NewProgram(hostChoice).Start(); err != nil {
|
|
logger.Logger("[ERROR] tea error occured")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Print out the final choice.
|
|
if fn = <-result; len(fn) !=0 {
|
|
logger.Logger("[LOG] loading file: " + fn)
|
|
look.Look(fn)
|
|
}
|
|
|
|
},
|
|
}
|
|
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(loadCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// billyCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// billyCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m host) Init() tea.Cmd {
|
|
return tick()
|
|
}
|
|
|
|
func choicesUpdate(msg tea.Msg, m host) (tea.Model, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
switch msg.String() {
|
|
case "q", "esc":
|
|
m.Quitting = true
|
|
return m, tea.Quit
|
|
case "j", "down":
|
|
m.choice += 1
|
|
if m.choice > len(m.choices) {
|
|
m.choice = len(m.choices)
|
|
}
|
|
case "k", "up":
|
|
m.choice -= 1
|
|
if m.choice < 0 {
|
|
m.choice = 0
|
|
}
|
|
case "l", "enter":
|
|
m.File <- m.choices[m.choice]
|
|
return m, tea.Quit
|
|
}
|
|
|
|
case tickMsg:
|
|
if m.tic == 0 {
|
|
m.Quitting = true
|
|
return m, tea.Quit
|
|
}
|
|
m.tic -= 1
|
|
return m, tick()
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// Update loop for the second view after a choice has been made
|
|
func (m host) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
if msg, ok := msg.(tea.KeyMsg); ok {
|
|
k := msg.String()
|
|
if k == "q" || k == "esc" || k == "ctrl+c" {
|
|
m.Quitting = true
|
|
return m, tea.Quit
|
|
}
|
|
}
|
|
|
|
if !m.chosen {
|
|
return choicesUpdate(msg, m)
|
|
}
|
|
return m, tea.Quit
|
|
}
|
|
|
|
// Views return a string based on data in the host. That string which will be
|
|
// rendered to the terminal.
|
|
func (m host) View() string {
|
|
var s string
|
|
if m.Quitting {
|
|
logger.Logger("[LOG] leaving pgm")
|
|
return "leaving pgm..."
|
|
|
|
}
|
|
if !m.chosen {
|
|
s = choicesView(m)
|
|
} else {
|
|
m.Quitting = true
|
|
logger.Logger("[LOG] leaving pgm")
|
|
return "leaving pgm..."
|
|
}
|
|
return s
|
|
}
|
|
|
|
|
|
|
|
// Creates the menu for choosing a file from your configs.
|
|
// Exits after 60 seconds.
|
|
// Gets Title from outside in the load method if this... should ever be needed elsewhere.
|
|
func choicesView(m host) string {
|
|
var menu string
|
|
c := m.choice
|
|
|
|
tpl := m.t + "\n\n"
|
|
tpl += "%s\n\n"
|
|
tpl += "menu exits in %s seconds\n\n"
|
|
tpl += subtle("j/k, up/down: select") + dot + subtle("enter: choose") + dot + subtle("q, esc: quit")
|
|
for i, s := range m.choices {
|
|
menu = menu + "::" + checkbox(s, c == i)
|
|
}
|
|
mn := strings.Replace(menu, "::", "\n", -1)
|
|
choices := fmt.Sprintf(
|
|
mn,
|
|
)
|
|
|
|
return fmt.Sprintf(tpl, choices, colorFg(strconv.Itoa(m.tic), "79"))
|
|
}
|
|
|
|
// the rest of these functions are to do with looks and aesthetics
|
|
|
|
// returns a neat checkbox
|
|
func checkbox(label string, checked bool) string {
|
|
if checked {
|
|
return colorFg("[x] "+label, "212")
|
|
}
|
|
return fmt.Sprintf("[ ] %s", label)
|
|
}
|
|
|
|
// fun colors
|
|
func colorFg(val, color string) string {
|
|
return termenv.String(val).Foreground(term.Color(color)).String()
|
|
}
|
|
|
|
// Return a function that will colorize the foreground of a given string.
|
|
func makeFgStyle(color string) func(string) string {
|
|
return termenv.Style{}.Foreground(term.Color(color)).Styled
|
|
}
|
|
|
|
// Frame event
|
|
func Frame() tea.Cmd {
|
|
return tea.Tick(time.Second/60, func(time.Time) tea.Msg {
|
|
return FrameMsg{}
|
|
})
|
|
}
|
|
|
|
type tickMsg struct{}
|
|
type FrameMsg struct{}
|
|
|
|
// exits if no choice is selected in a minute
|
|
func tick() tea.Cmd {
|
|
return tea.Tick(time.Second, func(time.Time) tea.Msg {
|
|
return tickMsg{}
|
|
})
|
|
}
|
|
|
|
// loads a yaml file into the correct format for consumption.
|
|
// errs the pipeline step if there is not a configuration in this repository.
|
|
func loadhost(c string, p *data.HostDetails) *data.HostDetails {
|
|
cfg, err := ioutil.ReadFile(c)
|
|
if err != nil {
|
|
logger.Logger("[ERROR] unable to find this filepath: " + err.Error())
|
|
os.Exit(1)
|
|
}
|
|
json.Unmarshal(cfg, &p)
|
|
return p
|
|
}
|
|
|
|
|
|
// Simple struct for a view in the UI.
|
|
type Scr struct {
|
|
Query string `json:"query"`
|
|
Columns int `json:"columns"`
|
|
UIType string `json:"uiType"`
|
|
Title string `json:"title"`
|
|
Quitting bool
|
|
}
|
|
|
|
func (m Scr) Init() tea.Cmd {
|
|
return tick()
|
|
}
|
|
|
|
func (m Scr) View() string {
|
|
|
|
return m.Query
|
|
}
|
|
|
|
func (m Scr) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
if msg, ok := msg.(tea.KeyMsg); ok {
|
|
k := msg.String()
|
|
if k == "q" || k == "esc" || k == "ctrl+c" {
|
|
m.Quitting = true
|
|
return m, tea.Quit
|
|
}
|
|
}
|
|
return m, nil
|
|
}
|