refactor: split config real and config demo

main
sudo pacman -Syu 2021-05-05 16:37:38 +07:00
parent f173917f48
commit a08fd5bf8e
3 changed files with 72 additions and 66 deletions

View File

@ -1,8 +1,67 @@
package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
)
const (
configDirPath = "data"
configFile = "data.json"
)
type Config interface {
Install() error
Update() error
Clean() error
Compare() error
}
type configApps struct {
Apps map[string]App `json:"apps"`
}
// Read from file
type App struct {
Paths []Path `json:"paths"`
}
type Path struct {
Internal string `json:"internal"`
External string `json:"external"`
}
// LoadConfig return config, configDemo
func LoadConfig(path string) (*configReal, *configDemo, error) {
configPath := getConfigPath(path)
bytes, err := os.ReadFile(configPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, nil, fmt.Errorf("file not exist %s: %w", configPath, err)
}
return nil, nil, fmt.Errorf("failed to read file%s: %w", configPath, err)
}
var cfgApps configApps
if err = json.Unmarshal(bytes, &cfgApps); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal: %w", err)
}
cfgReal := configReal{
configApps: cfgApps,
}
cfgDemo := configDemo{
configApps: cfgApps,
}
return &cfgReal, &cfgDemo, nil
}
func getConfigPath(path string) string {
return filepath.Join(path, configDirPath, configFile)
}

View File

@ -8,8 +8,8 @@ type configDemo struct {
var _ Config = (*configDemo)(nil)
func (cd *configDemo) Install() error {
for _, app := range cd.Apps {
func (c *configDemo) Install() error {
for _, app := range c.Apps {
for _, p := range app.Paths {
fmt.Printf("Replace %s -> %s\n", p.Internal, p.External)
}
@ -18,8 +18,8 @@ func (cd *configDemo) Install() error {
return nil
}
func (cd *configDemo) Update() error {
for _, app := range cd.Apps {
func (c *configDemo) Update() error {
for _, app := range c.Apps {
for _, p := range app.Paths {
fmt.Printf("Replace %s -> %s\n", p.External, p.Internal)
}
@ -28,8 +28,8 @@ func (cd *configDemo) Update() error {
return nil
}
func (cd *configDemo) Clean() error {
unusedDirs, err := getUnusedDirs(cd.Apps)
func (c *configDemo) Clean() error {
unusedDirs, err := getUnusedDirs(c.Apps)
if err != nil {
return err
}
@ -41,6 +41,6 @@ func (cd *configDemo) Clean() error {
return nil
}
func (cd *configDemo) Compare() error {
func (c *configDemo) Compare() error {
return nil
}

View File

@ -1,8 +1,6 @@
package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
@ -10,61 +8,14 @@ import (
"github.com/haunt98/copy-go"
)
const (
configDirPath = "data"
configFile = "data.json"
)
type config struct {
type configReal struct {
configApps
}
var _ Config = (*config)(nil)
type configApps struct {
Apps map[string]App `json:"apps"`
}
// Read from file
type App struct {
Paths []Path `json:"paths"`
}
type Path struct {
Internal string `json:"internal"`
External string `json:"external"`
}
// LoadConfig return config, configDemo
func LoadConfig(path string) (*config, *configDemo, error) {
configPath := getConfigPath(path)
bytes, err := os.ReadFile(configPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, nil, fmt.Errorf("file not exist %s: %w", configPath, err)
}
return nil, nil, fmt.Errorf("failed to read file%s: %w", configPath, err)
}
var cfgApps configApps
if err = json.Unmarshal(bytes, &cfgApps); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal: %w", err)
}
cfg := config{
configApps: cfgApps,
}
cfgDemo := configDemo{
configApps: cfgApps,
}
return &cfg, &cfgDemo, nil
}
var _ Config = (*configReal)(nil)
// Install internal -> external
func (c *config) Install() error {
func (c *configReal) Install() error {
for _, app := range c.Apps {
for _, p := range app.Paths {
if err := copy.Replace(p.Internal, p.External); err != nil {
@ -77,7 +28,7 @@ func (c *config) Install() error {
}
// Update external -> internal
func (c *config) Update() error {
func (c *configReal) Update() error {
for _, app := range c.Apps {
for _, p := range app.Paths {
if err := copy.Replace(p.External, p.Internal); err != nil {
@ -90,7 +41,7 @@ func (c *config) Update() error {
}
// Clean remove unused config inside config dir
func (c *config) Clean() error {
func (c *configReal) Clean() error {
unusedDirs, err := getUnusedDirs(c.Apps)
if err != nil {
return err
@ -107,7 +58,7 @@ func (c *config) Clean() error {
return nil
}
func (c *config) Compare() error {
func (c *configReal) Compare() error {
for _, app := range c.Apps {
for _, p := range app.Paths {
if err := copy.Compare(p.Internal, p.External); err != nil {
@ -119,10 +70,6 @@ func (c *config) Compare() error {
return nil
}
func getConfigPath(path string) string {
return filepath.Join(path, configDirPath, configFile)
}
func getUnusedDirs(apps map[string]App) (map[string]struct{}, error) {
files, err := os.ReadDir(configDirPath)
if err != nil {