-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge updater into the config package (#426)
* Merge updater into the config package * Rename config.NewUpdaterOptionsWithFlags() for clarity; update deps
- Loading branch information
1 parent
8dae06b
commit 750f6bd
Showing
6 changed files
with
60 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package config | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/Arriven/db1000n/src/utils" | ||
) | ||
|
||
// NewUpdaterOptionsWithFlags returns updater options initialized with command line flags. | ||
func NewUpdaterOptionsWithFlags() (updaterMode *bool, destinationPath *string) { | ||
return flag.Bool("updater-mode", utils.GetEnvBoolDefault("UPDATER_MODE", false), "Only run config updater"), | ||
flag.String("updater-destination-config", utils.GetEnvStringDefault("UPDATER_DESTINATION_CONFIG", "config/config.json"), | ||
"Destination config file to write (only applies if updater-mode is enabled") | ||
} | ||
|
||
func UpdateLocal(destinationPath string, configPaths []string, backupConfig []byte) { | ||
lastKnownConfig := &RawConfig{Body: backupConfig} | ||
|
||
for { | ||
if rawConfig := FetchRawConfig(configPaths, lastKnownConfig); !bytes.Equal(lastKnownConfig.Body, rawConfig.Body) { | ||
if err := writeConfig(rawConfig.Body, destinationPath); err != nil { | ||
log.Printf("Error writing config: %v", err) | ||
} | ||
|
||
return | ||
} | ||
|
||
time.Sleep(1 * time.Minute) | ||
} | ||
} | ||
|
||
func writeConfig(body []byte, destinationPath string) error { | ||
file, err := os.Create(destinationPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer file.Close() | ||
|
||
size, err := file.Write(body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("Saved %s with size %d", destinationPath, size) | ||
|
||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.