diff --git a/go.mod b/go.mod index 1247e29..e89e88c 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,9 @@ module github.com/G-core/gcore-cli go 1.21.5 + 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 a524a02..0865fc5 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= diff --git a/internal/commands/fastedge/app.go b/internal/commands/fastedge/app.go index fd3bfa2..b0b2646 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("cannot find app by name: %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("cannot find app by name: %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("cannot find app by name: %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("cannot find app by name: %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("cannot find app by name: %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 af00f3e..afc3c7e 100644 --- a/internal/commands/fastedge/logs.go +++ b/internal/commands/fastedge/logs.go @@ -3,11 +3,9 @@ package fastedge import ( "bufio" "context" - "errors" "fmt" "net/http" "os" - "strconv" "strings" "time" @@ -28,7 +26,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 +39,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 +59,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 +84,17 @@ 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) + id, err := getAppIdByName(args[0]) if err != nil { - return fmt.Errorf("parsing app id: %w", err) + return fmt.Errorf("cannot find app by name: %w", err) } + rsp, err := client.GetV1AppsIdLogsWithResponse( context.Background(), id, &sdk.GetV1AppsIdLogsParams{ - From: from, - To: to, + From: &from, + To: &to, Edge: edge, Sort: sort, ClientIp: clientIp, @@ -155,8 +141,8 @@ This command allows you filtering by edge name, client ip and time range.`, context.Background(), id, &sdk.GetV1AppsIdLogsParams{ - From: from, - To: to, + From: &from, + To: &to, Edge: edge, Sort: sort, ClientIp: clientIp, @@ -179,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("cannot find app by name: %w", err) } rsp, err := client.PatchAppWithResponse( context.Background(), @@ -220,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("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 493146e..56644d4 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("cannot find app by name: %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("cannot find app by name: %w", err) } from, err := parseTimeFlag(cmd, "from")