From ae00e708da3f7e57473a08f42e546dd4af06ff63 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Fri, 25 Dec 2020 17:20:30 -0500 Subject: [PATCH] add resources command --- .idea/CS-Control.iml | 9 +++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ .idea/workspace.xml | 42 ++++++++++++++++++++++++++++++++++++++++++ cmd/resources.go | 38 ++++++++++++++++++++++++++++++++++++++ internal/custom.go | 4 +++- internal/daemon.go | 3 +-- main.go | 6 ++++++ 8 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 .idea/CS-Control.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 cmd/resources.go diff --git a/.idea/CS-Control.iml b/.idea/CS-Control.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/CS-Control.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..dbf0bb3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..5a3cff4 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + \ No newline at end of file diff --git a/cmd/resources.go b/cmd/resources.go new file mode 100644 index 0000000..b3f7369 --- /dev/null +++ b/cmd/resources.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "fmt" + "github.com/olekukonko/tablewriter" + "github.com/urfave/cli/v2" + "os/user" + "strings" + + "csctl/internal" +) + +func Resources(*cli.Context) error { + u, _ := user.Current() + + memoryUsage, err := internal.ContactSR(&internal.TCPData{Type: "mem", Username: u.Username}) + if err != nil { + return internal.Error(err, "Unable to fetch information from CSD.") + } + storageUsage, err := internal.ContactSR(&internal.TCPData{Type: "storage", Username: u.Username}) + if err != nil { + return internal.Error(err, "Unable to fetch information from CSD.") + } + processCount, err := internal.ContactSR(&internal.TCPData{Type: "processcount", Username: u.Username}) + if err != nil { + return internal.Error(err, "Unable to fetch information from CSD.") + } + sshLogins, err := internal.ContactSR(&internal.TCPData{Type: "sshlogins", Username: u.Username}) + + tableString := &strings.Builder{} + table := tablewriter.NewWriter(tableString) + table.SetHeader([]string{"RAM Usage", "Storage Usage", "Processes Running", "SSH Logins"}) + table.Append([]string{memoryUsage, storageUsage, processCount, sshLogins}) + table.Render() + + fmt.Println(tableString.String()) + return nil +} diff --git a/internal/custom.go b/internal/custom.go index 8eba0fe..e2aa6ee 100644 --- a/internal/custom.go +++ b/internal/custom.go @@ -1,6 +1,7 @@ package internal import ( + "errors" "fmt" "github.com/kataras/golog" "os" @@ -8,7 +9,7 @@ import ( "time" ) -func Error(err error, msg string) { +func Error(err error, msg string) error { currentUser, _ := user.Current() path := fmt.Sprintf("%s/.csctl.err.log", currentUser.HomeDir) file, errf := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600) @@ -26,4 +27,5 @@ func Error(err error, msg string) { panic(errf) } golog.Fatal("An error has occurred while processing the executable. A log file has been created in %s, please alert a Technician and provide the contents of this file if needed.\n", path) + return errors.New(fmt.Sprintf("%s\n\nError Details: %s", msg, err)) } diff --git a/internal/daemon.go b/internal/daemon.go index 9a93a60..84ebca6 100644 --- a/internal/daemon.go +++ b/internal/daemon.go @@ -45,8 +45,7 @@ func FetchAccount(username string) (*Account, error) { } var account Account - err = json.Unmarshal([]byte(data), &account) - if err != nil { + if err := json.Unmarshal([]byte(data), &account); err != nil { return nil, err } return &account, nil diff --git a/main.go b/main.go index f14d527..a4ccd41 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,12 @@ func main() { }, }, }, + { + Name: "resources", + Usage: "", + Description: "Provides information about the resources you're currently using.", + Action: cmd.Resources, + }, }, } app.Run(os.Args)