Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config file read only #6

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,3 @@ func readConfig(filename string) (*Config, error) {

return c, nil
}

func writeConfig(filename string, config *Config) error {
data, err := yaml.Marshal(config)
if err != nil {
return err
}

return os.WriteFile(filename, data, 0644)
}
26 changes: 4 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ import (
_ "github.com/lib/pq"
)

func generatePassword() (string, error) {
bytes := make([]byte, 16)
if _, err := rand.Read(bytes); err != nil {
return "", err
}

return hex.EncodeToString(bytes), nil
}

func connectDB(username, password, dbhost, dbname string) (*sql.DB, error) {
return connectDBWithRetry(username, password, dbhost, dbname, 2)
}
Expand Down Expand Up @@ -60,7 +51,10 @@ func execSQL(db *sql.DB, query string) error {

func main() {

config, err := readConfig("ochami.yaml")
if config_file == "" {
log.Fatal("OCHAMI_CONFIG is required")
}
config, err := readConfig(config_file)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -98,13 +92,6 @@ func main() {
}

for _, user := range database.Users {
if user.Password == "" {
user.Password, err = generatePassword()
if err != nil {
log.Fatal(err)
}
}

err = execSQL(db, fmt.Sprintf("CREATE USER \"%s\" WITH PASSWORD '%s';", user.Name, user.Password))
if err != nil {
log.Fatal(err)
Expand All @@ -116,9 +103,4 @@ func main() {
}
}
}

err = writeConfig("ochami.yaml", config)
if err != nil {
log.Fatal(err)
}
}