Minor adjustments, Example poc for sub commands with 'app start' and 'app stop' working concepts

pull/19/head
Paulo Truta 2022-02-06 17:10:02 +00:00
parent 2f36538e1e
commit c88726917f
4 changed files with 62 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"database/sql"
"fmt"
"log"
"os"
@ -23,9 +24,62 @@ func main() {
Name: "edgeboxctl",
Usage: "A tool to facilitate hosting apps and securing your personal data",
Action: func(c *cli.Context) error {
startService()
startService() // Defaults to start as a service if no commands or flags are passed
return nil
},
Commands: []*cli.Command{
{
Name: "bootstrap",
Aliases: []string{"a"},
Usage: "Setps up initial structure and dependencies for the edgebox system",
Action: func(c *cli.Context) error {
fmt.Println("Edgebox Setup")
return nil
},
},
{
Name: "app",
Aliases: []string{"t"},
Usage: "options for edgeapp management",
Subcommands: []*cli.Command{
{
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 = string(tasks.STATUS_CREATED)
task.Created = currentFormattedTime
task.Updated = currentFormattedTime
task = tasks.ExecuteTask(task)
return nil
},
},
{
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 = string(tasks.STATUS_CREATED)
task.Created = currentFormattedTime
task.Updated = currentFormattedTime
task = tasks.ExecuteTask(task)
return nil
},
},
},
},
},
}
err := app.Run(os.Args)

View File

@ -119,7 +119,7 @@ func ExecuteTask(task Task) Task {
log.Fatal(err.Error())
}
formatedDatetime := utils.GetSQLiteFormattedDateTime(time.Now())
formatedDatetime := utils.GetFormattedDateTime(time.Now())
_, err = statement.Exec(STATUS_EXECUTING, formatedDatetime, strconv.Itoa(task.ID)) // Execute SQL Statement
if err != nil {
@ -246,7 +246,7 @@ func ExecuteTask(task Task) Task {
log.Fatal(err.Error())
}
formatedDatetime = utils.GetSQLiteFormattedDateTime(time.Now())
formatedDatetime = utils.GetFormattedDateTime(time.Now())
if task.Result.Valid {
_, err = statement.Exec(STATUS_FINISHED, task.Result.String, formatedDatetime, strconv.Itoa(task.ID)) // Execute SQL Statement with result info

View File

@ -91,8 +91,8 @@ func GetSQLiteDbConnectionDetails() string {
}
// GetSQLiteFormattedDateTime: Given a Time, Returns a string that is formatted ready to be inserted into an SQLite Datetime field using sql.Prepare.
func GetSQLiteFormattedDateTime(t time.Time) string {
// GetFormattedDateTime: Given a Time, Returns a string that is formatted ready to be inserted into an SQLite Datetime field using sql.Prepare.
func GetFormattedDateTime(t time.Time) string {
// This date is used to indicate the layout.
const datetimeLayout = "2006-01-02 15:04:05"
formatedDatetime := t.Format(datetimeLayout)
@ -177,7 +177,7 @@ func WriteOption(optionKey string, optionValue string) {
log.Fatal(err.Error())
}
formatedDatetime := GetSQLiteFormattedDateTime(time.Now())
formatedDatetime := GetFormattedDateTime(time.Now())
_, err = statement.Exec(optionKey, optionValue, formatedDatetime, formatedDatetime) // Execute SQL Statement
if err != nil {

View File

@ -97,9 +97,9 @@ func TestGetPath(t *testing.T) {
}
}
func TestGetSQLiteFormattedDateTime(t *testing.T) {
func TestGetFormattedDateTime(t *testing.T) {
datetime := time.Date(2021, time.Month(1), 01, 1, 30, 15, 0, time.UTC)
result := GetSQLiteFormattedDateTime(datetime)
result := GetFormattedDateTime(datetime)
if result != "2021-01-01 01:30:15" {
t.Log("Expected 2021-01-01 01:30:15 but got ", result)