github.com/evanlouie/fabrikate@v0.17.4/cmd/remove.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/evanlouie/fabrikate/core"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  // Remove implements the `remove` command. Taking in a list of subcomponent names, this function
    13  // will load the root component and attempt to remove any subcomponents with names matching
    14  // those provided.
    15  func Remove(subcomponent core.Component) (err error) {
    16  	component := core.Component{
    17  		PhysicalPath: "./",
    18  		LogicalPath:  "",
    19  	}
    20  
    21  	component, err = component.LoadComponent()
    22  	if err != nil {
    23  		path, err := os.Getwd()
    24  		if err != nil {
    25  			return err
    26  		}
    27  
    28  		pathParts := strings.Split(path, "/")
    29  
    30  		component = core.Component{
    31  			Name:          pathParts[len(pathParts)-1],
    32  			Serialization: "yaml",
    33  		}
    34  	}
    35  
    36  	err = component.RemoveSubcomponent(subcomponent)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	return component.Write()
    42  }
    43  
    44  var removeCmd = &cobra.Command{
    45  	Use:   "remove <component-name>",
    46  	Short: "Removes a subcomponent from the current component.",
    47  	Long: `Removes a subcomponent from the current component.
    48  
    49  example:
    50  
    51  $ fab remove fabrikate-cloud-native
    52  `,
    53  	RunE: func(cmd *cobra.Command, args []string) error {
    54  		if len(args) <= 0 {
    55  			return errors.New("'remove' takes one or more component-name arguments")
    56  		}
    57  
    58  		component := core.Component{
    59  			Name: args[0],
    60  		}
    61  
    62  		return Remove(component)
    63  	},
    64  }
    65  
    66  func init() {
    67  	rootCmd.AddCommand(removeCmd)
    68  }