blob: 272d0717dfb47a31fc8c0c4a07142c791ab8390f [file] [log] [blame]
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package cachingservice
import (
"context"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/maruel/subcommands"
"go.chromium.org/luci/auth/client/authcli"
"go.chromium.org/luci/common/cli"
"go.chromium.org/luci/common/flag"
"go.chromium.org/luci/grpc/prpc"
"infra/cmd/shivas/cmdhelp"
"infra/cmd/shivas/site"
"infra/cmd/shivas/utils"
"infra/cmdsupport/cmdlib"
ufsAPI "infra/unifiedfleet/api/v1/rpc"
ufsUtil "infra/unifiedfleet/app/util"
)
// GetCachingServiceCmd get chrome platform by given name.
var GetCachingServiceCmd = &subcommands.Command{
UsageLine: "cachingservice ...",
ShortDesc: "Get CachingService details by filters",
LongDesc: `Get CachingService details by filters.
Example:
shivas get cachingservice {name1} {name2}
shivas get cachingservice -state serving
shivas get cachingservice
Gets the CachingService and prints the output in the user-specified format.`,
CommandRun: func() subcommands.CommandRun {
c := &getCachingService{}
c.authFlags.Register(&c.Flags, site.DefaultAuthOptions)
c.envFlags.Register(&c.Flags)
c.commonFlags.Register(&c.Flags)
c.outputFlags.Register(&c.Flags)
c.Flags.IntVar(&c.pageSize, "n", 0, cmdhelp.ListPageSizeDesc)
c.Flags.BoolVar(&c.keysOnly, "keys", false, cmdhelp.KeysOnlyText)
c.Flags.Var(flag.StringSlice(&c.states), "state", "Name(s) of a state to filter by. Can be specified multiple times."+cmdhelp.StateFilterHelpText)
c.Flags.Var(flag.StringSlice(&c.subnets), "subnets", "Name(s) of a subnet to filter by. Can be specified multiple times.")
return c
},
}
type getCachingService struct {
subcommands.CommandRunBase
authFlags authcli.Flags
envFlags site.EnvFlags
commonFlags site.CommonFlags
outputFlags site.OutputFlags
// Filters
states []string
subnets []string
pageSize int
keysOnly bool
}
func (c *getCachingService) Run(a subcommands.Application, args []string, env subcommands.Env) int {
if err := c.innerRun(a, args, env); err != nil {
cmdlib.PrintError(a, err)
return 1
}
return 0
}
func (c *getCachingService) innerRun(a subcommands.Application, args []string, env subcommands.Env) error {
ctx := cli.GetContext(a, c, env)
ctx = utils.SetupContext(ctx, ufsUtil.OSNamespace)
hc, err := cmdlib.NewHTTPClient(ctx, &c.authFlags)
if err != nil {
return err
}
e := c.envFlags.Env()
if c.commonFlags.Verbose() {
fmt.Printf("Using UnifiedFleet service %s\n", e.UnifiedFleetService)
}
ic := ufsAPI.NewFleetPRPCClient(&prpc.Client{
C: hc,
Host: e.UnifiedFleetService,
Options: site.DefaultPRPCOptions,
})
emit := !utils.NoEmitMode(c.outputFlags.NoEmit())
full := utils.FullMode(c.outputFlags.Full())
var res []proto.Message
if len(args) > 0 {
res = utils.ConcurrentGet(ctx, ic, args, c.getSingle)
} else {
res, err = utils.BatchList(ctx, ic, listCachingServices, c.formatFilters(), c.pageSize, c.keysOnly, full)
}
if err != nil {
return err
}
return utils.PrintEntities(ctx, ic, res, utils.PrintCachingServicesJSON, printCachingServiceFull, printCachingServiceNormal,
c.outputFlags.JSON(), emit, full, c.outputFlags.Tsv(), c.keysOnly)
}
func (c *getCachingService) formatFilters() []string {
filters := make([]string, 0)
filters = utils.JoinFilters(filters, utils.PrefixFilters(ufsUtil.StateFilterName, c.states)...)
filters = utils.JoinFilters(filters, utils.PrefixFilters(ufsUtil.SubnetsFilterName, c.subnets)...)
return filters
}
func (c *getCachingService) getSingle(ctx context.Context, ic ufsAPI.FleetClient, name string) (proto.Message, error) {
return ic.GetCachingService(ctx, &ufsAPI.GetCachingServiceRequest{
Name: ufsUtil.AddPrefix(ufsUtil.CachingServiceCollection, name),
})
}
func listCachingServices(ctx context.Context, ic ufsAPI.FleetClient, pageSize int32, pageToken, filter string, keysOnly, full bool) ([]proto.Message, string, error) {
req := &ufsAPI.ListCachingServicesRequest{
PageSize: pageSize,
PageToken: pageToken,
Filter: filter,
KeysOnly: keysOnly,
}
res, err := ic.ListCachingServices(ctx, req)
if err != nil {
return nil, "", err
}
protos := make([]proto.Message, len(res.GetCachingServices()))
for i, m := range res.GetCachingServices() {
protos[i] = m
}
return protos, res.GetNextPageToken(), nil
}
func printCachingServiceFull(ctx context.Context, ic ufsAPI.FleetClient, msgs []proto.Message, tsv bool) error {
return printCachingServiceNormal(msgs, tsv, false)
}
func printCachingServiceNormal(entities []proto.Message, tsv, keysOnly bool) error {
if len(entities) == 0 {
return nil
}
if tsv {
utils.PrintTSVCachingServices(entities, keysOnly)
return nil
}
utils.PrintTableTitle(utils.CachingServiceTitle, tsv, keysOnly)
utils.PrintCachingServices(entities, keysOnly)
return nil
}