From 89c103279b69edbe59f047d34085d83fa50baa7e Mon Sep 17 00:00:00 2001 From: hunterlong Date: Thu, 12 Mar 2020 03:00:20 -0700 Subject: [PATCH] vue --- cmd/cli.go | 84 ++++++++++++++++++++++++++++++++++++++++++++-- handlers/export.go | 22 +++++++----- 2 files changed, 95 insertions(+), 11 deletions(-) diff --git a/cmd/cli.go b/cmd/cli.go index ba63ac71..ac3b43b8 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -16,6 +16,7 @@ package main import ( + "bufio" "encoding/json" "fmt" "github.com/joho/godotenv" @@ -27,6 +28,8 @@ import ( "github.com/statping/statping/types/services" "github.com/statping/statping/utils" "io/ioutil" + "os" + "strings" "time" ) @@ -109,13 +112,17 @@ func catchCLI(args []string) error { if err = configs.ConnectConfigs(config); err != nil { return err } + if _, err := services.SelectAllServices(false); err != nil { + return err + } if data, err = handlers.ExportSettings(); err != nil { return fmt.Errorf("could not export settings: %v", err.Error()) } - //core.CloseDB() - if err = utils.SaveFile(dir+"/statping-export.json", data); err != nil { + filename := fmt.Sprintf("%s/statping-%s.json", dir, time.Now().Format("01-02-2006-1504")) + if err = utils.SaveFile(filename, data); err != nil { 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") case "import": var err error @@ -131,6 +138,71 @@ func catchCLI(args []string) error { if err = json.Unmarshal(data, &exportData); err != nil { 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") case "run": if err := runLogs(); err != nil { @@ -166,6 +238,14 @@ func catchCLI(args []string) error { 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 //func ExportIndexHTML() []byte { // source.Assets() diff --git a/handlers/export.go b/handlers/export.go index 940aa82c..264f03be 100644 --- a/handlers/export.go +++ b/handlers/export.go @@ -28,13 +28,13 @@ import ( // ExportChartsJs renders the charts for the index page type ExportData struct { - Core *core.Core `json:"core"` - Services map[int64]*services.Service `json:"services"` - Messages []*messages.Message `json:"messages"` - Checkins []*checkins.Checkin `json:"checkins"` - Users []*users.User `json:"users"` - Groups []*groups.Group `json:"groups"` - Notifiers []core.AllNotifiers `json:"notifiers"` + Core *core.Core `json:"core"` + Services []services.Service `json:"services"` + Messages []*messages.Message `json:"messages"` + Checkins []*checkins.Checkin `json:"checkins"` + Users []*users.User `json:"users"` + Groups []*groups.Group `json:"groups"` + Notifiers []core.AllNotifiers `json:"notifiers"` } // ExportSettings will export a JSON file containing all of the settings below: @@ -46,12 +46,16 @@ type ExportData struct { // - Groups // - Messages func ExportSettings() ([]byte, error) { + c, err := core.Select() + if err != nil { + return nil, err + } data := ExportData{ - Core: core.App, + Core: c, //Notifiers: notifications.All(), Checkins: checkins.All(), Users: users.All(), - Services: services.Services(), + Services: services.AllInOrder(), Groups: groups.All(), Messages: messages.All(), }