From 3546c1d7f6bdc25158213f51b690d9c49f53b7a7 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sat, 10 Feb 2024 13:35:47 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Write=20config=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/config.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++ config/default.go | 27 +++++++++++++++++------ 2 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 cmd/config.go diff --git a/cmd/config.go b/cmd/config.go new file mode 100644 index 0000000..530dc6e --- /dev/null +++ b/cmd/config.go @@ -0,0 +1,56 @@ +/* +Copyright © 2024 Dan Jones + +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 . +*/ +package cmd + +import ( + "fmt" + "os" + + "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, + RunE: func(cmd *cobra.Command, args []string) error { + force, _ := cmd.Flags().GetBool("force") + if !force { + _, err := os.Stat(config.ConfigPath) + if !os.IsNotExist(err) { + return fmt.Errorf("%s already exists. Use -f to overwrite", config.ConfigPath) + } + } + f, err := os.Create(config.ConfigPath) + if err != nil { + return err + } + defer f.Close() + c := config.DefaultStr() + fmt.Fprint(f, c) + return nil + }, +} + +func init() { + rootCmd.AddCommand(configCmd) + + configCmd.Flags().BoolP("force", "f", false, "Force overwrite") +} diff --git a/config/default.go b/config/default.go index d60f003..04aacf2 100644 --- a/config/default.go +++ b/config/default.go @@ -8,23 +8,36 @@ import ( "github.com/BurntSushi/toml" ) -const ConfigStr = ` -[input] -ext = "txt" -recurse = true -path = "%s" +const ConfigStr = `# Configuration for my-log +[input] +# Path to where the log files are stored +path = "%s" +# File extension for log files +ext = "txt" +# Whether to look in sub-folders +recurse = true + +# config for output types [output] + +# This one just prints the logs to stdout when run [output.stdout] enabled = true [output.stdout.config] +# Whether to output as JSON. Maybe useful to pipe elsewhere. json = false + ` -func DefaultConfig() (Config, error) { +func DefaultStr() string { home, _ := os.UserHomeDir() inDir := fp.Join(home, "my-log") - s := fmt.Sprintf(ConfigStr, inDir) + return fmt.Sprintf(ConfigStr, inDir) +} + +func DefaultConfig() (Config, error) { + s := DefaultStr() c := Config{} _, err := toml.Decode(s, &c) return c, err