github.com/nats-io/nsc@v0.0.0-20221206222106-35db9400b257/cmd/describeoperator.go (about)

     1  /*
     2   * Copyright 2018-2020 The NATS Authors
     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   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15  
    16  package cmd
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  
    22  	"github.com/nats-io/jwt/v2"
    23  	"github.com/nats-io/nsc/cmd/store"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  func createDescribeOperatorCmd() *cobra.Command {
    28  	var params DescribeOperatorParams
    29  	cmd := &cobra.Command{
    30  		Use:          "operator",
    31  		Short:        "Describes the operator",
    32  		Args:         cobra.MaximumNArgs(1),
    33  		SilenceUsage: true,
    34  		RunE: func(cmd *cobra.Command, args []string) error {
    35  			return RunMaybeStorelessAction(cmd, args, &params)
    36  		},
    37  	}
    38  	cmd.Flags().StringVarP(&params.outputFile, "output-file", "o", "--", "output file, '--' is stdout")
    39  	cmd.Flags().StringVarP(&params.name, "name", "n", "", "operator name")
    40  
    41  	return cmd
    42  }
    43  
    44  func init() {
    45  	describeCmd.AddCommand(createDescribeOperatorCmd())
    46  }
    47  
    48  type DescribeOperatorParams struct {
    49  	name       string
    50  	outputFile string
    51  	claim      jwt.OperatorClaims
    52  	raw        []byte
    53  }
    54  
    55  func (p *DescribeOperatorParams) SetDefaults(ctx ActionCtx) error {
    56  	p.name = NameFlagOrArgument(p.name, ctx)
    57  	if p.name != "" {
    58  		actx, ok := ctx.(*Actx)
    59  		if !ok {
    60  			return errors.New("unable to cast to actx")
    61  		}
    62  		s, err := GetStoreForOperator(p.name)
    63  		if err != nil {
    64  			return err
    65  		}
    66  		cc, err := s.GetContext()
    67  		if err != nil {
    68  			return err
    69  		}
    70  		actx.ctx.Store = s
    71  		actx.ctx = cc
    72  	} else if ctx.StoreCtx().Store == nil {
    73  		return fmt.Errorf("set an operator")
    74  
    75  	}
    76  	return nil
    77  }
    78  
    79  func (p *DescribeOperatorParams) PreInteractive(_ ActionCtx) error {
    80  	return nil
    81  }
    82  
    83  func (p *DescribeOperatorParams) Load(ctx ActionCtx) error {
    84  	var err error
    85  	if Json || Raw || JsonPath != "" {
    86  		p.raw, err = ctx.StoreCtx().Store.ReadRawOperatorClaim()
    87  		if err != nil {
    88  			return err
    89  		}
    90  		if Json || JsonPath != "" {
    91  			p.raw, err = bodyAsJson(p.raw)
    92  			if err != nil {
    93  				return err
    94  			}
    95  
    96  			if JsonPath != "" {
    97  				p.raw, err = GetField(p.raw, JsonPath)
    98  				if err != nil {
    99  					return err
   100  				}
   101  			}
   102  		}
   103  	} else {
   104  		oc, err := ctx.StoreCtx().Store.ReadOperatorClaim()
   105  		if err != nil {
   106  			return err
   107  		}
   108  		p.claim = *oc
   109  	}
   110  	return nil
   111  }
   112  
   113  func (p *DescribeOperatorParams) Validate(_ ActionCtx) error {
   114  	return nil
   115  }
   116  
   117  func (p *DescribeOperatorParams) PostInteractive(_ ActionCtx) error {
   118  	return nil
   119  }
   120  
   121  func (p *DescribeOperatorParams) Run(_ ActionCtx) (store.Status, error) {
   122  	if Raw || Json || JsonPath != "" {
   123  		if !IsStdOut(p.outputFile) {
   124  			var err error
   125  			p.raw, err = jwt.DecorateJWT(string(p.raw))
   126  			if err != nil {
   127  				return nil, err
   128  			}
   129  		}
   130  		p.raw = append(p.raw, '\n')
   131  		if err := Write(p.outputFile, p.raw); err != nil {
   132  			return nil, err
   133  		}
   134  	} else {
   135  		v := NewOperatorDescriber(p.claim).Describe()
   136  		data := []byte(v)
   137  		if err := Write(p.outputFile, data); err != nil {
   138  			return nil, err
   139  		}
   140  	}
   141  	var s store.Status
   142  	if !IsStdOut(p.outputFile) {
   143  		k := "description"
   144  		if Raw {
   145  			k = "jwt"
   146  		}
   147  		s = store.OKStatus("wrote operator %s to %#q", k, AbbrevHomePaths(p.outputFile))
   148  	}
   149  	return s, nil
   150  }