github.com/evanlouie/fabrikate@v0.17.4/cmd/add.go (about) 1 package cmd 2 3 import ( 4 "errors" 5 "os" 6 "strings" 7 8 "github.com/evanlouie/fabrikate/core" 9 "github.com/evanlouie/fabrikate/logger" 10 "github.com/kyokomi/emoji" 11 "github.com/spf13/cobra" 12 ) 13 14 // Add implements the 'add' command in Fabrikate. It takes a spec for the new subcomponent, loads 15 // the previous component (if any), adds the subcomponent, and serializes the new component back out. 16 func Add(subcomponent core.Component) (err error) { 17 component := core.Component{ 18 PhysicalPath: "./", 19 LogicalPath: "", 20 } 21 22 component, err = component.LoadComponent() 23 if err != nil { 24 path, err := os.Getwd() 25 if err != nil { 26 return err 27 } 28 29 pathParts := strings.Split(path, "/") 30 31 component = core.Component{ 32 Name: pathParts[len(pathParts)-1], 33 Serialization: "yaml", 34 } 35 } 36 37 err = component.AddSubcomponent(subcomponent) 38 if err != nil { 39 return err 40 } 41 42 return component.Write() 43 } 44 45 var addCmd = &cobra.Command{ 46 Use: "add <component-name> --source <component-source> [--type <component|helm|static>] [--method <git|helm|local|http>] [--path <filepath>] [--version <SHA|tag|helm_chart_version>]", 47 Short: "Adds a subcomponent to the current component (or the component specified by the passed path).", 48 Long: `Adds a subcomponent to the current component (or the component specified by the passed path). 49 50 source: where the component lives (either a local path or remote http(s) endpoint) 51 type: the type of component (component (default), helm, or static) 52 method: method used to fetch the component (git (default)) 53 path: the path to the component that this subcomponent should be added to. 54 55 example: 56 57 $ fab add cloud-native --source https://github.com/evanlouie/fabrikate-definitions --path definitions/fabrikate-cloud-native --branch master --version v1.0.0 58 `, 59 RunE: func(cmd *cobra.Command, args []string) error { 60 if len(args) < 1 { 61 return errors.New("'add' takes one or more key=value arguments") 62 } 63 64 // If method not "git", set branch to zero value 65 method := cmd.Flag("method").Value.String() 66 branch := cmd.Flag("branch").Value.String() 67 if cmd.Flags().Changed("method") && method != "git" { 68 // Warn users if they explicitly set --branch that the config is being removed 69 if cmd.Flags().Changed("branch") { 70 logger.Warn(emoji.Sprintf(":exclamation: Non 'git' --method and explicit --branch specified. Removing --branch configuration of 'branch: %s'", branch)) 71 } 72 branch = "" 73 } 74 75 component := core.Component{ 76 Name: args[0], 77 Source: cmd.Flag("source").Value.String(), 78 Method: method, 79 Branch: branch, 80 Version: cmd.Flag("version").Value.String(), 81 Path: cmd.Flag("path").Value.String(), 82 ComponentType: cmd.Flag("type").Value.String(), 83 } 84 85 return Add(component) 86 }, 87 } 88 89 func init() { 90 addCmd.PersistentFlags().String("source", "", "Source for this component") 91 addCmd.PersistentFlags().String("method", "git", "Method to use to fetch this component") 92 addCmd.PersistentFlags().String("branch", "master", "Branch of git repo to use; noop when method is 'git'") 93 addCmd.PersistentFlags().String("path", "", "Path of git repo to use") 94 addCmd.PersistentFlags().String("type", "component", "Type of this component") 95 addCmd.PersistentFlags().String("version", "", "Commit SHA or Tag to checkout of the git repo when method is 'git' or the version of the helm chart to fetch when method is 'helm'") 96 97 rootCmd.AddCommand(addCmd) 98 }