Skip to content

Commit b484e96

Browse files
Got a basic search command working
1 parent 9fb414f commit b484e96

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

plugin/commands/search/search.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
11
package search
22

33
import (
4+
45
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/plugin"
56
"github.com/spf13/cobra"
67

78
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
810
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
912
)
1013

14+
type SearchCommand struct {
15+
*metadata.SoftlayerCommand
16+
SearchManager managers.SearchManager
17+
Command *cobra.Command
18+
Query string
19+
}
20+
1121
func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
22+
thisCmd := &SearchCommand{
23+
SoftlayerCommand: sl,
24+
SearchManager: managers.NewSearchManager(sl.Session),
25+
}
1226
cobraCmd := &cobra.Command{
1327
Use: "search",
1428
Short: T("Perform a query against the SoftLayer search database."),
15-
RunE: nil,
16-
}
29+
Long: T(`Read More: https://sldn.softlayer.com/reference/services/SoftLayer_Search/search/
30+
Examples::
1731
32+
sl search --query 'test.com'
33+
sl search --query '_objectType:SoftLayer_Virtual_Guest test.com'
34+
`),
35+
Args: metadata.NoArgs,
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
return thisCmd.Run(args)
38+
},
39+
}
40+
cobraCmd.Flags().StringVarP(&thisCmd.Query, "query", "q", "", T("The search query you want to use."))
1841
cobraCmd.AddCommand(NewSearchTypesCommand(sl).Command)
1942
return cobraCmd
2043
}
@@ -26,3 +49,11 @@ func SearchNamespace() plugin.Namespace {
2649
Description: T("Perform a query against the SoftLayer search database."),
2750
}
2851
}
52+
53+
func (cmd *SearchCommand) Run(args []string) error {
54+
55+
results, err := cmd.SearchManager.AdvancedSearch("", cmd.Query)
56+
if err != nil { return err}
57+
58+
return utils.PrintPrettyJSON(cmd.UI, results)
59+
}

0 commit comments

Comments
 (0)