fix: base path flag

This commit is contained in:
Kevin Franklin Kim 2024-03-22 16:09:33 +01:00
parent d9a8248c0a
commit 5e7798a53f
No known key found for this signature in database
2 changed files with 15 additions and 17 deletions

View File

@ -43,7 +43,7 @@ func basePathFlag(v *viper.Viper) string {
func addBasePathFlag(flags *pflag.FlagSet, v *viper.Viper) { func addBasePathFlag(flags *pflag.FlagSet, v *viper.Viper) {
flags.String("base-path", "/contentserver", "Base path to export the webserver on") flags.String("base-path", "/contentserver", "Base path to export the webserver on")
_ = v.BindPFlag("base_path", flags.Lookup("base_path")) _ = v.BindPFlag("base_path", flags.Lookup("base-path"))
_ = v.BindEnv("base_path", "CONTENT_SERVER_BASE_PATH") _ = v.BindEnv("base_path", "CONTENT_SERVER_BASE_PATH")
} }

View File

@ -2,7 +2,6 @@ package repo
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -11,6 +10,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/pkg/errors"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -67,27 +67,25 @@ func NewHistory(l *zap.Logger, opts ...HistoryOption) *History {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
func (h *History) Add(jsonBytes []byte) error { func (h *History) Add(jsonBytes []byte) error {
var ( var filename = path.Join(h.historyDir, HistoryRepoJSONPrefix+time.Now().Format(time.RFC3339Nano)+HistoryRepoJSONSuffix)
// historiy file name
filename = path.Join(h.historyDir, HistoryRepoJSONPrefix+time.Now().Format(time.RFC3339Nano)+HistoryRepoJSONSuffix) if err := os.MkdirAll(path.Dir(filename), 0700); err != nil {
err = os.WriteFile(filename, jsonBytes, 0600) return errors.Wrap(err, "failed to create history dir")
) }
if err != nil {
return err if err := os.WriteFile(filename, jsonBytes, 0600); err != nil {
return errors.Wrap(err, "failed to write history")
} }
h.l.Debug("adding content backup", zap.String("file", filename)) h.l.Debug("adding content backup", zap.String("file", filename))
// current filename // current filename
err = os.WriteFile(h.GetCurrentFilename(), jsonBytes, 0600) if err := os.WriteFile(h.GetCurrentFilename(), jsonBytes, 0600); err != nil {
if err != nil { return errors.Wrap(err, "failed to write current history")
return err
} }
err = h.cleanup() if err := h.cleanup(); err != nil {
if err != nil { return errors.Wrap(err, "failed to clean up history")
h.l.Error("an error occurred while cleaning up my history", zap.Error(err))
return err
} }
return nil return nil
@ -97,7 +95,7 @@ func (h *History) GetCurrentFilename() string {
return path.Join(h.historyDir, HistoryRepoJSONPrefix+"current"+HistoryRepoJSONSuffix) return path.Join(h.historyDir, HistoryRepoJSONPrefix+"current"+HistoryRepoJSONSuffix)
} }
func (h *History) GetCurrent(buf *bytes.Buffer) (err error) { func (h *History) GetCurrent(buf *bytes.Buffer) error {
f, err := os.Open(h.GetCurrentFilename()) f, err := os.Open(h.GetCurrentFilename())
if err != nil { if err != nil {
return err return err