Skip to content

Commit

Permalink
refactor: remove dependency from Run/Process
Browse files Browse the repository at this point in the history
  • Loading branch information
khatibomar committed May 3, 2024
1 parent 7ee5c2b commit 69167b5
Showing 1 changed file with 68 additions and 42 deletions.
110 changes: 68 additions & 42 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,61 +35,37 @@ func main() {
flag.Usage = Usage
flag.Parse()

cfgDir := filepath.Dir(*configPath)

if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

var config Config
_, err := toml.DecodeFile(*configPath, &config)
var cfg Config
_, err := toml.DecodeFile(*configPath, &cfg)
if err != nil {
log.Fatal().
Err(err).
Send()
}

result, err := Run(config)
if err != nil {
log.Fatal().
Err(err).
Send()
}
groupedMappings := groupMappings(cfg.Mappings)

for _, r := range result {
log.Print(string(r.Buf))
}
}

func Usage() {
log.Info().
Msg("\nUsage of km:\nFlags:")
flag.PrintDefaults()
}
var w []work

func Run(cfg Config) ([]File, error) {
cfgDir := filepath.Dir(*configPath)
groupedMappings := make(map[string][]Mapping)
for _, m := range cfg.Mappings {
destinationPath := filepath.Dir(m.Destination.Path)
groupedMappings[destinationPath] = append(groupedMappings[destinationPath], m)
}

var result []File

for key, mapping := range groupedMappings {
g := Generator{}

for i, m := range mapping {
for _, mapping := range groupedMappings {
for _, m := range mapping {
sourceNode, err := loadAstFromFile(path.Join(cfgDir, m.Source.Path))
if err != nil {
return result, err
log.Fatal().
Err(err).
Send()
}
destinationNode, err := loadAstFromFile(path.Join(cfgDir, m.Destination.Path))
if err != nil {
return result, err
}

if i == 0 {
g.Printf("package %s\n", getPackage(destinationNode))
log.Fatal().
Err(err).
Send()
}

ignoredMap := make(map[string]struct{})
Expand All @@ -111,20 +87,65 @@ func Run(cfg Config) ([]File, error) {
fieldsMap: m.Destination.FieldsMap,
}

if err := g.generate(source, destination); err != nil {
return result, err
}
w = append(w, work{
Source: source,
Destination: destination,
})
}
}

result, err := Process(w)
if err != nil {
log.Fatal().
Err(err).
Send()
}

for _, r := range result {
log.Print(string(r.Buf))
}
}

func Usage() {
log.Info().
Msg("\nUsage of km:\nFlags:")
flag.PrintDefaults()
}

func Process(work []work) ([]File, error) {
var result []File

for i, w := range work {

g := Generator{}

if i == 0 {
g.Printf("package %s\n", getPackage(w.Destination.node))
}

if err := g.generate(w.Source, w.Destination); err != nil {
return result, err
}

result = append(result, File{
Path: key,
Path: w.Destination.path,
Buf: g.format(),
})
}

return result, nil
}

func groupMappings(mappings []Mapping) map[string][]Mapping {
groupedMappings := make(map[string][]Mapping)
for _, m := range mappings {
destinationPath := filepath.Dir(m.Destination.Path)
groupedMappings[destinationPath] = append(groupedMappings[destinationPath], m)
}

return groupedMappings
}

type Generator struct {
buf bytes.Buffer
}
Expand Down Expand Up @@ -262,3 +283,8 @@ type DestinationData struct {
ignoredMap map[string]struct{}
fieldsMap map[string]string
}

type work struct {
Source SourceData
Destination DestinationData
}

0 comments on commit 69167b5

Please sign in to comment.