Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query list command to list all saved queries #48

Merged
merged 13 commits into from
Aug 6, 2024
Merged
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'
go-version: '1.22.4'

- name: make verification
run: make verifiers
Expand Down
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,3 @@ issues:
- should have comment or be unexported
- time-naming
- error strings should not be capitalized or end with punctuation or a newline

service:
golangci-lint-version: 1.43.0 # use the fixed version to not introduce new linters unexpectedly
49 changes: 24 additions & 25 deletions cmd/autocomplete.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Copyright (c) 2024 Parseable, Inc
//
//
Expand All @@ -18,35 +17,35 @@
package cmd

import (
"fmt"
"os"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/cobra"
)

// AutocompleteCmd represents the autocomplete command
var AutocompleteCmd = &cobra.Command{
Use: "autocomplete [bash|zsh|powershell]",
Short: "Generate autocomplete script",
Long: `Generate autocomplete script for bash, zsh, or powershell`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
switch args[0] {
case "bash":
err = cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
err = cmd.Root().GenZshCompletion(os.Stdout)
case "powershell":
err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
default:
err = fmt.Errorf("unsupported shell type: %s. Only bash, zsh, and powershell are supported", args[0])
}
Use: "autocomplete [bash|zsh|powershell]",
Short: "Generate autocomplete script",
Long: `Generate autocomplete script for bash, zsh, or powershell`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
switch args[0] {
case "bash":
err = cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
err = cmd.Root().GenZshCompletion(os.Stdout)
case "powershell":
err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
default:
err = fmt.Errorf("unsupported shell type: %s. Only bash, zsh, and powershell are supported", args[0])
}

if err != nil {
return fmt.Errorf("error generating autocomplete script: %w", err)
}
if err != nil {
return fmt.Errorf("error generating autocomplete script: %w", err)
}

return nil
},
return nil
},
}
142 changes: 142 additions & 0 deletions cmd/filterList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) 2024 Parseable, Inc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
"fmt"
"os"
"pb/pkg/model"
"strings"
"time"

"github.com/spf13/cobra"
)

var FilterList = &cobra.Command{
Use: "list",
Example: "pb query list ",
Short: "List of saved filter for a stream",
Long: "\nShow a list of saved filter for a stream ",
PreRunE: PreRunDefaultProfile,
Run: func(_ *cobra.Command, _ []string) {
client := DefaultClient()

p := model.UIApp()
_, err := p.Run()
if err != nil {
os.Exit(1)
}

a := model.FilterToApply()
d := model.FilterToDelete()
if a.Stream() != "" {
filterToPbQuery(a.Stream(), a.StartTime(), a.EndTime())
}
if d.FilterID() != "" {
deleteFilter(&client, d.FilterID())
}
},
}

// Delete a saved filter from the list of filter
func deleteFilter(client *HTTPClient, filterID string) {
deleteURL := `filters/filter/` + filterID
req, err := client.NewRequest("DELETE", deleteURL, nil)
if err != nil {
fmt.Println("Error deleting the filter")
}

resp, err := client.client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()

if resp.StatusCode == 200 {
fmt.Printf("\n\nFilter Deleted")
}
}

// Convert a filter to executable pb query
func filterToPbQuery(query string, start string, end string) {
var timeStamps string
if start == "" || end == "" {
timeStamps = ``
} else {
startFormatted := formatToRFC3339(start)
endFormatted := formatToRFC3339(end)
timeStamps = ` --from=` + startFormatted + ` --to=` + endFormatted
}
queryTemplate := `pb query run ` + query + timeStamps
fmt.Printf("\nCopy and paste the command")
fmt.Printf("\n\n%s\n\n", queryTemplate)
}

// Parses all UTC time format from string to time interface
func parseTimeToFormat(input string) (time.Time, error) {
// List of possible formats
formats := []string{
time.RFC3339,
"2006-01-02 15:04:05",
"2006-01-02",
"01/02/2006 15:04:05",
"02-Jan-2006 15:04:05 MST",
"2006-01-02T15:04:05Z",
"02-Jan-2006",
}

var err error
var t time.Time

for _, format := range formats {
t, err = time.Parse(format, input)
if err == nil {
return t, nil
}
}

return t, fmt.Errorf("unable to parse time: %s", input)
}

// Converts to RFC3339
func convertTime(input string) (string, error) {
t, err := parseTimeToFormat(input)
if err != nil {
return "", err
}

return t.Format(time.RFC3339), nil
}

// Converts User inputted time to string type RFC3339 time
func formatToRFC3339(time string) string {
var formattedTime string
if len(strings.Fields(time)) > 1 {
newTime := strings.Fields(time)[0:2]
rfc39990time, err := convertTime(strings.Join(newTime, " "))
if err != nil {
fmt.Println("error formatting time")
}
formattedTime = rfc39990time
} else {
rfc39990time, err := convertTime(time)
if err != nil {
fmt.Println("error formatting time")
}
formattedTime = rfc39990time
}
return formattedTime
}
Loading
Loading