11package profile
22
33import (
4- "fmt"
54 "log/slog"
65 "os"
76 "path"
7+ "path/filepath"
88
99 "github.com/FascodeNet/alterlinux/src/internal/archiso"
1010 "github.com/FascodeNet/alterlinux/src/internal/errors"
1111 "github.com/Hayao0819/nahi/exutils"
1212 "github.com/spf13/cobra"
1313)
1414
15+ func getProfileFromArg (cmd * cobra.Command , configDir string ) (* archiso.Profile , error ) {
16+ bootloadersPath := cmd .Parent ().PersistentFlags ().Lookup ("bootloaders" ).Value .String ()
17+ modulesPath := cmd .Parent ().PersistentFlags ().Lookup ("modules" ).Value .String ()
18+
19+ profile , err := archiso .NewProfile (configDir ,
20+ archiso .WithModulesPath (modulesPath ),
21+ archiso .WithbootloadersPath (bootloadersPath ),
22+ )
23+ if err != nil {
24+ return nil , errors .Wrap (err )
25+ }
26+
27+ return profile , nil
28+ }
29+
1530func buildCmd () * cobra.Command {
1631 outDir := "./out"
32+ workDir := "./work"
1733 cmd := cobra.Command {
1834 Use : "build" ,
1935 Short : "Build the ISO from generated profile" ,
20- RunE : func (cmd * cobra.Command , args []string ) error {
21- configDir := args [0 ]
22- configName := path .Base (configDir )
23-
24- bootloadersPath := cmd .Parent ().PersistentFlags ().Lookup ("bootloaders" ).Value .String ()
25- modulesPath := cmd .Parent ().PersistentFlags ().Lookup ("modules" ).Value .String ()
36+ PreRunE : func (cmd * cobra.Command , args []string ) error {
37+ // TODO: 権限チェック
2638
27- profile , err := archiso .NewProfile (configDir ,
28- archiso .WithModulesPath (modulesPath ),
29- archiso .WithbootloadersPath (bootloadersPath ),
30- )
31- if err != nil {
32- return err
39+ return nil
40+ },
41+ RunE : func (cmd * cobra.Command , args []string ) error {
42+ // Load profile
43+ var configDir string
44+ if len (args ) < 1 {
45+ configDir = "./configs/xfce"
46+ } else {
47+ configDir = args [0 ]
3348 }
3449
35- profileDir , err := os .MkdirTemp ("" , fmt .Sprintf ("alteriso-%s-*" , configName ))
50+ configName := path .Base (configDir )
51+ slog .Info ("Loading profile..." , "config" , configDir )
52+ profile , err := getProfileFromArg (cmd , configDir )
3653 if err != nil {
3754 return errors .Wrap (err )
3855 }
39- defer os .RemoveAll (profileDir )
56+ slog .Info ("Using profile" , "name" , configName )
57+
58+ // Setup directories
59+ for _ , dir := range []* string {& outDir , & workDir } {
60+ absDir , err := filepath .Abs (* dir )
61+ if err != nil {
62+ return errors .Wrap (err )
63+ }
64+ * dir = absDir
65+ if err := os .MkdirAll (* dir , 0o755 ); err != nil {
66+ return errors .Wrap (err )
67+ }
68+ }
69+ slog .Info ("Output directory" , "dir" , outDir )
70+ slog .Info ("Working directory" , "dir" , workDir )
4071
41- if err := profile .GenArchisoProfile (profileDir ); err != nil {
72+ // Generate archiso profile
73+ archisoprofileDir := path .Join (workDir , "profile" )
74+ if err := profile .GenArchisoProfile (archisoprofileDir ); err != nil {
4275 return errors .Wrap (err )
4376 }
77+ slog .Info ("Generated archiso profile" , "dir" , archisoprofileDir )
4478
45- slog .Info ("Generated archiso profile" , "dir" , profileDir )
46-
47- mkarchiso , err := archiso .MkarchisoPath ()
79+ // Build ISO with mkarchiso
80+ // TODO: 権限昇格
81+ archisoWorkDir := path .Join (workDir , "archiso" )
82+ if err := os .MkdirAll (archisoWorkDir , 0o755 ); err != nil {
83+ return errors .Wrap (err )
84+ }
85+ archisoPacmanCacheDir := path .Join (workDir , "pacman_cache" )
86+ if err := os .MkdirAll (archisoPacmanCacheDir , 0o755 ); err != nil {
87+ return errors .Wrap (err )
88+ }
89+ mkarchisoPath , err := archiso .MkarchisoPath ()
4890 if err != nil {
4991 return errors .Wrap (err )
5092 }
93+ mkarchisoCmd := exutils .CommandWithStdio (mkarchisoPath , "-v" , "-w" , archisoWorkDir , "-o" , outDir , archisoprofileDir )
94+ mkarchisoCmd .Env = append (mkarchisoCmd .Env , "ALTERISO_PACMAN_CACHE=" + archisoPacmanCacheDir )
95+ slog .Info ("Building ISO image..." , "command" , mkarchisoCmd .String ())
5196
52- if err := exutils . CommandWithStdio ( mkarchiso , "-v" , "-w" , "work" , "-o" , outDir , profileDir ) .Run (); err != nil {
97+ if err := mkarchisoCmd .Run (); err != nil {
5398 return errors .Wrap (err )
5499 }
55100
@@ -59,6 +104,7 @@ func buildCmd() *cobra.Command {
59104 }
60105
61106 cmd .Flags ().StringVarP (& outDir , "out" , "o" , outDir , "Output directory" )
107+ cmd .Flags ().StringVar (& workDir , "work" , workDir , "Working directory" )
62108
63109 return & cmd
64110
0 commit comments