|
| 1 | +package archiso |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | +) |
| 9 | + |
| 10 | +type profileOption func(*Profile) error |
| 11 | + |
| 12 | +func WithPacmanCacheDir(dir string) profileOption { |
| 13 | + return func(p *Profile) error { |
| 14 | + p.PacmanCacheDir = dir |
| 15 | + return nil |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func WithModulesPath(dir string) profileOption { |
| 20 | + return func(p *Profile) error { |
| 21 | + p.ModulesPath = dir |
| 22 | + return nil |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func WithbootloadersPath(dir string) profileOption { |
| 27 | + return func(p *Profile) error { |
| 28 | + p.BootloadersPath = dir |
| 29 | + return nil |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func NewProfile(dir string, opts ...profileOption) (*Profile, error) { |
| 34 | + configFile, err := os.ReadFile(path.Join(dir, "profiledef.json")) |
| 35 | + if err != nil { |
| 36 | + return nil, fmt.Errorf("failed to read profile config: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + profile := Profile{ |
| 40 | + Path: dir, |
| 41 | + } |
| 42 | + if err := json.Unmarshal(configFile, &profile.Config); err != nil { |
| 43 | + return nil, fmt.Errorf("failed to parse profile config: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + for _, opt := range opts { |
| 47 | + if err := opt(&profile); err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Load modules |
| 53 | + for _, modName := range profile.Config.Modules { |
| 54 | + modDir := path.Join(profile.ModulesPath, modName) |
| 55 | + mod, err := NewModule(modDir) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("failed to load module %s: %w", modName, err) |
| 58 | + } |
| 59 | + profile.modules = append(profile.modules, *mod) |
| 60 | + } |
| 61 | + |
| 62 | + return &profile, nil |
| 63 | +} |
0 commit comments