| 
									
										
										
										
											2021-01-20 15:34:40 +01:00
										 |  |  | package applyconfig | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/spf13/cobra" | 
					
						
							|  |  |  | 	"novit.nc/direktil/inits/pkg/apply" | 
					
						
							|  |  |  | 	"novit.nc/direktil/pkg/config" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dlog "novit.nc/direktil/pkg/log" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ( | 
					
						
							| 
									
										
										
										
											2023-02-01 18:51:44 +01:00
										 |  |  | 	filters []string | 
					
						
							|  |  |  | 	log     = dlog.Get("dkl") | 
					
						
							| 
									
										
										
										
											2021-01-20 15:34:40 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func Command() (c *cobra.Command) { | 
					
						
							|  |  |  | 	c = &cobra.Command{ | 
					
						
							|  |  |  | 		Use:   "apply-config <config.yaml>", | 
					
						
							|  |  |  | 		Short: "apply a config to the current system", | 
					
						
							|  |  |  | 		Args:  cobra.ExactArgs(1), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Run: run, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-01 18:51:44 +01:00
										 |  |  | 	flag := c.Flags() | 
					
						
							|  |  |  | 	flag.StringArrayVarP(&filters, "filter", "F", []string{}, "glob filter to select files to apply") | 
					
						
							| 
									
										
										
										
											2021-01-20 15:34:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return c | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func run(_ *cobra.Command, args []string) { | 
					
						
							|  |  |  | 	configPath := args[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							|  |  |  | 		cfg *config.Config | 
					
						
							|  |  |  | 		err error | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if configPath == "-" { | 
					
						
							|  |  |  | 		log.Print("loading config from stdin") | 
					
						
							|  |  |  | 		cfg, err = config.Read(os.Stdin) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		log.Print("loading config from ", configPath) | 
					
						
							|  |  |  | 		cfg, err = config.Load(configPath) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Print("failed to load config: ", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err = apply.Files(cfg /*log,*/, filters...); err != nil { | 
					
						
							|  |  |  | 		log.Taint(dlog.Fatal, "failed to apply files: ", err) | 
					
						
							|  |  |  | 		os.Exit(1) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |