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 package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
)
const (
configDirPath = "data"
configFile = "data.json"
)
type Config interface { type Config interface {
Install() error Install() error
Update() error Update() error
Clean() error Clean() error
Compare() 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) var _ Config = (*configDemo)(nil)
func (cd *configDemo) Install() error { func (c *configDemo) Install() error {
for _, app := range cd.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
fmt.Printf("Replace %s -> %s\n", p.Internal, p.External) fmt.Printf("Replace %s -> %s\n", p.Internal, p.External)
} }
@ -18,8 +18,8 @@ func (cd *configDemo) Install() error {
return nil return nil
} }
func (cd *configDemo) Update() error { func (c *configDemo) Update() error {
for _, app := range cd.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
fmt.Printf("Replace %s -> %s\n", p.External, p.Internal) fmt.Printf("Replace %s -> %s\n", p.External, p.Internal)
} }
@ -28,8 +28,8 @@ func (cd *configDemo) Update() error {
return nil return nil
} }
func (cd *configDemo) Clean() error { func (c *configDemo) Clean() error {
unusedDirs, err := getUnusedDirs(cd.Apps) unusedDirs, err := getUnusedDirs(c.Apps)
if err != nil { if err != nil {
return err return err
} }
@ -41,6 +41,6 @@ func (cd *configDemo) Clean() error {
return nil return nil
} }
func (cd *configDemo) Compare() error { func (c *configDemo) Compare() error {
return nil return nil
} }

View File

@ -1,8 +1,6 @@
package config package config
import ( import (
"encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -10,61 +8,14 @@ import (
"github.com/haunt98/copy-go" "github.com/haunt98/copy-go"
) )
const ( type configReal struct {
configDirPath = "data"
configFile = "data.json"
)
type config struct {
configApps configApps
} }
var _ Config = (*config)(nil) var _ Config = (*configReal)(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
}
// Install internal -> external // Install internal -> external
func (c *config) Install() error { func (c *configReal) Install() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if err := copy.Replace(p.Internal, p.External); err != nil { if err := copy.Replace(p.Internal, p.External); err != nil {
@ -77,7 +28,7 @@ func (c *config) Install() error {
} }
// Update external -> internal // Update external -> internal
func (c *config) Update() error { func (c *configReal) Update() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if err := copy.Replace(p.External, p.Internal); err != nil { 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 // Clean remove unused config inside config dir
func (c *config) Clean() error { func (c *configReal) Clean() error {
unusedDirs, err := getUnusedDirs(c.Apps) unusedDirs, err := getUnusedDirs(c.Apps)
if err != nil { if err != nil {
return err return err
@ -107,7 +58,7 @@ func (c *config) Clean() error {
return nil return nil
} }
func (c *config) Compare() error { func (c *configReal) Compare() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if err := copy.Compare(p.Internal, p.External); err != nil { if err := copy.Compare(p.Internal, p.External); err != nil {
@ -119,10 +70,6 @@ func (c *config) Compare() error {
return nil return nil
} }
func getConfigPath(path string) string {
return filepath.Join(path, configDirPath, configFile)
}
func getUnusedDirs(apps map[string]App) (map[string]struct{}, error) { func getUnusedDirs(apps map[string]App) (map[string]struct{}, error) {
files, err := os.ReadDir(configDirPath) files, err := os.ReadDir(configDirPath)
if err != nil { if err != nil {