Skip to content

Commit 7eb7546

Browse files
committed
feat: Add repo command
1 parent 6828629 commit 7eb7546

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

alteriso/src/internal/cmd/repo.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cmd
2+
3+
import (
4+
"io"
5+
"net/http"
6+
"os"
7+
8+
"github.com/FascodeNet/alterlinux/src/internal/errors"
9+
"github.com/Hayao0819/nahi/exutils"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
const setupScriptUrl = "https://raw.githubusercontent.com/FascodeNet/alterlinux/refs/heads/dev/alteriso/setup_3rd_repo.sh"
14+
15+
// TODO: 権限昇格
16+
func repoCmd() *cobra.Command {
17+
cmd := cobra.Command{
18+
Use: "repo",
19+
Short: "Install 3rd party repositories",
20+
PreRunE: func(cmd *cobra.Command, args []string) error {
21+
// root check
22+
if os.Geteuid() != 0 {
23+
return errors.Newf("this command requires root privileges")
24+
}
25+
return nil
26+
},
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
tempFile, err := os.CreateTemp("", "setup_3rd_repo_*.sh")
29+
if err != nil {
30+
return err
31+
}
32+
defer os.Remove(tempFile.Name())
33+
34+
resp, err := http.Get(setupScriptUrl)
35+
if err != nil {
36+
return errors.Wrap(err)
37+
}
38+
defer resp.Body.Close()
39+
40+
_, err = io.Copy(tempFile, resp.Body)
41+
if err != nil {
42+
return errors.Wrap(err)
43+
}
44+
45+
if err := tempFile.Close(); err != nil {
46+
return errors.Wrap(err)
47+
}
48+
49+
if err := os.Chmod(tempFile.Name(), 0755); err != nil {
50+
return errors.Wrap(err)
51+
}
52+
53+
scriptCmd := exutils.CommandWithStdio("bash", tempFile.Name(), "-r", "all")
54+
if err := scriptCmd.Run(); err != nil {
55+
return errors.Wrap(err)
56+
}
57+
58+
return nil
59+
60+
},
61+
}
62+
return &cmd
63+
}
64+
65+
func init() {
66+
rootReg.Add(repoCmd())
67+
}

0 commit comments

Comments
 (0)