-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage):
STORAGE_FILEPATH
option (#2416)
- `STORAGE_FILEPATH=` disables storing to and reading from a local servers.json file - `STORAGE_FILEPATH` defaults to `/gluetun/servers.json` - Fix #2074
- Loading branch information
Showing
7 changed files
with
82 additions
and
14 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
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,59 @@ | ||
package settings | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/qdm12/gosettings" | ||
"github.com/qdm12/gosettings/reader" | ||
"github.com/qdm12/gotree" | ||
) | ||
|
||
// Storage contains settings to configure the storage. | ||
type Storage struct { | ||
// Filepath is the path to the servers.json file. An empty string disables on-disk storage. | ||
Filepath *string | ||
} | ||
|
||
func (s Storage) validate() (err error) { | ||
if *s.Filepath != "" { // optional | ||
_, err := filepath.Abs(*s.Filepath) | ||
if err != nil { | ||
return fmt.Errorf("filepath is not valid: %w", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s *Storage) copy() (copied Storage) { | ||
return Storage{ | ||
Filepath: gosettings.CopyPointer(s.Filepath), | ||
} | ||
} | ||
|
||
func (s *Storage) overrideWith(other Storage) { | ||
s.Filepath = gosettings.OverrideWithPointer(s.Filepath, other.Filepath) | ||
} | ||
|
||
func (s *Storage) setDefaults() { | ||
const defaultFilepath = "/gluetun/servers.json" | ||
s.Filepath = gosettings.DefaultPointer(s.Filepath, defaultFilepath) | ||
} | ||
|
||
func (s Storage) String() string { | ||
return s.toLinesNode().String() | ||
} | ||
|
||
func (s Storage) toLinesNode() (node *gotree.Node) { | ||
if *s.Filepath == "" { | ||
return gotree.New("Storage settings: disabled") | ||
} | ||
node = gotree.New("Storage settings:") | ||
node.Appendf("Filepath: %s", *s.Filepath) | ||
return node | ||
} | ||
|
||
func (s *Storage) read(r *reader.Reader) (err error) { | ||
s.Filepath = r.Get("STORAGE_FILEPATH", reader.AcceptEmpty(true)) | ||
return nil | ||
} |
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