-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move entity-like structs into entity package
- Loading branch information
Showing
47 changed files
with
261 additions
and
262 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,11 @@ | ||
package base | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"os" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/femnad/fup/base/settings" | ||
"github.com/femnad/fup/entity" | ||
"github.com/femnad/fup/internal" | ||
"github.com/femnad/fup/remote" | ||
) | ||
|
||
type PackageSpec []entity.PackageGroup | ||
type RemotePackageSpec []entity.RemotePackageGroup | ||
type UserInGroupSpec map[string][]entity.Group | ||
|
||
type Config struct { | ||
file string | ||
isRemote bool | ||
AcceptHostKeys []string `yaml:"host_key"` | ||
AptRepos []entity.AptRepo `yaml:"apt_repo"` | ||
Archives []Archive `yaml:"archive"` | ||
Binaries []entity.Binary `yaml:"binary"` | ||
Cargo []CargoPkg `yaml:"rust"` | ||
DnfRepos []entity.DnfRepo `yaml:"dnf_repo"` | ||
EnsureDirs []entity.Dir `yaml:"dir"` | ||
EnsureLines []LineInFile `yaml:"line"` | ||
Flatpak entity.Flatpak `yaml:"flatpak"` | ||
GithubUserKey UserKey `yaml:"github_key"` | ||
Go []entity.GoPkgGroup `yaml:"go"` | ||
Packages PackageSpec `yaml:"package"` | ||
PostflightTasks []Task `yaml:"postflight"` | ||
PreflightTasks []Task `yaml:"preflight"` | ||
Python []PythonPkg `yaml:"python"` | ||
RemotePackages RemotePackageSpec `yaml:"remote_package"` | ||
RepoGroups []entity.RepoGroup `yaml:"repo"` | ||
Services []Service `yaml:"service"` | ||
Settings settings.Settings `yaml:"settings"` | ||
SnapPackages []entity.Snap `yaml:"snap"` | ||
Tasks []Task `yaml:"task"` | ||
Templates []Template `yaml:"template"` | ||
UserInGroup UserInGroupSpec `yaml:"user_group"` | ||
} | ||
|
||
func (c Config) IsRemote() bool { | ||
return c.isRemote | ||
} | ||
|
||
func (c Config) File() string { | ||
return c.file | ||
} | ||
|
||
type configReader struct { | ||
reader io.Reader | ||
isRemote bool | ||
} | ||
|
||
func readLocalConfigFile(config string) (io.Reader, error) { | ||
f, err := os.Open(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return f, nil | ||
} | ||
|
||
func readRemoteConfigFile(config string) (io.Reader, error) { | ||
response, err := remote.ReadResponseBody(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return response.Body, nil | ||
} | ||
|
||
func getConfigReader(config string) (configReader, error) { | ||
parsed, err := url.Parse(config) | ||
if err != nil { | ||
return configReader{}, err | ||
} | ||
|
||
var readerFn func(string) (io.Reader, error) | ||
var isRemote bool | ||
if parsed.Scheme == "" { | ||
readerFn = readLocalConfigFile | ||
isRemote = false | ||
} else { | ||
readerFn = readRemoteConfigFile | ||
isRemote = true | ||
} | ||
|
||
reader, err := readerFn(config) | ||
if err != nil { | ||
return configReader{}, err | ||
} | ||
|
||
return configReader{reader: reader, isRemote: isRemote}, nil | ||
} | ||
|
||
func decodeConfig(filename string) (Config, error) { | ||
config := Config{} | ||
cfgReader, err := getConfigReader(filename) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
data, err := io.ReadAll(cfgReader.reader) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
err = yaml.Unmarshal(data, &config) | ||
if err != nil { | ||
return config, fmt.Errorf("error deserializing config from %s: %v", filename, err) | ||
} | ||
|
||
config.isRemote = cfgReader.isRemote | ||
config.file = filename | ||
return config, nil | ||
} | ||
|
||
func ReadConfig(filename string) (Config, error) { | ||
func ReadConfig(filename string) (entity.Config, error) { | ||
filename = internal.ExpandUser(filename) | ||
return decodeConfig(filename) | ||
return entity.UnmarshalConfig(filename) | ||
} |
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
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,122 @@ | ||
package entity | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"os" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/femnad/fup/remote" | ||
"github.com/femnad/fup/settings" | ||
) | ||
|
||
type Config struct { | ||
file string | ||
isRemote bool | ||
AcceptHostKeys []string `yaml:"host_key"` | ||
AptRepos []AptRepo `yaml:"apt_repo"` | ||
Archives []Archive `yaml:"archive"` | ||
Binaries []Binary `yaml:"binary"` | ||
Cargo []CargoPkg `yaml:"rust"` | ||
DnfRepos []DnfRepo `yaml:"dnf_repo"` | ||
EnsureDirs []Dir `yaml:"dir"` | ||
EnsureLines []LineInFile `yaml:"line"` | ||
Flatpak Flatpak `yaml:"flatpak"` | ||
GithubUserKey UserKey `yaml:"github_key"` | ||
Go []GoPkgGroup `yaml:"go"` | ||
Packages PackageSpec `yaml:"package"` | ||
PostflightTasks []Task `yaml:"postflight"` | ||
PreflightTasks []Task `yaml:"preflight"` | ||
Python []PythonPkg `yaml:"python"` | ||
RemotePackages RemotePackageSpec `yaml:"remote_package"` | ||
RepoGroups []RepoGroup `yaml:"repo"` | ||
Services []Service `yaml:"service"` | ||
Settings settings.Settings `yaml:"settings"` | ||
SnapPackages []Snap `yaml:"snap"` | ||
Tasks []Task `yaml:"task"` | ||
TaskGroups []TaskGroup `yaml:"task_group"` | ||
Templates []Template `yaml:"template"` | ||
UserInGroup UserInGroupSpec `yaml:"user_group"` | ||
} | ||
|
||
type PackageSpec []PackageGroup | ||
type RemotePackageSpec []RemotePackageGroup | ||
type UserInGroupSpec map[string][]Group | ||
|
||
func (c Config) IsRemote() bool { | ||
return c.isRemote | ||
} | ||
|
||
func (c Config) File() string { | ||
return c.file | ||
} | ||
|
||
type configReader struct { | ||
reader io.Reader | ||
isRemote bool | ||
} | ||
|
||
func readLocalConfigFile(config string) (io.Reader, error) { | ||
f, err := os.Open(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return f, nil | ||
} | ||
|
||
func readRemoteConfigFile(config string) (io.Reader, error) { | ||
response, err := remote.ReadResponseBody(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return response.Body, nil | ||
} | ||
|
||
func getConfigReader(config string) (configReader, error) { | ||
parsed, err := url.Parse(config) | ||
if err != nil { | ||
return configReader{}, err | ||
} | ||
|
||
var readerFn func(string) (io.Reader, error) | ||
var isRemote bool | ||
if parsed.Scheme == "" { | ||
readerFn = readLocalConfigFile | ||
isRemote = false | ||
} else { | ||
readerFn = readRemoteConfigFile | ||
isRemote = true | ||
} | ||
|
||
reader, err := readerFn(config) | ||
if err != nil { | ||
return configReader{}, err | ||
} | ||
|
||
return configReader{reader: reader, isRemote: isRemote}, nil | ||
} | ||
|
||
func UnmarshalConfig(filename string) (Config, error) { | ||
config := Config{} | ||
cfgReader, err := getConfigReader(filename) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
data, err := io.ReadAll(cfgReader.reader) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
err = yaml.Unmarshal(data, &config) | ||
if err != nil { | ||
return config, fmt.Errorf("error deserializing config from %s: %v", filename, err) | ||
} | ||
|
||
config.isRemote = cfgReader.isRemote | ||
config.file = filename | ||
return config, 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package base | ||
package entity | ||
|
||
type Replacement struct { | ||
Old string `yaml:"old"` | ||
|
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
Oops, something went wrong.