add resources command

master
Matthew 2020-12-25 17:20:30 -05:00
parent d597108609
commit ae00e708da
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
8 changed files with 113 additions and 3 deletions

9
.idea/CS-Control.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/CS-Control.iml" filepath="$PROJECT_DIR$/.idea/CS-Control.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

42
.idea/workspace.xml Normal file
View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="81638931-38b1-4ebe-9407-5f51f2b01d2a" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/internal/custom.go" beforeDir="false" afterPath="$PROJECT_DIR$/internal/custom.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/internal/daemon.go" beforeDir="false" afterPath="$PROJECT_DIR$/internal/daemon.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/main.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="GOROOT" url="file://$PROJECT_DIR$/../../../../Go" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="GoLibraries">
<option name="indexEntireGoPath" value="false" />
</component>
<component name="ProjectId" id="1mAPN2yrpYEo0Rd7piZqZFUHEUZ" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="go.import.settings.migrated" value="true" />
<property name="go.sdk.automatically.set" value="true" />
<property name="go.tried.to.enable.integration.vgo.integrator" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="VgoProject">
<integration-enabled>true</integration-enabled>
</component>
</project>

38
cmd/resources.go Normal file
View File

@ -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
}

View File

@ -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))
}

View File

@ -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

View File

@ -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)