get.porter.sh/porter@v1.3.0/cmd/porter/parameters.go (about)

     1  package main
     2  
     3  import (
     4  	"get.porter.sh/porter/pkg/porter"
     5  	"github.com/spf13/cobra"
     6  )
     7  
     8  func buildParametersCommands(p *porter.Porter) *cobra.Command {
     9  	cmd := &cobra.Command{
    10  		Use:         "parameters",
    11  		Aliases:     []string{"parameter", "param", "params"},
    12  		Annotations: map[string]string{"group": "resource"},
    13  		Short:       "Parameter set commands",
    14  	}
    15  
    16  	cmd.AddCommand(buildParametersApplyCommand(p))
    17  	cmd.AddCommand(buildParametersEditCommand(p))
    18  	cmd.AddCommand(buildParametersGenerateCommand(p))
    19  	cmd.AddCommand(buildParametersListCommand(p))
    20  	cmd.AddCommand(buildParametersDeleteCommand(p))
    21  	cmd.AddCommand(buildParametersShowCommand(p))
    22  	cmd.AddCommand(buildParametersCreateCommand(p))
    23  
    24  	return cmd
    25  }
    26  
    27  func buildParametersApplyCommand(p *porter.Porter) *cobra.Command {
    28  	opts := porter.ApplyOptions{}
    29  
    30  	cmd := &cobra.Command{
    31  		Use:   "apply FILE",
    32  		Short: "Apply changes to a parameter set",
    33  		Long: `Apply changes from the specified file to a parameter set. If the parameter set doesn't already exist, it is created.
    34  
    35  Supported file extensions: json and yaml.
    36  
    37  You can use the generate and show commands to create the initial file:
    38    porter parameters generate myparams --reference SOME_BUNDLE
    39    porter parameters show myparams --output yaml > myparams.yaml
    40  `,
    41  		Example: `  porter parameters apply myparams.yaml`,
    42  		PreRunE: func(cmd *cobra.Command, args []string) error {
    43  			return opts.Validate(p.Context, args)
    44  		},
    45  		RunE: func(cmd *cobra.Command, args []string) error {
    46  			return p.ParametersApply(cmd.Context(), opts)
    47  		},
    48  	}
    49  
    50  	f := cmd.Flags()
    51  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
    52  		"Namespace in which the parameter set is defined. The namespace in the file, if set, takes precedence.")
    53  
    54  	return cmd
    55  }
    56  
    57  func buildParametersEditCommand(p *porter.Porter) *cobra.Command {
    58  	opts := porter.ParameterEditOptions{}
    59  
    60  	cmd := &cobra.Command{
    61  		Use:     "edit",
    62  		Short:   "Edit Parameter Set",
    63  		Long:    `Edit a named parameter set.`,
    64  		Example: `  porter parameter edit debug-tweaks --namespace dev`,
    65  		PreRunE: func(cmd *cobra.Command, args []string) error {
    66  			return opts.Validate(args)
    67  		},
    68  		RunE: func(cmd *cobra.Command, args []string) error {
    69  			return p.EditParameter(cmd.Context(), opts)
    70  		},
    71  	}
    72  
    73  	f := cmd.Flags()
    74  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
    75  		"Namespace in which the parameter set is defined. Defaults to the global namespace.")
    76  
    77  	return cmd
    78  }
    79  
    80  func buildParametersGenerateCommand(p *porter.Porter) *cobra.Command {
    81  	opts := porter.ParameterOptions{}
    82  	cmd := &cobra.Command{
    83  		Use:   "generate [NAME]",
    84  		Short: "Generate Parameter Set",
    85  		Long: `Generate a named set of parameters.
    86  
    87  The first argument is the name of parameter set you wish to generate. If not
    88  provided, this will default to the bundle name. By default, Porter will
    89  generate a parameter set for the bundle in the current directory. You may also
    90  specify a bundle with --file.
    91  
    92  Bundles define 1 or more parameter(s) that are required to interact with a
    93  bundle. The bundle definition defines where the parameter should be delivered
    94  to the bundle, i.e. via DB_USERNAME. A parameter set, on the other hand,
    95  represents the source data that you wish to use when interacting with the
    96  bundle. These will typically be environment variables or files on your local
    97  file system.
    98  
    99  When you wish to install, upgrade or delete a bundle, Porter will use the
   100  parameter set to determine where to read the necessary information from and
   101  will then provide it to the bundle in the correct location. `,
   102  		Example: `  porter parameters generate
   103    porter parameters generate myparamset --reference getporter/hello-llama:v0.1.1 --namespace dev
   104    porter parameters generate myparamset --label owner=myname --reference getporter/hello-llama:v0.1.1
   105    porter parameters generate myparamset --reference localhost:5000/getporter/hello-llama:v0.1.1 --insecure-registry --force
   106    porter parameters generate myparamset --file myapp/porter.yaml
   107    porter parameters generate myparamset --cnab-file myapp/bundle.json
   108  `,
   109  		PreRunE: func(cmd *cobra.Command, args []string) error {
   110  			return opts.Validate(cmd.Context(), args, p)
   111  		},
   112  		RunE: func(cmd *cobra.Command, args []string) error {
   113  			return p.GenerateParameters(cmd.Context(), opts)
   114  		},
   115  	}
   116  
   117  	f := cmd.Flags()
   118  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
   119  		"Namespace in which the parameter set is defined. Defaults to the global namespace.")
   120  	f.StringSliceVarP(&opts.Labels, "label", "l", nil,
   121  		"Associate the specified labels with the parameter set. May be specified multiple times.")
   122  	addBundleDefinitionFlags(f, &opts.BundleDefinitionOptions)
   123  	addBundlePullFlags(f, &opts.BundlePullOptions)
   124  
   125  	return cmd
   126  }
   127  
   128  func buildParametersListCommand(p *porter.Porter) *cobra.Command {
   129  	opts := porter.ListOptions{}
   130  
   131  	cmd := &cobra.Command{
   132  		Use:     "list",
   133  		Aliases: []string{"ls"},
   134  		Short:   "List parameter sets",
   135  		Long: `List named sets of parameters defined by the user.
   136  
   137  Optionally filters the results name, which returns all results whose name contain the provided query.
   138  The results may also be filtered by associated labels and the namespace in which the parameter set is defined.`,
   139  		Example: `  porter parameters list
   140    porter parameters list --namespace prod -o json
   141    porter parameters list --all-namespaces,
   142    porter parameters list --name myapp
   143    porter parameters list --label env=dev
   144    porter parameters list --skip 2 --limit 2`,
   145  		PreRunE: func(cmd *cobra.Command, args []string) error {
   146  			return opts.Validate()
   147  		},
   148  		RunE: func(cmd *cobra.Command, args []string) error {
   149  			return p.PrintParameters(cmd.Context(), opts)
   150  		},
   151  	}
   152  
   153  	f := cmd.Flags()
   154  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
   155  		"Namespace in which the parameter set is defined. Defaults to the global namespace. Use * to list across all namespaces.")
   156  	f.BoolVar(&opts.AllNamespaces, "all-namespaces", false,
   157  		"Include all namespaces in the results.")
   158  	f.StringVar(&opts.Name, "name", "",
   159  		"Filter the parameter sets where the name contains the specified substring.")
   160  	f.StringSliceVarP(&opts.Labels, "label", "l", nil,
   161  		"Filter the parameter sets by a label formatted as: KEY=VALUE. May be specified multiple times.")
   162  	f.StringVarP(&opts.RawFormat, "output", "o", "plaintext",
   163  		"Specify an output format.  Allowed values: plaintext, json, yaml")
   164  	f.Int64Var(&opts.Skip, "skip", 0,
   165  		"Skip the number of parameter sets by a certain amount. Defaults to 0.")
   166  	f.Int64Var(&opts.Limit, "limit", 0,
   167  		"Limit the number of parameter sets by a certain amount. Defaults to 0.")
   168  
   169  	return cmd
   170  }
   171  
   172  func buildParametersDeleteCommand(p *porter.Porter) *cobra.Command {
   173  	opts := porter.ParameterDeleteOptions{}
   174  
   175  	cmd := &cobra.Command{
   176  		Use:   "delete NAME",
   177  		Short: "Delete a Parameter Set",
   178  		Long:  `Delete a named parameter set.`,
   179  		PreRunE: func(_ *cobra.Command, args []string) error {
   180  			return opts.Validate(args)
   181  		},
   182  		RunE: func(cmd *cobra.Command, _ []string) error {
   183  			return p.DeleteParameter(cmd.Context(), opts)
   184  		},
   185  	}
   186  
   187  	f := cmd.Flags()
   188  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
   189  		"Namespace in which the parameter set is defined. Defaults to the global namespace.")
   190  
   191  	return cmd
   192  }
   193  
   194  func buildParametersShowCommand(p *porter.Porter) *cobra.Command {
   195  	opts := porter.ParameterShowOptions{}
   196  
   197  	cmd := &cobra.Command{
   198  		Use:     "show",
   199  		Short:   "Show a Parameter Set",
   200  		Long:    `Show a named parameter set, including all named parameters and their corresponding mappings.`,
   201  		Example: `  porter parameter show NAME [-o table|json|yaml]`,
   202  		PreRunE: func(cmd *cobra.Command, args []string) error {
   203  			return opts.Validate(args)
   204  		},
   205  		RunE: func(cmd *cobra.Command, args []string) error {
   206  			return p.ShowParameter(cmd.Context(), opts)
   207  		},
   208  	}
   209  
   210  	f := cmd.Flags()
   211  	f.StringVarP(&opts.Namespace, "namespace", "n", "",
   212  		"Namespace in which the parameter set is defined. Defaults to the global namespace.")
   213  	f.StringVarP(&opts.RawFormat, "output", "o", "plaintext",
   214  		"Specify an output format.  Allowed values: plaintext, json, yaml")
   215  
   216  	return cmd
   217  }
   218  
   219  func buildParametersCreateCommand(p *porter.Porter) *cobra.Command {
   220  	opts := porter.ParameterCreateOptions{}
   221  
   222  	cmd := &cobra.Command{
   223  		Use:   "create",
   224  		Short: "Create a Parameter Set",
   225  		Long:  "Create a new blank resource for the definition of a Parameter Set.",
   226  		Example: `
   227  		porter parameters create FILE [--output yaml|json]
   228  		porter parameters create parameter-set.json
   229  		porter parameters create parameter-set --output yaml`,
   230  		PreRunE: func(cmd *cobra.Command, args []string) error {
   231  			return opts.Validate(args)
   232  		},
   233  		RunE: func(cmd *cobra.Command, argrs []string) error {
   234  			return p.CreateParameter(opts)
   235  		},
   236  	}
   237  
   238  	f := cmd.Flags()
   239  	f.StringVar(&opts.OutputType, "output", "", "Parameter set resource file format")
   240  
   241  	return cmd
   242  }