From 51d480a3f02efe321b5bbe52db3ed3132484b0e3 Mon Sep 17 00:00:00 2001 From: Dustin Davis <1dustindavis@gmail.com> Date: Sun, 26 Aug 2018 13:25:12 -0700 Subject: [PATCH] Added GorillaReport.json that includes all items processed --- cmd/gorilla/main.go | 7 ++++ pkg/config/config.go | 6 ++++ pkg/installer/installer.go | 13 +++++++ pkg/report/report.go | 74 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 pkg/report/report.go diff --git a/cmd/gorilla/main.go b/cmd/gorilla/main.go index 55e30e4..dac14c6 100644 --- a/cmd/gorilla/main.go +++ b/cmd/gorilla/main.go @@ -6,6 +6,7 @@ import ( "github.com/1dustindavis/gorilla/pkg/gorillalog" "github.com/1dustindavis/gorilla/pkg/manifest" "github.com/1dustindavis/gorilla/pkg/process" + "github.com/1dustindavis/gorilla/pkg/report" ) func main() { @@ -13,6 +14,9 @@ func main() { // Create a new logger object gorillalog.NewLog() + // Start creating GorillaReport + report.Start() + // Get our configuration config.Get() @@ -40,5 +44,8 @@ func main() { gorillalog.Info("Processing managed updates...") process.Updates(updates, catalog) + // Save GorillaReport to disk + report.End() + gorillalog.Info("Done!") } diff --git a/pkg/config/config.go b/pkg/config/config.go index 48e012f..0e5d9b7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" + "github.com/1dustindavis/gorilla/pkg/report" "gopkg.in/yaml.v2" ) @@ -91,6 +92,7 @@ func Get() { configuration.Verbose = true } + // Set global variables URL = configuration.URL Manifest = configuration.Manifest Catalog = configuration.Catalog @@ -98,5 +100,9 @@ func Get() { Verbose = configuration.Verbose Debug = configuration.Debug + // Add to GorillaReport + report.Items["Manifest"] = Manifest + report.Items["Catalog"] = Catalog + return } diff --git a/pkg/installer/installer.go b/pkg/installer/installer.go index ef854d1..df39718 100644 --- a/pkg/installer/installer.go +++ b/pkg/installer/installer.go @@ -11,6 +11,7 @@ import ( "github.com/1dustindavis/gorilla/pkg/config" "github.com/1dustindavis/gorilla/pkg/download" "github.com/1dustindavis/gorilla/pkg/gorillalog" + "github.com/1dustindavis/gorilla/pkg/report" "github.com/1dustindavis/gorilla/pkg/status" ) @@ -127,6 +128,10 @@ func Install(item catalog.Item) { return } + // Add the item to InstalledItems in GorillaReport + report.InstalledItems = append(report.InstalledItems, item) + + // Run the command and arguments runCommand(installCmd, installArgs) return @@ -203,6 +208,10 @@ func Uninstall(item catalog.Item) { return } + // Add the item to UninstalledItems in GorillaReport + report.UninstalledItems = append(report.UninstalledItems, item) + + // Run the command and arguments runCommand(uninstallCmd, uninstallArgs) return @@ -287,6 +296,10 @@ func Update(item catalog.Item) { return } + // Add the item to UpdatedItems in GorillaReport + report.UpdatedItems = append(report.UpdatedItems, item) + + // Run the command and arguments runCommand(installCmd, installArgs) return diff --git a/pkg/report/report.go b/pkg/report/report.go new file mode 100644 index 0000000..d7905d3 --- /dev/null +++ b/pkg/report/report.go @@ -0,0 +1,74 @@ +package report + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + "os/user" + "path/filepath" + "time" +) + +var ( + // Items contains the data we will save to GorillaReport + Items = make(map[string]interface{}) + + // InstalledItems contains a list of items we attempted to install + InstalledItems []interface{} + + // UninstalledItems contains a list of items we attempted to uninstall + UninstalledItems []interface{} + + // UpdatedItems contains a list of items we attempted to update + UpdatedItems []interface{} +) + +// Start adds the data we already know at the begining of a run +func Start() { + + // Store the start time + currentTime := time.Now().UTC() + Items["StartTime"] = fmt.Sprint(currentTime.Format("2006-01-02 15:04:05 -0700")) + + // Store the current user + currentUser, userErr := user.Current() + if userErr != nil { + fmt.Println("Unable to determine current user", userErr) + } + Items["CurrentUser"] = fmt.Sprint(currentUser.Username) + + // Store the hostname + hostName, hostErr := os.Hostname() + if hostErr != nil { + fmt.Println("Unable to determine current time", hostErr) + } + Items["HostName"] = fmt.Sprint(hostName) +} + +// End will compile everything and save to disk +func End() { + + // Compile everything + Items["InstalledItems"] = InstalledItems + Items["UninstalledItems"] = UninstalledItems + Items["UpdatedItems"] = UpdatedItems + + // Store the end time + currentTime := time.Now().UTC() + Items["EndTime"] = fmt.Sprint(currentTime.Format("2006-01-02 15:04:05 -0700")) + + // Convert it all to json + reportJSON, marshalErr := json.Marshal(Items) + if marshalErr != nil { + fmt.Println("Unable to create GorillaReport json", marshalErr) + } + + // Write Items to disk as GorillaReport.json + reportPath := filepath.Join(os.Getenv("ProgramData"), "gorilla/GorillaReport.json") + writeErr := ioutil.WriteFile(reportPath, reportJSON, 0644) + if writeErr != nil { + fmt.Println("Unable to write GorillaReport.json to disk:", writeErr) + } + +}