From 9ccb3f1d718f262c55831700afeaa6eb4a62df70 Mon Sep 17 00:00:00 2001 From: Paulo Truta Date: Mon, 7 Feb 2022 20:20:09 +0000 Subject: [PATCH] Commented out unecessary logging (should be under --verbose flag), tidy up command logic, implemented stop and start app commands --- cmd/edgeboxctl/main.go | 35 ++++++++--------------- go.mod | 5 +++- go.sum | 16 +++++++++++ internal/edgeapps/edgeapps.go | 3 +- internal/tasks/tasks.go | 53 +++++++++++++++++++++++------------ internal/utils/utils.go | 9 ++++++ 6 files changed, 78 insertions(+), 43 deletions(-) diff --git a/cmd/edgeboxctl/main.go b/cmd/edgeboxctl/main.go index 64e2bf1..cf5d04d 100644 --- a/cmd/edgeboxctl/main.go +++ b/cmd/edgeboxctl/main.go @@ -1,7 +1,6 @@ package main import ( - "database/sql" "fmt" "log" "os" @@ -46,35 +45,25 @@ func main() { Name: "start", Usage: "start the specified app", Action: func(c *cli.Context) error { - fmt.Println("Starting edgeapp: ", c.Args().First()) - currentFormattedTime := utils.GetFormattedDateTime(time.Now()) - var task tasks.Task - task.ID = 0 - task.Task = "start_edgeapp" - task.Args = sql.NullString{String: fmt.Sprintf("{\"id\": \"%s\"}", c.Args().First()), Valid: true} - task.Status = fmt.Sprintf("%d", tasks.STATUS_CREATED) - task.Created = currentFormattedTime - task.Updated = currentFormattedTime + task := tasks.ExecuteTask(tasks.GetBaseTask( + "start_edgeapp", + fmt.Sprintf("{\"id\": \"%s\"}", c.Args().First()), + )) - task = tasks.ExecuteTask(task) - return nil + return cli.Exit(utils.ColorJsonString(task.Result.String), 0) }, }, { Name: "stop", Usage: "stop the specified app", Action: func(c *cli.Context) error { - fmt.Println("Stopping edgeapp: ", c.Args().First()) - currentFormattedTime := utils.GetFormattedDateTime(time.Now()) - var task tasks.Task - task.ID = 0 - task.Task = "stop_edgeapp" - task.Args = sql.NullString{String: fmt.Sprintf("{\"id\": \"%s\"}", c.Args().First()), Valid: true} - task.Status = fmt.Sprintf("%d", tasks.STATUS_CREATED) - task.Created = currentFormattedTime - task.Updated = currentFormattedTime - task = tasks.ExecuteTask(task) - return nil + + task := tasks.ExecuteTask(tasks.GetBaseTask( + "stop_edgeapp", + fmt.Sprintf("{\"id\": \"%s\"}", c.Args().First()), + )) + + return cli.Exit(utils.ColorJsonString(task.Result.String), 0) }, }, }, diff --git a/go.mod b/go.mod index fd837f4..3a672b0 100644 --- a/go.mod +++ b/go.mod @@ -4,15 +4,18 @@ go 1.15 require ( github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect + github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect github.com/dariubs/percent v0.0.0-20200128140941-b7801cf1c7e2 // indirect github.com/dustin/go-humanize v1.0.0 // indirect + github.com/fatih/color v1.13.0 // indirect github.com/go-ole/go-ole v1.2.5 // indirect github.com/go-sql-driver/mysql v1.5.0 github.com/joho/godotenv v1.3.0 + github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-sqlite3 v1.14.7 // indirect github.com/shirou/gopsutil v3.21.4+incompatible // indirect github.com/tklauser/go-sysconf v0.3.6 // indirect github.com/urfave/cli/v2 v2.3.0 // indirect - golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549 // indirect + golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a // indirect gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index 0e248f7..201d889 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 h1:5sXbqlSomvdjlRbWyNqkPsJ3Fg+tQZCbgeX1VGljbQY= github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= +github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4= +github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/dariubs/percent v0.0.0-20200128140941-b7801cf1c7e2 h1:5EPE4Uk7ucthLTJAZqZxu6LZluox5/AqXUxJDpzgJjg= @@ -9,6 +11,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY= github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= @@ -20,6 +24,12 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA= github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -62,9 +72,15 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549 h1:OL5GcZ2XPkte3dpfuFQ9o884vrE3BZQhajdntNMruv4= golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a h1:ppl5mZgokTT8uPkmYOyEUmPTr3ypaKkg5eFOGrAmxxE= +golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= diff --git a/internal/edgeapps/edgeapps.go b/internal/edgeapps/edgeapps.go index 79ae78e..5a8a792 100644 --- a/internal/edgeapps/edgeapps.go +++ b/internal/edgeapps/edgeapps.go @@ -81,7 +81,8 @@ func GetEdgeApp(ID string) MaybeEdgeApp { myEdgeAppServiceEnv, err := godotenv.Read(utils.GetPath("edgeAppsPath") + ID + myEdgeAppServiceEnvFilename) if err != nil { - log.Println("No myedge.app environment file found. Status is Network-Only") + // log.Println("No myedge.app environment file found. Status is Network-Only") + // File probably not found! } else { if myEdgeAppServiceEnv["INTERNET_URL"] != "" { edgeAppInternetAccessible = true diff --git a/internal/tasks/tasks.go b/internal/tasks/tasks.go index 774c7a5..37adc21 100644 --- a/internal/tasks/tasks.go +++ b/internal/tasks/tasks.go @@ -69,6 +69,23 @@ const STATUS_EXECUTING int = 1 const STATUS_FINISHED int = 2 const STATUS_ERROR int = 3 +func GetBaseTask(taskCode string, argsString string) Task { + currentFormattedTime := utils.GetFormattedDateTime(time.Now()) + task := Task{ + ID: 0, + Task: taskCode, + Args: sql.NullString{ + String: argsString, + Valid: true, + }, + Status: fmt.Sprintf("%d", STATUS_CREATED), + Created: currentFormattedTime, + Updated: currentFormattedTime, + } + + return task +} + // GetNextTask : Performs a MySQL query over the device's Edgebox API func GetNextTask() Task { @@ -129,8 +146,8 @@ func ExecuteTask(task Task) Task { if diagnostics.GetReleaseVersion() == diagnostics.DEV_VERSION { log.Printf("Dev environemnt. Not executing tasks.") } else { - log.Println("Task: " + task.Task) - log.Println("Args: " + task.Args.String) + // log.Println("Task: " + task.Task) + // log.Println("Args: " + task.Args.String) switch task.Task { case "setup_tunnel": @@ -327,7 +344,7 @@ func ExecuteSchedules(tick int) { } func taskSetupTunnel(args taskSetupTunnelArgs) string { - fmt.Println("Executing taskSetupTunnel") + // // fmt.Println("Executing taskSetupTunnel") cmdargs := []string{"gen", "--name", args.NodeName, "--token", args.BootnodeToken, args.BootnodeAddress + ":8655", "--prefix", args.AssignedAddress} utils.Exec(utils.GetPath("wsPath"), "tinc-boot", cmdargs) @@ -343,7 +360,7 @@ func taskSetupTunnel(args taskSetupTunnelArgs) string { } func taskInstallEdgeApp(args taskInstallEdgeAppArgs) string { - fmt.Println("Executing taskInstallEdgeApp for " + args.ID) + // fmt.Println("Executing taskInstallEdgeApp for " + args.ID) result := edgeapps.SetEdgeAppInstalled(args.ID) resultJSON, _ := json.Marshal(result) @@ -353,7 +370,7 @@ func taskInstallEdgeApp(args taskInstallEdgeAppArgs) string { } func taskRemoveEdgeApp(args taskRemoveEdgeAppArgs) string { - fmt.Println("Executing taskRemoveEdgeApp for " + args.ID) + // fmt.Println("Executing taskRemoveEdgeApp for " + args.ID) // Making sure the application is stopped before setting it as removed. edgeapps.StopEdgeApp(args.ID) @@ -365,7 +382,7 @@ func taskRemoveEdgeApp(args taskRemoveEdgeAppArgs) string { } func taskStartEdgeApp(args taskStartEdgeAppArgs) string { - fmt.Println("Executing taskStartEdgeApp for " + args.ID) + // fmt.Println("Executing taskStartEdgeApp for " + args.ID) result := edgeapps.RunEdgeApp(args.ID) resultJSON, _ := json.Marshal(result) @@ -375,7 +392,7 @@ func taskStartEdgeApp(args taskStartEdgeAppArgs) string { } func taskStopEdgeApp(args taskStopEdgeAppArgs) string { - fmt.Println("Executing taskStopEdgeApp for " + args.ID) + // fmt.Println("Executing taskStopEdgeApp for " + args.ID) result := edgeapps.StopEdgeApp(args.ID) resultJSON, _ := json.Marshal(result) @@ -385,7 +402,7 @@ func taskStopEdgeApp(args taskStopEdgeAppArgs) string { } func taskEnableOnline(args taskEnableOnlineArgs) string { - fmt.Println("Executing taskEnableOnline for " + args.ID) + // fmt.Println("Executing taskEnableOnline for " + args.ID) result := edgeapps.EnableOnline(args.ID, args.InternetURL) resultJSON, _ := json.Marshal(result) @@ -395,7 +412,7 @@ func taskEnableOnline(args taskEnableOnlineArgs) string { } func taskDisableOnline(args taskDisableOnlineArgs) string { - fmt.Println("Executing taskDisableOnline for " + args.ID) + // fmt.Println("Executing taskDisableOnline for " + args.ID) result := edgeapps.DisableOnline(args.ID) resultJSON, _ := json.Marshal(result) @@ -405,7 +422,7 @@ func taskDisableOnline(args taskDisableOnlineArgs) string { } func taskEnablePublicDashboard(args taskEnablePublicDashboardArgs) string { - fmt.Println("Enabling taskEnablePublicDashboard") + // fmt.Println("Enabling taskEnablePublicDashboard") result := edgeapps.EnablePublicDashboard(args.InternetURL) if result { @@ -418,7 +435,7 @@ func taskEnablePublicDashboard(args taskEnablePublicDashboardArgs) string { } func taskDisablePublicDashboard() string { - fmt.Println("Executing taskDisablePublicDashboard") + // fmt.Println("Executing taskDisablePublicDashboard") result := edgeapps.DisablePublicDashboard() utils.WriteOption("PUBLIC_DASHBOARD", "") if result { @@ -429,7 +446,7 @@ func taskDisablePublicDashboard() string { func taskSetReleaseVersion() string { - fmt.Println("Executing taskSetReleaseVersion") + // fmt.Println("Executing taskSetReleaseVersion") utils.WriteOption("RELEASE_VERSION", diagnostics.Version) @@ -437,7 +454,7 @@ func taskSetReleaseVersion() string { } func taskGetEdgeApps() string { - fmt.Println("Executing taskGetEdgeApps") + // fmt.Println("Executing taskGetEdgeApps") edgeApps := edgeapps.GetEdgeApps() edgeAppsJSON, _ := json.Marshal(edgeApps) @@ -447,14 +464,14 @@ func taskGetEdgeApps() string { } func taskGetSystemUptime() string { - fmt.Println("Executing taskGetSystemUptime") + // fmt.Println("Executing taskGetSystemUptime") uptime := system.GetUptimeInSeconds() utils.WriteOption("SYSTEM_UPTIME", uptime) return uptime } func taskGetStorageDevices() string { - fmt.Println("Executing taskGetStorageDevices") + // fmt.Println("Executing taskGetStorageDevices") devices := storage.GetDevices(diagnostics.GetReleaseVersion()) devicesJSON, _ := json.Marshal(devices) @@ -465,20 +482,20 @@ func taskGetStorageDevices() string { } func taskGetSystemIP() string { - fmt.Println("Executing taskGetStorageDevices") + // fmt.Println("Executing taskGetStorageDevices") ip := system.GetIP() utils.WriteOption("IP_ADDRESS", ip) return ip } func taskGetHostname() string { - fmt.Println("Executing taskGetHostname") + // fmt.Println("Executing taskGetHostname") hostname := system.GetHostname() utils.WriteOption("HOSTNAME", hostname) return hostname } func taskSetupCloudOptions() { - fmt.Println("Executing taskSetupCloudOptions") + // fmt.Println("Executing taskSetupCloudOptions") system.SetupCloudOptions() } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 0e5acb3..28142ca 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "database/sql" + "encoding/json" "fmt" "io" "log" @@ -12,6 +13,7 @@ import ( "strings" "time" + "github.com/TylerBrock/colorjson" "github.com/joho/godotenv" ) @@ -186,3 +188,10 @@ func WriteOption(optionKey string, optionValue string) { db.Close() } + +func ColorJsonString(jsonString string) string { + var obj map[string]interface{} + json.Unmarshal([]byte(jsonString), &obj) + resultJSON, _ := colorjson.Marshal(obj) + return string(resultJSON) +}