feat: add install config using my own copy pkg

main
Tran Hau 2021-01-18 15:29:01 +07:00
parent e130245287
commit dae2c42252
4 changed files with 46 additions and 5 deletions

View File

@ -7,6 +7,8 @@ import (
"io/ioutil"
"os"
"path/filepath"
"github.com/haunt98/copy-go"
)
type Config struct {
@ -18,13 +20,13 @@ type Config struct {
}
type App struct {
Files []FromToPath `json:"files"`
Folders []FromToPath `json:"folders"`
Files []Path `json:"files"`
Dirs []Path `json:"dirs"`
}
type FromToPath struct {
From string `json:"from"`
To string `json:"to"`
type Path struct {
Internal string `json:"internal"`
External string `json:"external"`
}
// Load config from file
@ -55,6 +57,38 @@ func LoadConfig(path string) (result Config, err error) {
return
}
// internal -> external
func (c *Config) Install() error {
for _, app := range c.Apps {
for _, file := range app.Files {
if err := os.RemoveAll(file.External); err != nil {
return fmt.Errorf("failed to remove %s: %w", file.External, err)
}
if err := copy.CopyFile(file.Internal, file.External); err != nil {
return fmt.Errorf("failed to copy from %s to %s: %w", file.Internal, file.External, err)
}
}
for _, dir := range app.Dirs {
if err := os.RemoveAll(dir.External); err != nil {
return fmt.Errorf("failed to remove %s: %w", dir.External, err)
}
if err := copy.CopyDir(dir.Internal, dir.External); err != nil {
return fmt.Errorf("failed to copy from %s to %s: %w", dir.Internal, dir.External, err)
}
}
}
return nil
}
// external -> internal
func (c *Config) Update() error {
return nil
}
func getConfigPath(path string) string {
return filepath.Join(path, "configs/config.json")
}

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.15
require (
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/fatih/color v1.10.0
github.com/haunt98/copy-go v0.1.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/cli/v2 v2.3.0
)

2
go.sum
View File

@ -5,6 +5,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/haunt98/copy-go v0.1.0 h1:h0WkcxKvprqH5whHUGz8wnekY7JTydayPCqRvS5Mz1s=
github.com/haunt98/copy-go v0.1.0/go.mod h1:4PtGf1Fl1ocrD9OhHyqAsBCA9UPuBPnUMm6RIJUe6vE=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=

View File

@ -71,6 +71,10 @@ func (a *action) RunInstall(c *cli.Context) error {
return fmt.Errorf("failed to load config: %w", err)
}
if err := cfg.Install(); err != nil {
return fmt.Errorf("failed to install config: %w", err)
}
return nil
}