Skip to content

Commit c3981b1

Browse files
committed
feat: Add injectable test with dummy profile
1 parent 18bbaa7 commit c3981b1

9 files changed

Lines changed: 170 additions & 14 deletions

File tree

alteriso/src/internal/cmd/cmds.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cmd
2+
3+
import (
4+
"github.com/FascodeNet/alterlinux/src/internal/cmd/injectable"
5+
"github.com/FascodeNet/alterlinux/src/internal/cmd/profile"
6+
)
7+
8+
func init() {
9+
rootReg.Add(
10+
profile.Cmd(),
11+
injectable.Cmd(),
12+
)
13+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package injectable
2+
3+
import (
4+
"embed"
5+
"fmt"
6+
"io/fs"
7+
"os"
8+
"os/exec"
9+
"path"
10+
"path/filepath"
11+
12+
"github.com/FascodeNet/alterlinux/src/internal/archiso"
13+
"github.com/FascodeNet/alterlinux/src/internal/errors"
14+
"github.com/FascodeNet/alterlinux/src/internal/logger"
15+
"github.com/spf13/cobra"
16+
)
17+
18+
//go:embed profile
19+
var emptyProfile embed.FS
20+
21+
func extractEmptyProfile(dest string) error {
22+
return fs.WalkDir(emptyProfile, "profile", func(p string, d fs.DirEntry, err error) error {
23+
if err != nil {
24+
return err
25+
}
26+
27+
relPath, err := filepath.Rel("profile", p)
28+
if err != nil {
29+
return err
30+
}
31+
32+
destPath := filepath.Join(dest, relPath)
33+
34+
if d.IsDir() {
35+
return os.MkdirAll(path.Join(dest, relPath), 0755)
36+
}
37+
38+
data, err := emptyProfile.ReadFile(p)
39+
if err != nil {
40+
return err
41+
}
42+
43+
return os.WriteFile(destPath, data, 0644)
44+
})
45+
46+
}
47+
48+
func TestInjectable() (bool, error) {
49+
tmpdir, err := os.MkdirTemp("", "alteriso-injectable-test-")
50+
if err != nil {
51+
return false, err
52+
}
53+
defer os.RemoveAll(tmpdir)
54+
55+
alterisoProfileDir := path.Join(tmpdir, "alteriso")
56+
archisoProfileDir := path.Join(tmpdir, "archiso")
57+
58+
if err := extractEmptyProfile(alterisoProfileDir); err != nil {
59+
return false, errors.Wrap(err)
60+
}
61+
62+
// slog.Info("Loading alteriso profile", "dir", archisoProfileDir)
63+
profile, err := archiso.NewProfile(alterisoProfileDir, archiso.WithModulesPath(""), archiso.WithbootloadersPath(alterisoProfileDir))
64+
if err != nil {
65+
return false, errors.Wrap(err)
66+
}
67+
68+
// slog.Info("Generating archiso profile", "dir", archisoProfileDir)
69+
70+
err = logger.WithoutLog(func() error {
71+
return profile.GenArchisoProfile(archisoProfileDir)
72+
})
73+
if err != nil {
74+
return false, errors.Wrap(err)
75+
}
76+
77+
// slog.Info("Testing injectable", "dir", archisoProfileDir)
78+
testCmd := exec.Command("fakeroot", "mkarchiso", "-v", archisoProfileDir)
79+
if err := testCmd.Run(); err != nil {
80+
return false, nil
81+
}
82+
83+
return true, nil
84+
}
85+
86+
func Cmd() *cobra.Command {
87+
cmd := &cobra.Command{
88+
Use: "test-injectable",
89+
Short: "Test if archiso is injectable",
90+
RunE: func(cmd *cobra.Command, args []string) error {
91+
injectable, err := TestInjectable()
92+
if err != nil {
93+
return err
94+
}
95+
if !injectable {
96+
fmt.Println("archiso is not injectable")
97+
return nil
98+
}
99+
100+
fmt.Println("archiso is injectable")
101+
102+
return nil
103+
},
104+
}
105+
return cmd
106+
}

alteriso/src/internal/cmd/injectable/profile/efiboot/.gitkeep

Whitespace-only changes.

alteriso/src/internal/cmd/injectable/profile/grub/.gitkeep

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"arch": "x86_64",
3+
"pacman_conf": "/dev/null",
4+
"require_injectable": true
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
# shellcheck disable=SC2034
3+
4+
arch="x86_64"
5+
pacman_conf="/dev/null"
6+
7+
__alteriso_injectable="n"
8+
__alteriso_run_once "alteriso_inject_test"
9+
if [[ "$__alteriso_injectable" != "y" ]]; then
10+
echo "mkarchiso is not injectable." >&2
11+
exit 1
12+
else
13+
echo "mkarchiso is injectable." >&2
14+
exit 0
15+
fi

alteriso/src/internal/cmd/injectable/profile/syslinux/.gitkeep

Whitespace-only changes.

alteriso/src/internal/cmd/install.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,35 @@ import (
77
"net/http"
88
"os"
99
"os/exec"
10+
"os/user"
1011

1112
"github.com/Hayao0819/go-distro"
1213
"github.com/Hayao0819/go-distro/linux"
1314

14-
"github.com/FascodeNet/alterlinux/src/internal/archiso"
15+
"github.com/FascodeNet/alterlinux/src/internal/cmd/injectable"
1516
"github.com/FascodeNet/alterlinux/src/internal/errors"
1617
"github.com/Jguer/go-alpm/v2"
1718
"github.com/go-git/go-git/v6"
1819
"github.com/go-git/go-git/v6/plumbing"
1920
"github.com/spf13/cobra"
2021
)
2122

23+
/*
24+
このコードは概ねこれです。
25+
26+
#!/usr/bin/env bash
27+
set -eEuo pipefail
28+
if which pacman 2> /dev/null 1>&2 && pacman -Qq archiso; then
29+
curl -sL "https://raw.githubusercontent.com/FascodeNet/alterlinux/refs/heads/dev/archiso/mkarchiso" > "/usr/local/bin/mkarchiso"
30+
else
31+
tmpdir=$(mktemp -d)
32+
trap 'rm -rf "$tmpdir"' 1 2 3 15
33+
git clone https://github.com/FascodeNet/alterlinux "$tmpdir"
34+
cd "$tmpdir" || exit 1
35+
make install
36+
fi
37+
*/
38+
2239
func isPkgInstalled(name ...string) (bool, error) {
2340

2441
if !isArchLinux() {
@@ -72,7 +89,7 @@ func checkoutBranch(repo *git.Repository, branch string) error {
7289
}
7390

7491
err = w.Checkout(&git.CheckoutOptions{
75-
Branch: plumbing.ReferenceName(branch),
92+
Branch: plumbing.NewBranchReferenceName(branch),
7693
})
7794
if err != nil {
7895
return err
@@ -108,9 +125,9 @@ func installArchisoCmd() *cobra.Command {
108125
Use: "install-archiso",
109126
Short: "Install injectable Archiso",
110127
PreRunE: func(cmd *cobra.Command, args []string) error {
111-
installed, err := archiso.IsInjectable()
128+
installed, err := injectable.TestInjectable()
112129
if err != nil {
113-
return err
130+
installed = false
114131
}
115132
if installed {
116133
return errors.New("archiso-injectable is already installed")
@@ -157,19 +174,26 @@ func installArchisoCmd() *cobra.Command {
157174
}
158175
return nil
159176
} else {
177+
if u, err := user.Current(); err == nil && u.Uid != "0" {
178+
return errors.New("archiso package is not installed, please run this command as root to install from source")
179+
}
180+
160181
slog.Info("archiso package is not installed, cloning full repository and installing from source")
161182
repoURL := fmt.Sprintf("https://github.com/%s/%s.git", githubOwner, githubRepo)
183+
slog.Info("Cloning repository", "url", repoURL, "ref", githubRef)
162184
repo, tempDir, err := gitCloneIntoTempDir(repoURL)
163185
if err != nil {
164186
return err
165187
}
166188
defer os.RemoveAll(tempDir)
167189

190+
slog.Info("Checking out branch", "branch", githubRef)
168191
if err := checkoutBranch(repo, githubRef); err != nil {
169192
return err
170193
}
171194

172-
makeCmd := exec.Command("make", "install")
195+
slog.Info("Running make install", "dir", tempDir)
196+
makeCmd := exec.Command("make", "install-scripts")
173197
makeCmd.Dir = tempDir
174198
makeCmd.Stdout = os.Stdout
175199
makeCmd.Stderr = os.Stderr
@@ -184,8 +208,8 @@ func installArchisoCmd() *cobra.Command {
184208

185209
cmd.Flags().StringP("script-dest", "", "/usr/local/bin/mkarchiso", "Local destination path for the mkarchiso script")
186210
cmd.Flags().StringP("github-owner", "", "FascodeNet", "GitHub owner of the archiso-injectable repository")
187-
cmd.Flags().StringP("github-repo", "", "archiso-injectable", "GitHub repository name of the archiso-injectable repository")
188-
cmd.Flags().StringP("github-ref", "", "main", "GitHub reference (branch, tag, commit) of the archiso-injectable repository")
211+
cmd.Flags().StringP("github-repo", "", "alterlinux", "GitHub repository name of the archiso-injectable repository")
212+
cmd.Flags().StringP("github-ref", "", "dev", "GitHub reference (branch, tag, commit) of the archiso-injectable repository")
189213

190214
return &cmd
191215

alteriso/src/internal/cmd/profile.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)