mirror of https://github.com/statping/statping
vue
parent
2654822915
commit
89c103279b
84
cmd/cli.go
84
cmd/cli.go
|
@ -16,6 +16,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
@ -27,6 +28,8 @@ import (
|
||||||
"github.com/statping/statping/types/services"
|
"github.com/statping/statping/types/services"
|
||||||
"github.com/statping/statping/utils"
|
"github.com/statping/statping/utils"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -109,13 +112,17 @@ func catchCLI(args []string) error {
|
||||||
if err = configs.ConnectConfigs(config); err != nil {
|
if err = configs.ConnectConfigs(config); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if _, err := services.SelectAllServices(false); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if data, err = handlers.ExportSettings(); err != nil {
|
if data, err = handlers.ExportSettings(); err != nil {
|
||||||
return fmt.Errorf("could not export settings: %v", err.Error())
|
return fmt.Errorf("could not export settings: %v", err.Error())
|
||||||
}
|
}
|
||||||
//core.CloseDB()
|
filename := fmt.Sprintf("%s/statping-%s.json", dir, time.Now().Format("01-02-2006-1504"))
|
||||||
if err = utils.SaveFile(dir+"/statping-export.json", data); err != nil {
|
if err = utils.SaveFile(filename, data); err != nil {
|
||||||
return fmt.Errorf("could not write file statping-export.json: %v", err.Error())
|
return fmt.Errorf("could not write file statping-export.json: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
log.Infoln("Statping export file saved to ", filename)
|
||||||
return errors.New("end")
|
return errors.New("end")
|
||||||
case "import":
|
case "import":
|
||||||
var err error
|
var err error
|
||||||
|
@ -131,6 +138,71 @@ func catchCLI(args []string) error {
|
||||||
if err = json.Unmarshal(data, &exportData); err != nil {
|
if err = json.Unmarshal(data, &exportData); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
log.Printf("=== %s ===\n", exportData.Core.Name)
|
||||||
|
log.Printf("Services: %d\n", len(exportData.Services))
|
||||||
|
log.Printf("Checkins: %d\n", len(exportData.Checkins))
|
||||||
|
log.Printf("Groups: %d\n", len(exportData.Groups))
|
||||||
|
log.Printf("Messages: %d\n", len(exportData.Messages))
|
||||||
|
log.Printf("Users: %d\n", len(exportData.Users))
|
||||||
|
|
||||||
|
config, err := configs.LoadConfigs()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = configs.ConnectConfigs(config); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if data, err = handlers.ExportSettings(); err != nil {
|
||||||
|
return fmt.Errorf("could not export settings: %v", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if ask("Import Core settings?") {
|
||||||
|
c := exportData.Core
|
||||||
|
if err := c.Update(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, s := range exportData.Groups {
|
||||||
|
if ask(fmt.Sprintf("Import Group '%s'?", s.Name)) {
|
||||||
|
s.Id = 0
|
||||||
|
if err := s.Create(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, s := range exportData.Services {
|
||||||
|
if ask(fmt.Sprintf("Import Service '%s'?", s.Name)) {
|
||||||
|
s.Id = 0
|
||||||
|
if err := s.Create(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, s := range exportData.Checkins {
|
||||||
|
if ask(fmt.Sprintf("Import Checkin '%s'?", s.Name)) {
|
||||||
|
s.Id = 0
|
||||||
|
if err := s.Create(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, s := range exportData.Messages {
|
||||||
|
if ask(fmt.Sprintf("Import Message '%s'?", s.Title)) {
|
||||||
|
s.Id = 0
|
||||||
|
if err := s.Create(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, s := range exportData.Users {
|
||||||
|
if ask(fmt.Sprintf("Import User '%s'?", s.Username)) {
|
||||||
|
s.Id = 0
|
||||||
|
if err := s.Create(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Infof("Import complete")
|
||||||
return errors.New("end")
|
return errors.New("end")
|
||||||
case "run":
|
case "run":
|
||||||
if err := runLogs(); err != nil {
|
if err := runLogs(); err != nil {
|
||||||
|
@ -166,6 +238,14 @@ func catchCLI(args []string) error {
|
||||||
return errors.New("end")
|
return errors.New("end")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ask(format string) bool {
|
||||||
|
fmt.Printf(fmt.Sprintf(format + " [y/N]: "))
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
text, _ := reader.ReadString('\n')
|
||||||
|
text = strings.Replace(text, "\n", "", -1)
|
||||||
|
return strings.ToLower(text) == "y"
|
||||||
|
}
|
||||||
|
|
||||||
// ExportIndexHTML returns the HTML of the index page as a string
|
// ExportIndexHTML returns the HTML of the index page as a string
|
||||||
//func ExportIndexHTML() []byte {
|
//func ExportIndexHTML() []byte {
|
||||||
// source.Assets()
|
// source.Assets()
|
||||||
|
|
|
@ -28,13 +28,13 @@ import (
|
||||||
// ExportChartsJs renders the charts for the index page
|
// ExportChartsJs renders the charts for the index page
|
||||||
|
|
||||||
type ExportData struct {
|
type ExportData struct {
|
||||||
Core *core.Core `json:"core"`
|
Core *core.Core `json:"core"`
|
||||||
Services map[int64]*services.Service `json:"services"`
|
Services []services.Service `json:"services"`
|
||||||
Messages []*messages.Message `json:"messages"`
|
Messages []*messages.Message `json:"messages"`
|
||||||
Checkins []*checkins.Checkin `json:"checkins"`
|
Checkins []*checkins.Checkin `json:"checkins"`
|
||||||
Users []*users.User `json:"users"`
|
Users []*users.User `json:"users"`
|
||||||
Groups []*groups.Group `json:"groups"`
|
Groups []*groups.Group `json:"groups"`
|
||||||
Notifiers []core.AllNotifiers `json:"notifiers"`
|
Notifiers []core.AllNotifiers `json:"notifiers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExportSettings will export a JSON file containing all of the settings below:
|
// ExportSettings will export a JSON file containing all of the settings below:
|
||||||
|
@ -46,12 +46,16 @@ type ExportData struct {
|
||||||
// - Groups
|
// - Groups
|
||||||
// - Messages
|
// - Messages
|
||||||
func ExportSettings() ([]byte, error) {
|
func ExportSettings() ([]byte, error) {
|
||||||
|
c, err := core.Select()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
data := ExportData{
|
data := ExportData{
|
||||||
Core: core.App,
|
Core: c,
|
||||||
//Notifiers: notifications.All(),
|
//Notifiers: notifications.All(),
|
||||||
Checkins: checkins.All(),
|
Checkins: checkins.All(),
|
||||||
Users: users.All(),
|
Users: users.All(),
|
||||||
Services: services.Services(),
|
Services: services.AllInOrder(),
|
||||||
Groups: groups.All(),
|
Groups: groups.All(),
|
||||||
Messages: messages.All(),
|
Messages: messages.All(),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue