From 201a7c6b0125761db91e4665b34c0d60c508c0d9 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Fri, 26 Jul 2024 10:34:11 +0200 Subject: [PATCH] reduce complexity Signed-off-by: Christian Richter --- ocis/pkg/init/functions.go | 41 +++++++++++++++++++++++++++++ ocis/pkg/init/init.go | 53 +++++++------------------------------- 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/ocis/pkg/init/functions.go b/ocis/pkg/init/functions.go index dcb63292231..a76fda1202d 100644 --- a/ocis/pkg/init/functions.go +++ b/ocis/pkg/init/functions.go @@ -5,6 +5,7 @@ import ( "io" "log" "os" + "os/exec" "path" "time" ) @@ -61,3 +62,43 @@ func printBanner(targetPath, ocisAdminServicePassword, targetBackupConfig string targetBackupConfig) } } + +// writeConfig writes the config to the target path and prints a banner +func writeConfig(configPath, ocisAdminServicePassword, targetBackupConfig string, yamlOutput []byte) error { + targetPath := path.Join(configPath, configFilename) + err := os.WriteFile(targetPath, yamlOutput, 0600) + if err != nil { + return err + } + printBanner(targetPath, ocisAdminServicePassword, targetBackupConfig) + return nil +} + +// writePatch writes the diff to a file +func writePatch(configPath string, yamlOutput []byte) error { + fmt.Println("running in diff mode") + tmpFile := path.Join(configPath, "ocis.yaml.tmp") + err := os.WriteFile(tmpFile, yamlOutput, 0600) + if err != nil { + return err + } + fmt.Println("diff -u " + path.Join(configPath, configFilename) + " " + tmpFile) + cmd := exec.Command("diff", "-u", path.Join(configPath, configFilename), tmpFile) + stdout, err := cmd.Output() + if err == nil { + fmt.Println("no changes, your config is up to date") + return nil + } + fmt.Println(string(stdout)) + err = os.Remove(tmpFile) + if err != nil { + return err + } + patchPath := path.Join(configPath, "ocis.config.patch") + err = os.WriteFile(patchPath, stdout, 0600) + if err != nil { + return err + } + fmt.Printf("diff written to %s\n", patchPath) + return nil +} diff --git a/ocis/pkg/init/init.go b/ocis/pkg/init/init.go index 2a399f7d28f..5c837774f3c 100644 --- a/ocis/pkg/init/init.go +++ b/ocis/pkg/init/init.go @@ -3,7 +3,6 @@ package init import ( "fmt" "os" - "os/exec" "path" "github.com/gofrs/uuid" @@ -23,19 +22,15 @@ var ( // CreateConfig creates a config file with random passwords at configPath func CreateConfig(insecure, forceOverwrite, diff bool, configPath, adminPassword string) error { - if diff { - if forceOverwrite { - return fmt.Errorf("diff and force-overwrite flags are mutually exclusive") - } - if adminPassword != "" { - return fmt.Errorf("diff and admin-password flags are mutually exclusive") - } + if diff && forceOverwrite { + return fmt.Errorf("diff and force-overwrite flags are mutually exclusive") + } + if diff && adminPassword != "" { + return fmt.Errorf("diff and admin-password flags are mutually exclusive") } - if configExists(configPath) { - if !forceOverwrite && !diff { - return fmt.Errorf("config file already exists, use --force-overwrite to overwrite or --diff to show diff") - } + if configExists(configPath) && !forceOverwrite && !diff { + return fmt.Errorf("config file already exists, use --force-overwrite to overwrite or --diff to show diff") } err := checkConfigPath(configPath) @@ -292,37 +287,7 @@ func CreateConfig(insecure, forceOverwrite, diff bool, configPath, adminPassword return fmt.Errorf("could not marshall config into yaml: %s", err) } if diff { - fmt.Println("running in diff mode") - tmpFile := path.Join(configPath, "ocis.yaml.tmp") - err = os.WriteFile(tmpFile, yamlOutput, 0600) - if err != nil { - return err - } - fmt.Println("diff -u " + path.Join(configPath, configFilename) + " " + tmpFile) - cmd := exec.Command("diff", "-u", path.Join(configPath, configFilename), tmpFile) - stdout, err := cmd.Output() - if err == nil { - fmt.Println("no changes, your config is up to date") - return nil - } - fmt.Println(string(stdout)) - err = os.Remove(tmpFile) - if err != nil { - return err - } - patchPath := path.Join(configPath, "ocis.config.patch") - err = os.WriteFile(patchPath, stdout, 0600) - if err != nil { - return err - } - fmt.Printf("diff written to %s\n", patchPath) - } else { - targetPath := path.Join(configPath, configFilename) - err = os.WriteFile(targetPath, yamlOutput, 0600) - if err != nil { - return err - } - printBanner(targetPath, ocisAdminServicePassword, targetBackupConfig) + return writePatch(configPath, yamlOutput) } - return nil + return writeConfig(configPath, ocisAdminServicePassword, targetBackupConfig, yamlOutput) }