github.com/matthchr/controller-tools@v0.3.1-0.20200602225425-d33ced351ff8/cmd/type-scaffold/main.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/spf13/cobra"
    23  	"github.com/matthchr/controller-tools/pkg/typescaffold"
    24  )
    25  
    26  func main() {
    27  	opts := &typescaffold.ScaffoldOptions{
    28  		Resource: typescaffold.Resource{
    29  			Namespaced: true,
    30  		},
    31  	}
    32  	scaffoldCmd := &cobra.Command{
    33  		Use:   "type-scaffold",
    34  		Short: "Quickly scaffold out basic bits of a Kubernetes type.",
    35  		Long: `Quickly scaffold out the structure of a type for a Kubernetes kind and associated types.
    36  Produces:
    37  
    38  - a root type with appropriate metadata fields
    39  - Spec and Status types
    40  - a list type
    41  
    42  Also applies the appropriate comments to generate the code required to conform to runtime.Object.`,
    43  		Example: `	# Generate types for a Kind called Foo with a resource called foos
    44  		type-scaffold --kind Foo
    45  
    46  	# Generate types for a Kind called Bar with a resource of foobars
    47  	type-scaffold --kind Bar --resource foobars`,
    48  		RunE: func(_ *cobra.Command, _ []string) error {
    49  			if err := opts.Validate(); err != nil {
    50  				return err
    51  			}
    52  
    53  			return opts.Scaffold(os.Stdout)
    54  		},
    55  	}
    56  
    57  	scaffoldCmd.Flags().StringVar(&opts.Resource.Kind, "kind", opts.Resource.Kind, "The kind of the typescaffold being scaffolded.")
    58  	scaffoldCmd.Flags().StringVar(&opts.Resource.Resource, "resource", opts.Resource.Resource, "The resource of the typescaffold being scaffolded (defaults to a lower-case, plural version of kind).")
    59  	scaffoldCmd.Flags().BoolVar(&opts.Resource.Namespaced, "namespaced", opts.Resource.Namespaced, "Whether or not the given resource is namespaced.")
    60  
    61  	if err := cobra.MarkFlagRequired(scaffoldCmd.Flags(), "kind"); err != nil {
    62  		panic("unable to mark --kind as required")
    63  	}
    64  
    65  	if err := scaffoldCmd.Execute(); err != nil {
    66  		if _, err := fmt.Fprintln(os.Stderr, err); err != nil {
    67  			// this would be exceedingly bizarre if we ever got here
    68  			panic("unable to write to error details to standard error")
    69  		}
    70  		os.Exit(1)
    71  	}
    72  }