github.com/influxdata/influxdb/v2@v2.7.6/kit/cli/idflag.go (about)

     1  package cli
     2  
     3  import (
     4  	"github.com/influxdata/influxdb/v2/kit/platform"
     5  	"github.com/spf13/cobra"
     6  	"github.com/spf13/pflag"
     7  )
     8  
     9  // Wrapper for influxdb.ID
    10  type idValue platform.ID
    11  
    12  func newIDValue(val platform.ID, p *platform.ID) *idValue {
    13  	*p = val
    14  	return (*idValue)(p)
    15  }
    16  
    17  func (i *idValue) String() string { return platform.ID(*i).String() }
    18  func (i *idValue) Set(s string) error {
    19  	id, err := platform.IDFromString(s)
    20  	if err != nil {
    21  		return err
    22  	}
    23  	*i = idValue(*id)
    24  	return nil
    25  }
    26  
    27  func (i *idValue) Type() string {
    28  	return "ID"
    29  }
    30  
    31  // IDVar defines an influxdb.ID flag with specified name, default value, and usage string.
    32  // The argument p points to an influxdb.ID variable in which to store the value of the flag.
    33  func IDVar(fs *pflag.FlagSet, p *platform.ID, name string, value platform.ID, usage string) {
    34  	IDVarP(fs, p, name, "", value, usage)
    35  }
    36  
    37  // IDVarP is like IDVar, but accepts a shorthand letter that can be used after a single dash.
    38  func IDVarP(fs *pflag.FlagSet, p *platform.ID, name, shorthand string, value platform.ID, usage string) {
    39  	fs.VarP(newIDValue(value, p), name, shorthand, usage)
    40  }
    41  
    42  type OrgBucket struct {
    43  	Org    platform.ID
    44  	Bucket platform.ID
    45  }
    46  
    47  func (o *OrgBucket) AddFlags(cmd *cobra.Command) {
    48  	fs := cmd.Flags()
    49  	IDVar(fs, &o.Org, "org-id", platform.InvalidID(), "organization id")
    50  	IDVar(fs, &o.Bucket, "bucket-id", platform.InvalidID(), "bucket id")
    51  }
    52  
    53  func (o *OrgBucket) OrgBucketID() (orgID, bucketID platform.ID) {
    54  	return o.Org, o.Bucket
    55  }
    56  
    57  func (o *OrgBucket) Name() [platform.IDLength]byte {
    58  	// TODO: FIX THIS
    59  	panic("TODO: Fix")
    60  }