github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/cmd/cli/cli_job_query.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cli
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/pingcap/log"
    20  	"github.com/pingcap/tiflow/engine/enginepb"
    21  	cmdcontext "github.com/pingcap/tiflow/pkg/cmd/context"
    22  	"github.com/spf13/cobra"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  // jobQueryOptions defines flags for job query.
    27  type jobQueryOptions struct {
    28  	generalOpts *jobGeneralOptions
    29  
    30  	jobID string
    31  }
    32  
    33  // newJobQueryOptions creates new query job options.
    34  func newJobQueryOptions(generalOpts *jobGeneralOptions) *jobQueryOptions {
    35  	return &jobQueryOptions{generalOpts: generalOpts}
    36  }
    37  
    38  // addFlags receives a *cobra.Command reference and binds
    39  // flags related to template printing to it.
    40  func (o *jobQueryOptions) addFlags(cmd *cobra.Command) {
    41  	if o == nil {
    42  		return
    43  	}
    44  
    45  	cmd.Flags().StringVar(&o.jobID, "job-id", "", "job id")
    46  }
    47  
    48  func (o *jobQueryOptions) validate(ctx context.Context) error {
    49  	return o.generalOpts.validate(ctx)
    50  }
    51  
    52  // run the `cli job create` command.
    53  func (o *jobQueryOptions) run(ctx context.Context) error {
    54  	resp, err := o.generalOpts.jobManagerCli.GetJob(ctx, &enginepb.GetJobRequest{
    55  		Id:        o.jobID,
    56  		TenantId:  o.generalOpts.tenant.TenantID(),
    57  		ProjectId: o.generalOpts.tenant.ProjectID(),
    58  	})
    59  	if err != nil {
    60  		return err
    61  	}
    62  	log.Info("query job successfully", zap.Any("resp", resp))
    63  	return nil
    64  }
    65  
    66  // newCmdJobQuery creates the `cli job create` command.
    67  func newCmdJobQuery(generalOpts *jobGeneralOptions) *cobra.Command {
    68  	o := newJobQueryOptions(generalOpts)
    69  
    70  	command := &cobra.Command{
    71  		Use:   "query",
    72  		Short: "Query a job",
    73  		Args:  cobra.NoArgs,
    74  		RunE: func(cmd *cobra.Command, args []string) error {
    75  			ctx := cmdcontext.GetDefaultContext()
    76  			if err := o.validate(ctx); err != nil {
    77  				return err
    78  			}
    79  			return o.run(ctx)
    80  		},
    81  	}
    82  
    83  	o.addFlags(command)
    84  
    85  	return command
    86  }