2022-04-27 09:30:08 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-06-24 14:26:34 +02:00
|
|
|
"errors"
|
2022-04-27 09:30:08 +02:00
|
|
|
"fmt"
|
2022-10-14 17:03:05 +02:00
|
|
|
"time"
|
2022-04-27 09:30:08 +02:00
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
2022-10-14 17:03:05 +02:00
|
|
|
"go-micro.dev/v4/client"
|
2022-04-27 09:30:08 +02:00
|
|
|
|
2022-10-14 17:03:05 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
|
2022-05-04 14:49:59 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/service/grpc"
|
2023-07-27 12:44:42 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
|
2022-05-04 14:49:59 +02:00
|
|
|
searchsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0"
|
2022-06-20 16:49:06 +02:00
|
|
|
"github.com/owncloud/ocis/v2/services/search/pkg/config"
|
|
|
|
|
"github.com/owncloud/ocis/v2/services/search/pkg/config/parser"
|
2022-04-27 09:30:08 +02:00
|
|
|
)
|
|
|
|
|
|
2026-10-09 03:54:38 -04:00
|
|
|
// _noTimeout stands in for "no limit" when --timeout is 0: go-micro treats a
|
|
|
|
|
// zero RequestTimeout as an immediately expired deadline, so a far-off one is
|
|
|
|
|
// used instead.
|
|
|
|
|
const _noTimeout = 100 * 365 * 24 * time.Hour
|
|
|
|
|
|
|
|
|
|
// Index is the entrypoint for the index command.
|
2022-04-27 09:30:08 +02:00
|
|
|
func Index(cfg *config.Config) *cli.Command {
|
|
|
|
|
return &cli.Command{
|
|
|
|
|
Name: "index",
|
|
|
|
|
Usage: "index the files for one one more users",
|
|
|
|
|
Category: "index management",
|
|
|
|
|
Aliases: []string{"i"},
|
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
|
&cli.StringFlag{
|
2024-06-24 14:26:34 +02:00
|
|
|
Name: "space",
|
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
|
Usage: "the id of the space to travers and index the files of. This or --all-spaces is required.",
|
2022-04-27 09:30:08 +02:00
|
|
|
},
|
2024-06-24 14:26:34 +02:00
|
|
|
&cli.BoolFlag{
|
|
|
|
|
Name: "all-spaces",
|
|
|
|
|
Usage: "index all spaces instead. This or --space is required.",
|
2022-04-27 09:30:08 +02:00
|
|
|
},
|
2026-10-09 03:54:38 -04:00
|
|
|
&cli.DurationFlag{
|
|
|
|
|
Name: "timeout",
|
|
|
|
|
Usage: "how long to wait for the indexing to finish before giving up, e.g. 2h. The walk is cancelled when the timeout expires. 0 waits indefinitely.",
|
|
|
|
|
Value: 10 * time.Minute,
|
|
|
|
|
},
|
2022-04-27 09:30:08 +02:00
|
|
|
},
|
2024-06-24 14:26:34 +02:00
|
|
|
Before: func(_ *cli.Context) error {
|
2022-10-14 17:03:05 +02:00
|
|
|
return configlog.ReturnFatal(parser.ParseConfig(cfg))
|
2022-04-27 09:30:08 +02:00
|
|
|
},
|
2022-10-14 17:03:05 +02:00
|
|
|
Action: func(ctx *cli.Context) error {
|
2024-06-24 14:26:34 +02:00
|
|
|
if ctx.String("space") == "" && !ctx.Bool("all-spaces") {
|
|
|
|
|
return errors.New("either --space or --all-spaces is required")
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:44:42 +02:00
|
|
|
traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
grpcClient, err := grpc.NewClient(
|
|
|
|
|
append(grpc.GetClientOptions(cfg.GRPCClientTLS),
|
2026-10-06 17:21:46 +02:00
|
|
|
grpc.WithKeepaliveParams(cfg.GRPCClientOptions),
|
2023-07-27 12:44:42 +02:00
|
|
|
grpc.WithTraceProvider(traceProvider),
|
|
|
|
|
)...,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-09 03:54:38 -04:00
|
|
|
// The service walks the whole space inside this request, so the
|
|
|
|
|
// request timeout bounds the walk: when it expires the walk is
|
|
|
|
|
// cancelled and everything after that point stays unindexed.
|
|
|
|
|
timeout := ctx.Duration("timeout")
|
|
|
|
|
if timeout <= 0 {
|
|
|
|
|
timeout = _noTimeout
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:03:05 +02:00
|
|
|
c := searchsvc.NewSearchProviderService("com.owncloud.api.search", grpcClient)
|
2023-07-27 12:44:42 +02:00
|
|
|
_, err = c.IndexSpace(context.Background(), &searchsvc.IndexSpaceRequest{
|
2022-10-14 17:03:05 +02:00
|
|
|
SpaceId: ctx.String("space"),
|
2026-10-09 03:54:38 -04:00
|
|
|
}, func(opts *client.CallOptions) { opts.RequestTimeout = timeout })
|
2022-04-27 09:30:08 +02:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("failed to index space: " + err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|