From 32b5bb8df4b3098d13bd603ac00fd23d8a540e8d Mon Sep 17 00:00:00 2001 From: "Kenan H.Hasanovic" Date: Wed, 21 Feb 2024 23:04:25 +0000 Subject: [PATCH 1/4] Log from/to time parse and app by name --- internal/commands/fastedge/logs.go | 54 +++++++++++++----------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/internal/commands/fastedge/logs.go b/internal/commands/fastedge/logs.go index af00f3e..de7362e 100644 --- a/internal/commands/fastedge/logs.go +++ b/internal/commands/fastedge/logs.go @@ -3,7 +3,6 @@ package fastedge import ( "bufio" "context" - "errors" "fmt" "net/http" "os" @@ -28,7 +27,7 @@ func appLogsFilterFlags(cmd *cobra.Command) { // logs-related commands func logs() *cobra.Command { var ( - from, to *time.Time + from, to time.Time sort *sdk.GetV1AppsIdLogsParamsSort edge *string clientIp *string @@ -41,20 +40,13 @@ func logs() *cobra.Command { } var cmdLogsShow = &cobra.Command{ - Use: "show ", + Use: "show ", Short: "Show app logs", Long: `Show app logs printed to stdout/stderr. This command allows you filtering by edge name, client ip and time range.`, Args: cobra.ExactArgs(1), PreRunE: func(cmd *cobra.Command, args []string) error { - fromFlag, err := cmd.Flags().GetString("from") - if err != nil { - return err - } - toFlag, err := cmd.Flags().GetString("to") - if err != nil { - return err - } + sortFlag, err := cmd.Flags().GetString("sort") if err != nil { return err @@ -68,20 +60,14 @@ This command allows you filtering by edge name, client ip and time range.`, return err } - if fromFlag != "" { - f, err := time.Parse(time.RFC3339, fromFlag) - if err != nil { - return errors.New("invalid format for `from` expected RFC3339") - } - from = &f + from, err = parseTimeFlag(cmd, "from") + if err != nil { + return fmt.Errorf("cannot parse 'from' time: %w", err) } - if toFlag != "" { - t, err := time.Parse(time.RFC3339, toFlag) - if err != nil { - return errors.New("invalid format for `to` expected RFC3339") - } - to = &t + to, err = parseTimeFlag(cmd, "to") + if err != nil { + return fmt.Errorf("cannot parse 'to' time: %w", err) } if sortFlag != "" { @@ -99,16 +85,22 @@ This command allows you filtering by edge name, client ip and time range.`, return nil }, RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseInt(args[0], 10, 64) + appName := args[0] + idRsp, err := client.GetAppIdByNameWithResponse(context.Background(), appName) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("getting app id: %w", err) } + + if idRsp.StatusCode() != http.StatusOK { + return fmt.Errorf("%s", string(idRsp.Body)) + } + rsp, err := client.GetV1AppsIdLogsWithResponse( context.Background(), - id, + *idRsp.JSON200, &sdk.GetV1AppsIdLogsParams{ - From: from, - To: to, + From: &from, + To: &to, Edge: edge, Sort: sort, ClientIp: clientIp, @@ -153,10 +145,10 @@ This command allows you filtering by edge name, client ip and time range.`, // Call the API again with the new page number rsp, err = client.GetV1AppsIdLogsWithResponse( context.Background(), - id, + *idRsp.JSON200, &sdk.GetV1AppsIdLogsParams{ - From: from, - To: to, + From: &from, + To: &to, Edge: edge, Sort: sort, ClientIp: clientIp, From 376eb9c03275778747bae794276f5a2f6e232059 Mon Sep 17 00:00:00 2001 From: "Kenan H.Hasanovic" Date: Mon, 4 Mar 2024 10:43:55 +0000 Subject: [PATCH 2/4] Updated Golang SDK for FastEdge API --- go.mod | 4 +++- go.sum | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 8dc305c..b73d792 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,10 @@ module github.com/G-core/gcore-cli go 1.21.5 +replace github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240214130448-d87df1e38764 => ../FastEdge-client-sdk-go + require ( - github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240214130448-d87df1e38764 + github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240304075046-db0c8c3d17e7 github.com/alecthomas/assert v1.0.0 github.com/docker/go-units v0.5.0 github.com/dustin/go-humanize v1.0.1 diff --git a/go.sum b/go.sum index 24fc145..949fbf7 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240214130448-d87df1e38764 h1:7CATrk7BJpU1t4wr2avczZaX1KrwsjhArBKBaiSQN2M= github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240214130448-d87df1e38764/go.mod h1:ggyUVhy8/OCMBY4nbm7n9qDoPioROCk4vHhDJq9w7qE= +github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240304075046-db0c8c3d17e7 h1:99yyAfaF6OV2ghz75yE72LwdzxP6gUYa3hL7XkObb5s= +github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240304075046-db0c8c3d17e7/go.mod h1:ggyUVhy8/OCMBY4nbm7n9qDoPioROCk4vHhDJq9w7qE= github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= github.com/alecthomas/assert v1.0.0 h1:3XmGh/PSuLzDbK3W2gUbRXwgW5lqPkuqvRgeQ30FI5o= github.com/alecthomas/assert v1.0.0/go.mod h1:va/d2JC+M7F6s+80kl/R3G7FUiW6JzUO+hPhLyJ36ZY= From 94e26e6db0f48c6acef004b58dfc24e5ab22fc92 Mon Sep 17 00:00:00 2001 From: "Kenan H.Hasanovic" Date: Sun, 10 Mar 2024 21:18:00 +0000 Subject: [PATCH 3/4] Update fastedge commands to work with app name instead of app id --- go.mod | 1 - internal/commands/fastedge/app.go | 44 +++++++++++++++++++---------- internal/commands/fastedge/logs.go | 24 ++++++---------- internal/commands/fastedge/stats.go | 12 ++++---- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/go.mod b/go.mod index b73d792..27eb3c2 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,6 @@ module github.com/G-core/gcore-cli go 1.21.5 -replace github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240214130448-d87df1e38764 => ../FastEdge-client-sdk-go require ( github.com/G-Core/FastEdge-client-sdk-go v0.0.0-20240304075046-db0c8c3d17e7 diff --git a/internal/commands/fastedge/app.go b/internal/commands/fastedge/app.go index fd3bfa2..3de80ac 100644 --- a/internal/commands/fastedge/app.go +++ b/internal/commands/fastedge/app.go @@ -82,7 +82,7 @@ uploading binary using "--file ". To load file from stdin, use "-" as appPropertiesFlags(cmdCreate) var cmdUpdate = &cobra.Command{ - Use: "update ", + Use: "update ", Short: "Update the app", Long: `This command allows to change only specified properties of the app, omitted properties are left intact. When changing key-value properties, such @@ -92,9 +92,9 @@ You can use either previously-uploaded binary, by specifying "--binary ", or uploading binary using "--file ". To load file from stdin, use "-" as filename`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseInt(args[0], 10, 64) + id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("getting app id: %w", err) } app, err := parseAppProperties(cmd) @@ -184,7 +184,7 @@ uploading binary using "--file ". To load file from stdin, use "-" as } var cmdGet = &cobra.Command{ - Use: "show ", + Use: "show ", Aliases: []string{"get"}, Short: "Show app details", Long: `Show app properties. This command doesn't show app call statisrics. @@ -192,9 +192,9 @@ To see statistics, use "fastedge stat app_calls" and "fastedge stat app_duration commands.`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseInt(args[0], 10, 64) + id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("getting app id: %w", err) } rsp, err := client.GetAppWithResponse( context.Background(), @@ -230,13 +230,13 @@ commands.`, } var cmdEnable = &cobra.Command{ - Use: "enable ", + Use: "enable ", Short: "Enable the app", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseInt(args[0], 10, 64) + id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("getting app id: %w", err) } rsp, err := client.PatchAppWithResponse( context.Background(), @@ -261,13 +261,13 @@ commands.`, } var cmdDisable = &cobra.Command{ - Use: "disable ", + Use: "disable ", Short: "Disable the app", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseInt(args[0], 10, 64) + id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("getting app id: %w", err) } rsp, err := client.PatchAppWithResponse( context.Background(), @@ -292,7 +292,7 @@ commands.`, } var cmdDelete = &cobra.Command{ - Use: "delete ", + Use: "delete ", Short: "Delete the app", Aliases: []string{"rm"}, Long: `This command deletes the app. The binary, referenced by the app, is not deleted, @@ -300,9 +300,9 @@ however binaries, not referenced by any app, get deleted by cleanup process regu so if you don't want this to happen, consider disabling the app to keep binary referenced`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseInt(args[0], 10, 64) + id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("getting app id: %w", err) } if !sure.AreYou(cmd, fmt.Sprintf("delete app %d", id)) { @@ -445,3 +445,17 @@ func outputMap(m *map[string]string, title string) { output.Table(table, output.FmtHuman) } } + +func getAppIdByName(appName string) (int64, error) { + idRsp, err := client.GetAppIdByNameWithResponse(context.Background(), appName) + if err != nil { + return 0, fmt.Errorf("api response: %w", err) + } + if idRsp.StatusCode() != http.StatusOK { + return 0, fmt.Errorf("%s", string(idRsp.Body)) + } + if idRsp.JSON200 == nil { + return 0, fmt.Errorf("app '%s' not found", appName) + } + return *idRsp.JSON200, nil +} diff --git a/internal/commands/fastedge/logs.go b/internal/commands/fastedge/logs.go index de7362e..3590161 100644 --- a/internal/commands/fastedge/logs.go +++ b/internal/commands/fastedge/logs.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" "os" - "strconv" "strings" "time" @@ -85,19 +84,14 @@ This command allows you filtering by edge name, client ip and time range.`, return nil }, RunE: func(cmd *cobra.Command, args []string) error { - appName := args[0] - idRsp, err := client.GetAppIdByNameWithResponse(context.Background(), appName) + id, err := getAppIdByName(args[0]) if err != nil { return fmt.Errorf("getting app id: %w", err) } - if idRsp.StatusCode() != http.StatusOK { - return fmt.Errorf("%s", string(idRsp.Body)) - } - rsp, err := client.GetV1AppsIdLogsWithResponse( context.Background(), - *idRsp.JSON200, + id, &sdk.GetV1AppsIdLogsParams{ From: &from, To: &to, @@ -145,7 +139,7 @@ This command allows you filtering by edge name, client ip and time range.`, // Call the API again with the new page number rsp, err = client.GetV1AppsIdLogsWithResponse( context.Background(), - *idRsp.JSON200, + id, &sdk.GetV1AppsIdLogsParams{ From: &from, To: &to, @@ -171,13 +165,13 @@ This command allows you filtering by edge name, client ip and time range.`, } var cmdLogEnable = &cobra.Command{ - Use: "enable ", + Use: "enable ", Short: "Enable app logging", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseInt(args[0], 10, 64) + id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("getting app id: %w", err) } rsp, err := client.PatchAppWithResponse( context.Background(), @@ -212,13 +206,13 @@ This command allows you filtering by edge name, client ip and time range.`, } var cmdLogDisable = &cobra.Command{ - Use: "disable ", + Use: "disable ", Short: "Disable app logging", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseInt(args[0], 10, 64) + id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("getting app id: %w", err) } rsp, err := client.PatchAppWithResponse( context.Background(), diff --git a/internal/commands/fastedge/stats.go b/internal/commands/fastedge/stats.go index 493146e..8aca4d2 100644 --- a/internal/commands/fastedge/stats.go +++ b/internal/commands/fastedge/stats.go @@ -56,7 +56,7 @@ func stat() *cobra.Command { } var cmdCalls = &cobra.Command{ - Use: "calls ", + Use: "calls ", Aliases: []string{"calls"}, Short: "Show app calls statistic", Long: `Show number of app calls, grouped by time slots and HTTP statuses. @@ -67,9 +67,9 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag "--step" (in seconds).`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseInt(args[0], 10, 64) + id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("getting app id: %w", err) } from, err := parseTimeFlag(cmd, "from") @@ -169,7 +169,7 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag statFlags(cmdCalls) var cmdDuration = &cobra.Command{ - Use: "duration ", + Use: "duration ", Aliases: []string{"duration", "time", "timing"}, Short: "Show app execution duration", Long: `Show duration of app calls, grouped by time slots. All times are in msec @@ -180,9 +180,9 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag "--step" (in seconds).`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.ParseInt(args[0], 10, 64) + id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("getting app id: %w", err) } from, err := parseTimeFlag(cmd, "from") From 24747423a5a8dfad3c8b9f5e9f8569d65de36599 Mon Sep 17 00:00:00 2001 From: "Kenan H.Hasanovic" Date: Mon, 11 Mar 2024 20:58:13 +0000 Subject: [PATCH 4/4] More friendlier error messages for fastedge commands --- internal/commands/fastedge/app.go | 10 +++++----- internal/commands/fastedge/logs.go | 6 +++--- internal/commands/fastedge/stats.go | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/commands/fastedge/app.go b/internal/commands/fastedge/app.go index 3de80ac..b0b2646 100644 --- a/internal/commands/fastedge/app.go +++ b/internal/commands/fastedge/app.go @@ -94,7 +94,7 @@ uploading binary using "--file ". To load file from stdin, use "-" as RunE: func(cmd *cobra.Command, args []string) error { id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("getting app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } app, err := parseAppProperties(cmd) @@ -194,7 +194,7 @@ commands.`, RunE: func(cmd *cobra.Command, args []string) error { id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("getting app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } rsp, err := client.GetAppWithResponse( context.Background(), @@ -236,7 +236,7 @@ commands.`, RunE: func(cmd *cobra.Command, args []string) error { id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("getting app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } rsp, err := client.PatchAppWithResponse( context.Background(), @@ -267,7 +267,7 @@ commands.`, RunE: func(cmd *cobra.Command, args []string) error { id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("getting app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } rsp, err := client.PatchAppWithResponse( context.Background(), @@ -302,7 +302,7 @@ so if you don't want this to happen, consider disabling the app to keep binary r RunE: func(cmd *cobra.Command, args []string) error { id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("getting app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } if !sure.AreYou(cmd, fmt.Sprintf("delete app %d", id)) { diff --git a/internal/commands/fastedge/logs.go b/internal/commands/fastedge/logs.go index 3590161..afc3c7e 100644 --- a/internal/commands/fastedge/logs.go +++ b/internal/commands/fastedge/logs.go @@ -86,7 +86,7 @@ This command allows you filtering by edge name, client ip and time range.`, RunE: func(cmd *cobra.Command, args []string) error { id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("getting app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } rsp, err := client.GetV1AppsIdLogsWithResponse( @@ -171,7 +171,7 @@ This command allows you filtering by edge name, client ip and time range.`, RunE: func(cmd *cobra.Command, args []string) error { id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("getting app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } rsp, err := client.PatchAppWithResponse( context.Background(), @@ -212,7 +212,7 @@ This command allows you filtering by edge name, client ip and time range.`, RunE: func(cmd *cobra.Command, args []string) error { id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("getting app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } rsp, err := client.PatchAppWithResponse( context.Background(), diff --git a/internal/commands/fastedge/stats.go b/internal/commands/fastedge/stats.go index 8aca4d2..56644d4 100644 --- a/internal/commands/fastedge/stats.go +++ b/internal/commands/fastedge/stats.go @@ -69,7 +69,7 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag RunE: func(cmd *cobra.Command, args []string) error { id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("getting app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } from, err := parseTimeFlag(cmd, "from") @@ -182,7 +182,7 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag RunE: func(cmd *cobra.Command, args []string) error { id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("getting app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } from, err := parseTimeFlag(cmd, "from")