Skip to content

Commit 0eac7af

Browse files
committed
feat: New API for profile initialization
1 parent 0edc081 commit 0eac7af

5 files changed

Lines changed: 79 additions & 38 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}
Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
package archiso
22

3-
import (
4-
"encoding/json"
5-
"fmt"
6-
"os"
7-
"path"
8-
)
9-
103
type Profile struct {
114
Config ProfileDef
125
modules []Module
136
Path string
147
BootloadersPath string
8+
ModulesPath string
159
}
1610

1711
type ProfileDef struct {
@@ -23,30 +17,3 @@ type ProfileDef struct {
2317
Injects map[string][]string `json:"injects"`
2418
COWSpaceSize string `json:"cow_spacesize"`
2519
}
26-
27-
func NewProfile(dir, bootloadersPath, modulesPath string) (*Profile, error) {
28-
configFile, err := os.ReadFile(path.Join(dir, "profiledef.json"))
29-
if err != nil {
30-
return nil, fmt.Errorf("failed to read profile config: %w", err)
31-
}
32-
33-
profile := Profile{}
34-
35-
if err := json.Unmarshal(configFile, &profile.Config); err != nil {
36-
return nil, fmt.Errorf("failed to parse profile config: %w", err)
37-
}
38-
39-
profile.Path = dir
40-
profile.BootloadersPath = bootloadersPath
41-
42-
for _, modName := range profile.Config.Modules {
43-
modDir := path.Join(modulesPath, modName)
44-
mod, err := NewModule(modDir)
45-
if err != nil {
46-
return nil, fmt.Errorf("failed to load module %s: %w", modName, err)
47-
}
48-
profile.modules = append(profile.modules, *mod)
49-
}
50-
51-
return &profile, nil
52-
}

alteriso/src/internal/cmd/profile/build.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ func buildCmd() *cobra.Command {
2424
bootloadersPath := cmd.Parent().PersistentFlags().Lookup("bootloaders").Value.String()
2525
modulesPath := cmd.Parent().PersistentFlags().Lookup("modules").Value.String()
2626

27-
profile, err := archiso.NewProfile(configDir, bootloadersPath, modulesPath)
27+
profile, err := archiso.NewProfile(configDir,
28+
archiso.WithModulesPath(modulesPath),
29+
archiso.WithbootloadersPath(bootloadersPath),
30+
)
2831
if err != nil {
2932
return err
3033
}

alteriso/src/internal/cmd/profile/format.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ func profileFormatCmd() *cobra.Command {
2121
bootloadersPath := cmd.Parent().PersistentFlags().Lookup("bootloaders").Value.String()
2222
modulesPath := cmd.Parent().PersistentFlags().Lookup("modules").Value.String()
2323

24-
profile, err := archiso.NewProfile(configPath, bootloadersPath, modulesPath)
24+
profile, err := archiso.NewProfile(configPath,
25+
archiso.WithModulesPath(modulesPath),
26+
archiso.WithbootloadersPath(bootloadersPath),
27+
)
2528
if err != nil {
2629
return err
2730
}

alteriso/src/internal/cmd/profile/generate.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@ func generateCmd() *cobra.Command {
3939
bootloadersPath := cmd.Parent().PersistentFlags().Lookup("bootloaders").Value.String()
4040
modulesPath := cmd.Parent().PersistentFlags().Lookup("modules").Value.String()
4141

42-
profile, err := archiso.NewProfile(configDir, bootloadersPath, modulesPath)
42+
profile, err := archiso.NewProfile(configDir,
43+
archiso.WithModulesPath(modulesPath),
44+
archiso.WithbootloadersPath(bootloadersPath),
45+
// archiso.WithPacmanCacheDir(pacmanCacheDir),
46+
)
4347
if err != nil {
4448
return err
4549
}
4650

47-
dstDir := path.Join(outDir, configName)
51+
// dstDir := path.Join(outDir, configName)
52+
dstDir := path.Join(outDir)
4853
if futils.Exists(dstDir) {
4954
return errors.New("output directory already exists")
5055
}

0 commit comments

Comments
 (0)