Skip to content

Commit

Permalink
reduce complexity
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <[email protected]>
  • Loading branch information
dragonchaser committed Jul 26, 2024
1 parent a04cd2a commit 201a7c6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 44 deletions.
41 changes: 41 additions & 0 deletions ocis/pkg/init/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"log"
"os"
"os/exec"
"path"
"time"
)
Expand Down Expand Up @@ -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
}
53 changes: 9 additions & 44 deletions ocis/pkg/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package init
import (
"fmt"
"os"
"os/exec"
"path"

"github.com/gofrs/uuid"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

0 comments on commit 201a7c6

Please sign in to comment.