From 0376de1a46d167f98f20501715b1bb8718c4e1a2 Mon Sep 17 00:00:00 2001 From: Koustav Date: Mon, 29 Jul 2024 16:04:11 +0530 Subject: [PATCH] Error handling improved --- cmd/query.go | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/cmd/query.go b/cmd/query.go index 6a5a24f..dbc2948 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -39,10 +39,10 @@ var ( defaultEnd = "now" // save filter flags - saveFilterFlag = "save-as" - saveFilterShort = "s" + saveFilterFlag = "save-as" + saveFilterShort = "s" //save filter with time flags - saveFilterTimeFlag = "with-time" + saveFilterTimeFlag = "with-time" saveFilterTimeShort = "w" interactiveFlag = "interactive" @@ -104,12 +104,8 @@ var query = &cobra.Command{ if err != nil { return err } - if filterName=="" { - filterName = "DEFAULT_FILTER_NAME" - } filterNameTrimmed := strings.Trim(filterName, " ") - if interactive { p := tea.NewProgram(model.NewQueryModel(DefaultProfile, query, startTime, endTime), tea.WithAltScreen()) if _, err := p.Run(); err != nil { @@ -120,21 +116,24 @@ var query = &cobra.Command{ } // Checks if there is filter name which is not empty. Empty filter name wont be allowed - if len(filterNameTrimmed) == 0 { + if command.Flags().Changed(saveFilterFlag) { + if filterName == "" || len(filterNameTrimmed) == 0 || filterName == "=" { + fmt.Println("please provide a filter name") + command.Help() + return nil + } else if filterName != "" { + if keepTime { + createFilterWithTime(query, filterNameTrimmed, start, end) + + } else { + // if there is no keep time filter pass empty values for startTime and endTime + createFilter(query, filterNameTrimmed) + } + } + } else if filterName == "" && keepTime { fmt.Println("please provide a filter name") + command.Help() return nil - } else if filterName != "DEFAULT_FILTER_NAME" { - if keepTime { - createFilterWithTime(query, filterNameTrimmed, start, end) - - } else { - // if there is no keep time filter pass empty values for startTime and endTime - createFilter(query, filterNameTrimmed) - } - } else if filterName == "DEFAULT_FILTER_NAME" && keepTime{ - fmt.Println("please provide a filter name") - command.Help() - return nil } client := DefaultClient() @@ -143,11 +142,11 @@ var query = &cobra.Command{ } var QueryCmd = func() *cobra.Command { - query.Flags().BoolP(saveFilterTimeFlag, saveFilterTimeShort,false, "Save the time range associated in the query to the filter") // save time for a filter flag; default value = false (boolean type) + query.Flags().BoolP(saveFilterTimeFlag, saveFilterTimeShort, false, "Save the time range associated in the query to the filter") // save time for a filter flag; default value = false (boolean type) query.Flags().BoolP(interactiveFlag, interactiveFlagShort, false, "open the query result in interactive mode") query.Flags().StringP(startFlag, startFlagShort, defaultStart, "Start time for query. Takes date as '2024-10-12T07:20:50.52Z' or string like '10m', '1hr'") query.Flags().StringP(endFlag, endFlagShort, defaultEnd, "End time for query. Takes date as '2024-10-12T07:20:50.52Z' or 'now'") - query.Flags().StringP(saveFilterFlag, saveFilterShort , "", "Save a query filter") // save filter flag. Default value = FILTER_NAME (type string) + query.Flags().StringP(saveFilterFlag, saveFilterShort, "", "Save a query filter") // save filter flag. Default value = FILTER_NAME (type string) return query }()