wip
This commit is contained in:
parent
01932bdfc7
commit
b0fc471052
|
@ -0,0 +1,10 @@
|
|||
package actions
|
||||
|
||||
import (
|
||||
cmConfig "gitea.antoine-langlois.net/datahearth/config-mapper/internal/config"
|
||||
cmProviders "gitea.antoine-langlois.net/datahearth/config-mapper/internal/providers"
|
||||
)
|
||||
|
||||
func Load(cfg *cmConfig.Definition, providers ...cmProviders.Provider) error {
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
package actions
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
cmConfig "gitea.antoine-langlois.net/datahearth/config-mapper/internal/config"
|
||||
cmProviders "gitea.antoine-langlois.net/datahearth/config-mapper/internal/providers"
|
||||
)
|
||||
|
||||
func Save(logger, cfg *cmConfig.Definition, providers ...cmProviders.Provider) error {
|
||||
dstDir := path.Join(cfg.Path, cfg.UUID.String())
|
||||
for _, i := range cfg.Items {
|
||||
paths := strings.Split(i, ":")
|
||||
in, out := paths[0], path.Join(dstDir, paths[1])
|
||||
|
||||
s, err := os.Stat(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.IsDir() {
|
||||
if err := copyDir(in, out); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if err := copyFile(in, out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyDir(in string, out string) error {
|
||||
entries, err := os.ReadDir(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
return copyDir(path.Join(in, e.Name()), path.Join(out, e.Name()))
|
||||
}
|
||||
|
||||
if err := copyFile(path.Join(in, e.Name()), path.Join(out, e.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyFile(in string, out string) error {
|
||||
fIn, err := os.Open(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fIn.Close()
|
||||
|
||||
sIn, err := fIn.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fOut, err := os.OpenFile(out, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, sIn.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fOut.Close()
|
||||
|
||||
if _, err := io.Copy(fOut, fIn); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package actions
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
cmConfig "gitea.antoine-langlois.net/datahearth/config-mapper/internal/config"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Setup is the action to setup the configuration file
|
||||
func Setup() error {
|
||||
cfg := cmConfig.Definition{
|
||||
UUID: cmConfig.UUIDv4{UUID: uuid.New()},
|
||||
Path: "$HOME/.local/state/config-mapper",
|
||||
LogLevel: "info",
|
||||
Storage: cmConfig.Storage{},
|
||||
Items: []string{},
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(os.ExpandEnv(cmConfig.ConfigPath)), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return cfg.Write()
|
||||
}
|
|
@ -9,15 +9,42 @@ import (
|
|||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Definition struct {
|
||||
UUID uuid.UUID `yaml:"uuid"`
|
||||
Path string `yaml:"path,omitempty"`
|
||||
LogLevel string `yaml:"log-level,omitempty"`
|
||||
Storage Storage `yaml:"storage"`
|
||||
Items []string `yaml:"items"`
|
||||
const ConfigPath = "$HOME/.config/config-mapper.yml"
|
||||
|
||||
type UUIDv4 struct {
|
||||
uuid.UUID
|
||||
}
|
||||
|
||||
func Load(logger *cmLog.Logger) (*Definition, error) {
|
||||
func (u *UUIDv4) UnmarshalYAML(v *yaml.Node) error {
|
||||
var uuidStr string
|
||||
if err := v.Decode(&uuidStr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
uuid, err := uuid.Parse(uuidStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
u.UUID = uuid
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Definition struct {
|
||||
// unique system identifier
|
||||
UUID UUIDv4 `yaml:"uuid"`
|
||||
// directory location where the configuration is stored
|
||||
Path string `yaml:"path,omitempty"`
|
||||
// override log level
|
||||
LogLevel string `yaml:"log-level,omitempty"`
|
||||
// storage providers configuration
|
||||
Storage Storage `yaml:"storage"`
|
||||
// registered items
|
||||
Items []string `yaml:"items"`
|
||||
}
|
||||
|
||||
func (d *Definition) Load(logger *cmLog.Logger) error {
|
||||
env := "$HOME/.config/config-mapper.yml"
|
||||
if v := os.Getenv("CFG_MAPPER_CONFIG_PATH"); v != "" {
|
||||
env = v
|
||||
|
@ -26,19 +53,27 @@ func Load(logger *cmLog.Logger) (*Definition, error) {
|
|||
logger.Debug("Loading config from %s", env)
|
||||
f, err := os.Open(env)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
cfg := Definition{}
|
||||
d, err := io.ReadAll(f)
|
||||
data, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(d, &cfg); err != nil {
|
||||
return nil, err
|
||||
if err := yaml.Unmarshal(data, d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Definition) Write() error {
|
||||
b, err := yaml.Marshal(d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(os.ExpandEnv(ConfigPath), b, 0644)
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
// ClassicLogFormat is the format used to print user logs.
|
||||
//
|
||||
// Format: "LEVEL MESSAGE"
|
||||
const ClassicLogFormat = "%s %s"
|
||||
const ClassicLogFormat = "%s %s\n"
|
||||
|
||||
type Level uint8
|
||||
|
||||
|
@ -90,6 +90,6 @@ func (l Logger) Debug(msg string, args ...interface{}) {
|
|||
|
||||
func (l Logger) log(level Level, msg string, args ...interface{}) {
|
||||
if l.Lvl >= level {
|
||||
fmt.Printf(ClassicLogFormat, l.Lvl, fmt.Sprintf(msg, args...))
|
||||
fmt.Printf(ClassicLogFormat, level, fmt.Sprintf(msg, args...))
|
||||
}
|
||||
}
|
||||
|
|
59
main.go
59
main.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"os"
|
||||
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/actions"
|
||||
cmConfig "gitea.antoine-langlois.net/datahearth/config-mapper/internal/config"
|
||||
cmLog "gitea.antoine-langlois.net/datahearth/config-mapper/internal/logger"
|
||||
"github.com/urfave/cli/v2"
|
||||
|
@ -23,24 +24,43 @@ with a single configuration file.`,
|
|||
Suggest: true,
|
||||
EnableBashCompletion: true,
|
||||
Flags: []cli.Flag{},
|
||||
Before: func(ctx *cli.Context) error {
|
||||
var err error
|
||||
config, err = cmConfig.Load(logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "setup",
|
||||
Description: "Setup the configuration file",
|
||||
Action: func(ctx *cli.Context) error {
|
||||
if err := actions.Setup(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if config.LogLevel != "" {
|
||||
logger.Lvl = cmLog.LevelFromString(config.LogLevel)
|
||||
}
|
||||
logger.Info("Configuration file created at: %s", cmConfig.ConfigPath)
|
||||
|
||||
return nil
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "save",
|
||||
Aliases: []string{"s"},
|
||||
Description: "Save the current configuration",
|
||||
Before: beforeLoadConfig,
|
||||
Action: func(ctx *cli.Context) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "load",
|
||||
Aliases: []string{"l"},
|
||||
Description: "Load the onto your configuration",
|
||||
Before: beforeLoadConfig,
|
||||
Action: func(ctx *cli.Context) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
Commands: []*cli.Command{},
|
||||
}
|
||||
var (
|
||||
logger = cmLog.New(cmLog.Info)
|
||||
config *cmConfig.Definition
|
||||
logger = cmLog.New(cmLog.Info)
|
||||
config *cmConfig.Definition = new(cmConfig.Definition)
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -48,3 +68,16 @@ func main() {
|
|||
logger.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func beforeLoadConfig(ctx *cli.Context) error {
|
||||
// * Skip loading configuration when configuration
|
||||
if err := config.Load(logger); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if config.LogLevel != "" {
|
||||
logger.Lvl = cmLog.LevelFromString(config.LogLevel)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Reference in New Issue