From b60065f308380a9ef88910f192a98e17ee6b3967 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Fri, 26 Jul 2024 07:54:27 +0200 Subject: [PATCH] unclutter init pkg Signed-off-by: Christian Richter --- ocis/pkg/init/functions.go | 63 ++++++++++++++++++++++++++++++++++++++ ocis/pkg/init/init.go | 63 ++++---------------------------------- 2 files changed, 69 insertions(+), 57 deletions(-) create mode 100644 ocis/pkg/init/functions.go diff --git a/ocis/pkg/init/functions.go b/ocis/pkg/init/functions.go new file mode 100644 index 000000000..dcb632922 --- /dev/null +++ b/ocis/pkg/init/functions.go @@ -0,0 +1,63 @@ +package init + +import ( + "fmt" + "io" + "log" + "os" + "path" + "time" +) + +func checkConfigPath(configPath string) error { + targetPath := path.Join(configPath, configFilename) + if _, err := os.Stat(targetPath); err == nil { + return fmt.Errorf("config in %s already exists", targetPath) + } + return nil +} + +func configExists(configPath string) bool { + targetPath := path.Join(configPath, configFilename) + if _, err := os.Stat(targetPath); err == nil { + return true + } + return false +} + +func backupOcisConfigFile(configPath string) (string, error) { + sourceConfig := path.Join(configPath, configFilename) + targetBackupConfig := path.Join(configPath, configFilename+"."+time.Now().Format("2006-01-02-15-04-05")+".backup") + source, err := os.Open(sourceConfig) + if err != nil { + log.Fatalf("Could not read %s (%s)", sourceConfig, err) + } + defer source.Close() + target, err := os.Create(targetBackupConfig) + if err != nil { + log.Fatalf("Could not generate backup %s (%s)", targetBackupConfig, err) + } + defer target.Close() + _, err = io.Copy(target, source) + if err != nil { + log.Fatalf("Could not write backup %s (%s)", targetBackupConfig, err) + } + return targetBackupConfig, nil +} + +// printBanner prints the generated OCIS config banner. +func printBanner(targetPath, ocisAdminServicePassword, targetBackupConfig string) { + fmt.Printf( + "\n=========================================\n"+ + " generated OCIS Config\n"+ + "=========================================\n"+ + " configpath : %s\n"+ + " user : admin\n"+ + " password : %s\n\n", + targetPath, ocisAdminServicePassword) + if targetBackupConfig != "" { + fmt.Printf("\n=========================================\n"+ + "An older config file has been backuped to\n %s\n\n", + targetBackupConfig) + } +} diff --git a/ocis/pkg/init/init.go b/ocis/pkg/init/init.go index 8391e9b58..bc73d0d86 100644 --- a/ocis/pkg/init/init.go +++ b/ocis/pkg/init/init.go @@ -2,14 +2,11 @@ package init import ( "fmt" - "github.com/gofrs/uuid" - "io" - "log" "os" "os/exec" "path" - "time" + "github.com/gofrs/uuid" "github.com/owncloud/ocis/v2/ocis-pkg/generators" "gopkg.in/yaml.v2" ) @@ -24,42 +21,6 @@ var ( _insecureEvents = Events{TLSInsecure: true} ) -func checkConfigPath(configPath string) error { - targetPath := path.Join(configPath, configFilename) - if _, err := os.Stat(targetPath); err == nil { - return fmt.Errorf("config in %s already exists", targetPath) - } - return nil -} - -func configExists(configPath string) bool { - targetPath := path.Join(configPath, configFilename) - if _, err := os.Stat(targetPath); err == nil { - return true - } - return false -} - -func backupOcisConfigFile(configPath string) (string, error) { - sourceConfig := path.Join(configPath, configFilename) - targetBackupConfig := path.Join(configPath, configFilename+"."+time.Now().Format("2006-01-02-15-04-05")+".backup") - source, err := os.Open(sourceConfig) - if err != nil { - log.Fatalf("Could not read %s (%s)", sourceConfig, err) - } - defer source.Close() - target, err := os.Create(targetBackupConfig) - if err != nil { - log.Fatalf("Could not generate backup %s (%s)", targetBackupConfig, err) - } - defer target.Close() - _, err = io.Copy(target, source) - if err != nil { - log.Fatalf("Could not write backup %s (%s)", targetBackupConfig, err) - } - return targetBackupConfig, nil -} - // CreateConfig creates a config file with random passwords at configPath func CreateConfig(insecure, forceOverwrite, diff bool, configPath, adminPassword string) error { if diff { @@ -336,7 +297,11 @@ func CreateConfig(insecure, forceOverwrite, diff bool, configPath, adminPassword } fmt.Println("diff -u " + path.Join(configPath, configFilename) + " " + tmpFile) cmd := exec.Command("diff", "-u", path.Join(configPath, configFilename), tmpFile) - stdout, _ := cmd.Output() + 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) patchPath := path.Join(configPath, "ocis.config.patch") @@ -355,19 +320,3 @@ func CreateConfig(insecure, forceOverwrite, diff bool, configPath, adminPassword } return nil } - -func printBanner(targetPath, ocisAdminServicePassword, targetBackupConfig string) { - fmt.Printf( - "\n=========================================\n"+ - " generated OCIS Config\n"+ - "=========================================\n"+ - " configpath : %s\n"+ - " user : admin\n"+ - " password : %s\n\n", - targetPath, ocisAdminServicePassword) - if targetBackupConfig != "" { - fmt.Printf("\n=========================================\n"+ - "An older config file has been backuped to\n %s\n\n", - targetBackupConfig) - } -}