-
Notifications
You must be signed in to change notification settings - Fork 6
/
api_search.go
96 lines (79 loc) · 3.05 KB
/
api_search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cmd
import (
"encoding/json"
"fmt"
"strings"
"github.com/antihax/optional"
helpers "github.com/phrase/phrase-cli/helpers"
api "github.com/phrase/phrase-go/v3"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
initSearchInAccount()
rootCmd.AddCommand(SearchApiCmd)
}
var SearchApiCmd = &cobra.Command{
Use: helpers.ToSnakeCase("Search"),
Short: "Search API",
}
func initSearchInAccount() {
params := viper.New()
var use string
// this weird approach is due to mustache template limitations
use = strings.Join(strings.Split("search/in_account", "/")[1:], "_")
var SearchInAccount = &cobra.Command{
Use: use,
Short: "Search across projects",
Long: `Search for keys and translations in all account projects <br><br><i>Note: Search is limited to 10000 results and may not include recently updated data depending on the project sizes.</i>`,
Run: func(cmd *cobra.Command, args []string) {
auth := Auth()
cfg := api.NewConfiguration()
cfg.SetUserAgent(Config.UserAgent)
if Config.Credentials.Host != "" {
cfg.BasePath = Config.Credentials.Host
}
client := api.NewAPIClient(cfg)
localVarOptionals := api.SearchInAccountOpts{}
if Config.Credentials.TFA && Config.Credentials.TFAToken != "" {
localVarOptionals.XPhraseAppOTP = optional.NewString(Config.Credentials.TFAToken)
}
accountId := params.GetString(helpers.ToSnakeCase("AccountId"))
var searchInAccountParameters api.SearchInAccountParameters
if err := json.Unmarshal([]byte(params.GetString("data")), &searchInAccountParameters); err != nil {
HandleError(err)
}
if Config.Debug {
fmt.Printf("%+v\n", searchInAccountParameters)
}
if params.IsSet(helpers.ToSnakeCase("xPhraseAppOTP")) {
localVarOptionals.XPhraseAppOTP = optional.NewString(params.GetString(helpers.ToSnakeCase("XPhraseAppOTP")))
}
data, api_response, err := client.SearchApi.SearchInAccount(auth, accountId, searchInAccountParameters, &localVarOptionals)
if err != nil {
switch castedError := err.(type) {
case api.GenericOpenAPIError:
fmt.Printf("\n%s\n\n", string(castedError.Body()))
HandleError(castedError)
default:
HandleError(castedError)
}
} else if api_response.StatusCode >= 200 && api_response.StatusCode < 300 {
jsonBuf, jsonErr := json.MarshalIndent(data, "", " ")
if jsonErr != nil {
fmt.Printf("%v\n", data)
HandleError(err)
}
fmt.Printf("%s\n", string(jsonBuf))
if Config.Debug {
fmt.Printf("%+v\n", api_response) // &{Response:0xc00011ccf0 NextPage:2 FirstPage:1 LastPage:4 Rate:{Limit:1000 Remaining:998 Reset:2020-04-25 00:35:00 +0200 CEST}}
}
}
},
}
SearchApiCmd.AddCommand(SearchInAccount)
AddFlag(SearchInAccount, "string", helpers.ToSnakeCase("AccountId"), "", "Account ID", true)
AddFlag(SearchInAccount, "string", "data", "d", "payload in JSON format", true)
AddFlag(SearchInAccount, "string", helpers.ToSnakeCase("XPhraseAppOTP"), "", "Two-Factor-Authentication token (optional)", false)
params.BindPFlags(SearchInAccount.Flags())
}