| 
									
										
										
										
											2024-02-10 13:35:47 -06:00
										 |  |  | /* | 
					
						
							|  |  |  | Copyright © 2024 Dan Jones <danjones@goodevilgenius.org> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | (at your option) any later version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | GNU Affero General Public License for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | along with this program. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2024-02-25 15:53:37 -06:00
										 |  |  | 	fp "path/filepath" | 
					
						
							| 
									
										
										
										
											2024-02-10 13:35:47 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"codeberg.org/danjones000/my-log/config" | 
					
						
							|  |  |  | 	"github.com/spf13/cobra" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // configCmd represents the config command | 
					
						
							|  |  |  | var configCmd = &cobra.Command{ | 
					
						
							|  |  |  | 	Use:   "config", | 
					
						
							|  |  |  | 	Short: "Save default config to file", | 
					
						
							|  |  |  | 	//Long: ``, | 
					
						
							|  |  |  | 	SilenceUsage: true, | 
					
						
							| 
									
										
										
										
											2024-02-25 15:53:37 -06:00
										 |  |  | 	RunE: func(cmd *cobra.Command, args []string) (err error) { | 
					
						
							| 
									
										
										
										
											2024-02-10 13:35:47 -06:00
										 |  |  | 		force, _ := cmd.Flags().GetBool("force") | 
					
						
							|  |  |  | 		if !force { | 
					
						
							| 
									
										
										
										
											2024-02-25 15:53:37 -06:00
										 |  |  | 			_, err = os.Stat(config.ConfigPath) | 
					
						
							| 
									
										
										
										
											2024-02-10 13:35:47 -06:00
										 |  |  | 			if !os.IsNotExist(err) { | 
					
						
							|  |  |  | 				return fmt.Errorf("%s already exists. Use -f to overwrite", config.ConfigPath) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-02-25 15:53:37 -06:00
										 |  |  | 		dir := fp.Dir(config.ConfigPath) | 
					
						
							|  |  |  | 		err = os.MkdirAll(dir, 0755) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 13:35:47 -06:00
										 |  |  | 		f, err := os.Create(config.ConfigPath) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2024-02-25 15:53:37 -06:00
										 |  |  | 			return | 
					
						
							| 
									
										
										
										
											2024-02-10 13:35:47 -06:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		defer f.Close() | 
					
						
							|  |  |  | 		c := config.DefaultStr() | 
					
						
							|  |  |  | 		fmt.Fprint(f, c) | 
					
						
							| 
									
										
										
										
											2024-02-25 15:53:37 -06:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2024-02-10 13:35:47 -06:00
										 |  |  | 	}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							|  |  |  | 	rootCmd.AddCommand(configCmd) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	configCmd.Flags().BoolP("force", "f", false, "Force overwrite") | 
					
						
							|  |  |  | } |