github.com/codefly-dev/core@v0.1.107/resources/layout.go (about)

     1  package resources
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path"
     7  
     8  	"github.com/codefly-dev/core/wool"
     9  )
    10  
    11  type LayoutKind = string
    12  
    13  const (
    14  	LayoutKindFlat    LayoutKind = "flat"
    15  	LayoutKindModules LayoutKind = "modules"
    16  )
    17  
    18  type Layout interface {
    19  
    20  	// Modules inside a Workspace
    21  
    22  	ModulesRoot() string
    23  	ModulePath(name string) string
    24  
    25  	// Services inside a Module
    26  
    27  	ServicesRoot(module string) string
    28  	ServicePath(module string, name string) string
    29  }
    30  
    31  func NewLayout(ctx context.Context, root string, kind string, conf []byte) (Layout, error) {
    32  	switch kind {
    33  	case LayoutKindFlat:
    34  		return NewFlatLayout(ctx, root, conf)
    35  	case LayoutKindModules:
    36  		return NewModulesLayout(ctx, root, conf)
    37  	default:
    38  		return nil, fmt.Errorf("unknown layout kind %s", kind)
    39  	}
    40  }
    41  
    42  // FlatLayout is a layout where the module "root" is the top folders
    43  // Services are in the services folder at the root
    44  
    45  type FlatLayout struct {
    46  	root string
    47  }
    48  
    49  func (f FlatLayout) ModulesRoot() string {
    50  	return f.root
    51  }
    52  
    53  func (f FlatLayout) ModulePath(string) string {
    54  	return f.root
    55  }
    56  
    57  func (f FlatLayout) ServicesRoot(string) string {
    58  	return path.Join(f.root, "services")
    59  }
    60  
    61  func (f FlatLayout) ServicePath(_ string, name string) string {
    62  	return path.Join(f.root, "services", name)
    63  }
    64  
    65  func NewFlatLayout(ctx context.Context, root string, conf []byte) (*FlatLayout, error) {
    66  	w := wool.Get(ctx).In("resources.NewFlatLayout")
    67  	w.Debug("create", wool.Field("root", root))
    68  	layout := &FlatLayout{root: root}
    69  	err := LoadSpec(ctx, conf, layout)
    70  	if err != nil {
    71  		return nil, w.Wrapf(err, "failed to load spec")
    72  	}
    73  	return layout, nil
    74  }
    75  
    76  // ModulesLayout is a layout with modules
    77  
    78  type ModulesLayout struct {
    79  	root string
    80  }
    81  
    82  func (f ModulesLayout) ModulesRoot() string {
    83  	return f.root
    84  }
    85  
    86  func (f ModulesLayout) ModulePath(module string) string {
    87  	return path.Join(f.root, "modules", module)
    88  }
    89  
    90  func (f ModulesLayout) ServicesRoot(module string) string {
    91  	return path.Join(f.root, "modules", module, "services")
    92  }
    93  
    94  func (f ModulesLayout) ServicePath(module string, name string) string {
    95  	return path.Join(f.root, "modules", module, "services", name)
    96  }
    97  
    98  func NewModulesLayout(ctx context.Context, root string, conf []byte) (*ModulesLayout, error) {
    99  	w := wool.Get(ctx).In("resources.NewModulesLayout")
   100  	w.Debug("create", wool.Field("root", root))
   101  	layout := &ModulesLayout{root: root}
   102  	err := LoadSpec(ctx, conf, layout)
   103  	if err != nil {
   104  		return nil, w.Wrapf(err, "failed to load spec")
   105  	}
   106  	return layout, nil
   107  }