all repos — searchix @ 60a15699657cef86d1a644c0d13b0d117a818633

Search engine for NixOS, nix-darwin, home-manager and NUR users

feat: show default configuration with --print-default-config
Alan Pearce alan@alanpearce.eu
Mon, 20 May 2024 18:10:43 +0200
commit

60a15699657cef86d1a644c0d13b0d117a818633

parent

6037d5c33e9b98360f61a4fe4f09659d0e3e6160

2 files changed, 32 insertions(+), 4 deletions(-)

jump to
M internal/config/config.gointernal/config/config.go
@@ -180,6 +180,15 @@ }, 	},
 }
 
+func GetDefaultConfig() string {
+	out, err := toml.Marshal(&defaultConfig)
+	if err != nil {
+		panic("could not read default configuration")
+	}
+
+	return string(out)
+}
+
 func GetConfig(filename string) (*Config, error) {
 	config := defaultConfig
 	if filename != "" {
M searchix.gosearchix.go
@@ -23,10 +23,19 @@ var buildVersion string
 
 var (
-	configFile = flag.String("config", "config.toml", "config `file` to use")
+	configFile         = flag.String("config", "config.toml", "config `file` to use")
+	printDefaultConfig = flag.Bool(
+		"print-default-config",
+		false,
+		"print default configuration and exit",
+	)
 	liveReload = flag.Bool("live", false, "whether to enable live reloading (development)")
-	replace    = flag.Bool("replace", false, "whether to replace existing database, if it exists")
-	version    = flag.Bool("version", false, "print version information")
+	replace    = flag.Bool(
+		"replace",
+		false,
+		"whether to replace existing database, if it exists",
+	)
+	version = flag.Bool("version", false, "print version information")
 )
 
 func nextOccurrenceOfLocalTime(t toml.LocalTime) time.Time {
@@ -56,7 +65,17 @@ fmt.Fprintf(os.Stderr, "searchix %s", buildVersion) 		if buildVersion != config.CommitSHA && buildVersion != config.ShortSHA {
 			fmt.Fprintf(os.Stderr, " %s", config.CommitSHA)
 		}
-		fmt.Fprint(os.Stderr, "\n")
+		_, err := fmt.Fprint(os.Stderr, "\n")
+		if err != nil {
+			panic("can't write to standard error?!")
+		}
+		os.Exit(0)
+	}
+	if *printDefaultConfig {
+		_, err := fmt.Print(config.GetDefaultConfig())
+		if err != nil {
+			panic("can't write to standard output?!")
+		}
 		os.Exit(0)
 	}