blob: a33c1ab09d97897f7d55bd197f712ccaf6f75210 [file] [log] [blame]
// Copyright 2020 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 chromeplatform
import (
ufsAPI "infra/unifiedfleet/api/v1/rpc"
ufsUtil "infra/unifiedfleet/app/util"
// GetChromePlatformCmd get chrome platform by given name.
var GetChromePlatformCmd = &subcommands.Command{
UsageLine: "platform ...",
ShortDesc: "Get platform details by filters",
LongDesc: `Get platform details by filters.
shivas get platform {name1} {name2}
shivas get platform -man apple
shivas get platform
Gets the platform and prints the output in the user-specified format.`,
CommandRun: func() subcommands.CommandRun {
c := &getChromePlatform{}
c.authFlags.Register(&c.Flags, site.DefaultAuthOptions)
c.Flags.IntVar(&c.pageSize, "n", 0, cmdhelp.ListPageSizeDesc)
c.Flags.BoolVar(&c.keysOnly, "keys", false, cmdhelp.KeysOnlyText)
c.Flags.Var(flag.StringSlice(&c.manufacturers), "man", "Name(s) of a manufacturer to filter by. Can be specified multiple times.")
c.Flags.Var(flag.StringSlice(&c.tags), "tag", "Name(s) of a tag to filter by. Can be specified multiple times.")
return c
type getChromePlatform struct {
authFlags authcli.Flags
envFlags site.EnvFlags
commonFlags site.CommonFlags
outputFlags site.OutputFlags
// Filters
tags []string
manufacturers []string
pageSize int
keysOnly bool
func (c *getChromePlatform) 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 *getChromePlatform) innerRun(a subcommands.Application, args []string, env subcommands.Env) error {
ctx := cli.GetContext(a, c, env)
ns, err := c.envFlags.Namespace()
if err != nil {
return err
ctx = utils.SetupContext(ctx, ns)
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, listChromePlatforms, c.formatFilters(), c.pageSize, c.keysOnly, full)
if err != nil {
return err
return utils.PrintEntities(ctx, ic, res, utils.PrintChromePlatformsJSON, printChromePlatformFull, printChromePlatformNormal,
c.outputFlags.JSON(), emit, full, c.outputFlags.Tsv(), c.keysOnly)
func (c *getChromePlatform) formatFilters() []string {
filters := make([]string, 0)
filters = utils.JoinFilters(filters, utils.PrefixFilters(ufsUtil.TagFilterName, c.tags)...)
filters = utils.JoinFilters(filters, utils.PrefixFilters(ufsUtil.ManufacturerFilterName, c.manufacturers)...)
return filters
func (c *getChromePlatform) getSingle(ctx context.Context, ic ufsAPI.FleetClient, name string) (proto.Message, error) {
return ic.GetChromePlatform(ctx, &ufsAPI.GetChromePlatformRequest{
Name: ufsUtil.AddPrefix(ufsUtil.ChromePlatformCollection, name),
func listChromePlatforms(ctx context.Context, ic ufsAPI.FleetClient, pageSize int32, pageToken, filter string, keysOnly, full bool) ([]proto.Message, string, error) {
req := &ufsAPI.ListChromePlatformsRequest{
PageSize: pageSize,
PageToken: pageToken,
Filter: filter,
KeysOnly: keysOnly,
res, err := ic.ListChromePlatforms(ctx, req)
if err != nil {
return nil, "", err
protos := make([]proto.Message, len(res.GetChromePlatforms()))
for i, m := range res.GetChromePlatforms() {
protos[i] = m
return protos, res.GetNextPageToken(), nil
func printChromePlatformFull(ctx context.Context, ic ufsAPI.FleetClient, msgs []proto.Message, tsv bool) error {
return printChromePlatformNormal(msgs, tsv, false)
func printChromePlatformNormal(entities []proto.Message, tsv, keysOnly bool) error {
if len(entities) == 0 {
return nil
if tsv {
utils.PrintTSVPlatforms(entities, keysOnly)
return nil
utils.PrintTableTitle(utils.ChromePlatformTitle, tsv, keysOnly)
utils.PrintChromePlatforms(entities, keysOnly)
return nil