-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump-index-templates.go
51 lines (47 loc) · 1.74 KB
/
dump-index-templates.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
package main
import (
"context"
"encoding/json"
"fmt"
"os/signal"
"syscall"
"github.com/uselagoon/lagoon-opensearch-sync/internal/opensearch"
"go.uber.org/zap"
)
// DumpIndexTemplatesCmd represents the `dump-index-templates` command.
type DumpIndexTemplatesCmd struct {
OpensearchUsername string `kong:"default='admin',env='OPENSEARCH_ADMIN_USERNAME',help='Opensearch admin user'"`
OpensearchPassword string `kong:"required,env='OPENSEARCH_ADMIN_PASSWORD',help='Opensearch admin password'"`
OpensearchBaseURL string `kong:"required,env='OPENSEARCH_BASE_URL',help='Opensearch Base URL'"`
OpensearchCACertificate string `kong:"required,env='OPENSEARCH_CA_CERTIFICATE',help='Opensearch CA Certificate'"`
Raw bool `kong:"help='Dump the raw JSON recevied from the backend service.'"`
}
// Run the dump-index-templates command.
func (cmd *DumpIndexTemplatesCmd) Run(log *zap.Logger) error {
// get main process context, which cancels on SIGTERM
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM)
defer stop()
// init the opensearch client
o, err := opensearch.NewClient(ctx, log, cmd.OpensearchBaseURL,
cmd.OpensearchUsername, cmd.OpensearchPassword, cmd.OpensearchCACertificate)
if err != nil {
return fmt.Errorf("couldn't init opensearch client: %v", err)
}
if cmd.Raw {
data, err := o.RawIndexTemplates(ctx)
fmt.Println(string(data))
return err
}
// get the index templates
it, err := o.IndexTemplates(ctx)
if err != nil {
return fmt.Errorf("couldn't get opensearch index templates: %v", err)
}
// marshal and dump
data, err := json.Marshal(it)
if err != nil {
return fmt.Errorf("couldn't marshal index templates: %v", err)
}
_, err = fmt.Println(string(data))
return err
}