func newPropertyCmd()

in bydbctl/internal/cmd/property.go [41:121]


func newPropertyCmd() *cobra.Command {
	propertyCmd := &cobra.Command{
		Use:     "property",
		Version: version.Build(),
		Short:   "Property operation",
	}

	applyCmd := &cobra.Command{
		Use:     "apply -f [file|dir|-]",
		Version: version.Build(),
		Short:   "Apply(Create or Update) properties from files",
		RunE: func(cmd *cobra.Command, _ []string) error {
			return rest(func() ([]reqBody, error) { return parseFromYAMLForProperty(cmd.InOrStdin()) },
				func(request request) (*resty.Response, error) {
					s := new(propertyv1.Property)
					err := protojson.Unmarshal(request.data, s)
					if err != nil {
						return nil, err
					}
					cr := &propertyv1.ApplyRequest{
						Property: s,
						Strategy: propertyv1.ApplyRequest_STRATEGY_MERGE,
					}
					b, err := protojson.Marshal(cr)
					if err != nil {
						return nil, err
					}
					return request.req.SetPathParam("group", request.group).SetPathParam("name", request.name).
						SetPathParam("id", request.id).SetBody(b).Put(getPath(propertySchemaPathWithoutTagParams))
				}, yamlPrinter)
		},
	}
	bindFileFlag(applyCmd)

	getCmd := &cobra.Command{
		Use:     "get [-g group] -n name -i id [-t tags]",
		Version: version.Build(),
		Short:   "Get a property",
		RunE: func(_ *cobra.Command, _ []string) (err error) {
			return rest(parseFromFlags, func(request request) (*resty.Response, error) {
				return request.req.SetPathParam("name", request.name).SetPathParam("group", request.group).
					SetPathParam("id", request.id).SetPathParam("tag", request.tags()).
					Get(getPath(propertySchemaPathWithTagParams))
			}, yamlPrinter)
		},
	}

	deleteCmd := &cobra.Command{
		Use:     "delete [-g group] -n name -i id -t [tags]",
		Version: version.Build(),
		Short:   "Delete a property",
		RunE: func(_ *cobra.Command, _ []string) (err error) {
			return rest(parseFromFlags, func(request request) (*resty.Response, error) {
				return request.req.SetPathParam("name", request.name).SetPathParam("group", request.group).
					SetPathParam("id", request.id).SetPathParam("tag", request.tags()).Delete(getPath(propertySchemaPathWithTagParams))
			}, yamlPrinter)
		},
	}
	bindNameAndIDAndTagsFlag(getCmd, deleteCmd)

	listCmd := &cobra.Command{
		Use:     "list [-g group] -n name",
		Version: version.Build(),
		Short:   "List properties",
		RunE: func(_ *cobra.Command, _ []string) (err error) {
			return rest(parseFromFlags, func(request request) (*resty.Response, error) {
				if len(request.name) == 0 {
					return request.req.SetPathParam("group", request.group).Get(getPath(propertyListSchemaPath))
				}
				return request.req.SetPathParam("name", request.name).SetPathParam("group", request.group).
					SetPathParam("ids", request.ids()).SetPathParam("tags", request.tags()).Get(getPath(propertyListSchemaPathWithTagParams))
			}, yamlPrinter)
		},
	}
	listCmd.Flags().StringVarP(&name, "name", "n", "", "the name of the resource")
	listCmd.Flags().StringArrayVarP(&ids, "ids", "", nil, "id selector")
	listCmd.Flags().StringArrayVarP(&tags, "tags", "t", nil, "tag selector")

	propertyCmd.AddCommand(getCmd, applyCmd, deleteCmd, listCmd)
	return propertyCmd
}