From 8d8cc94213611e0e9bf4ae40eb794506d2ef9616 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 9 Apr 2018 12:30:28 +0200 Subject: [PATCH] Added command to create empty settings --- src/calanonsync/settings.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/calanonsync/settings.go b/src/calanonsync/settings.go index 63b5eaf..b876168 100644 --- a/src/calanonsync/settings.go +++ b/src/calanonsync/settings.go @@ -131,7 +131,13 @@ over-the-shoulder "attacks".`, Run: runSettingsDecryption, } - settingsCmd.AddCommand(encryptCmd, decryptCmd) + initCmd := &cobra.Command{ + Use: "init", + Short: "Initialize an empty but valid settings file.", + Run: runSettingsInit, + } + + settingsCmd.AddCommand(encryptCmd, decryptCmd, initCmd) return settingsCmd } @@ -225,6 +231,30 @@ func runSettingsDecryption(cmd *cobra.Command, args []string) { log.Println("Settings decrypted") } +func runSettingsInit(cmd *cobra.Command, args []string) { + if _, err := os.Stat(settingsName); err == nil || os.IsExist(err) { + log.Fatalln("You already have a settings file! Remove that first if you really want a new blank one!") + } + + s := Settings{} + s.Anonymize.Title = &StringAnonSettings{ReplaceWith: "Replacement", Whitelist: []string{}} + + // Rewrite the settings file. + f, err := os.OpenFile(settingsName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600) + if err != nil { + log.Fatalf("Could not rewrite settings: %s\n", err) + } + defer f.Close() + e := json.NewEncoder(f) + e.SetIndent("", " ") + err = e.Encode(&s) + if err != nil { + panic(err) + } + + log.Println("Blank settings have been created.") +} + // Apply the anonymization rule to the given string, returning the // anonymized version. // If the anonymization is nil or empty, the original string will