Skip to content

Commit 96eae3b

Browse files
committed
Add: check command
1 parent 697eb35 commit 96eae3b

8 files changed

Lines changed: 56 additions & 13 deletions

File tree

alteriso5/cmd/build/build.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
package build
22

33
import (
4-
"errors"
54
"os"
65
"path"
76

87
"github.com/FascodeNet/alterlinux/alteriso5/cmd/build/config"
98
"github.com/FascodeNet/alterlinux/alteriso5/cmd/build/work"
109
)
1110

12-
func check() error {
13-
if os.Getuid() != 0 {
14-
return errors.New("this program must be run as root")
15-
}
16-
return nil
17-
}
18-
1911
func build() error {
2012
current, err := os.Getwd()
2113
if err != nil {
@@ -32,7 +24,7 @@ func build() error {
3224

3325
// Dummy profile
3426
profile := config.Profile{
35-
Base: path.Join(current, "profile"),
27+
Base: path.Join(current, "profile"),
3628
InstallDir: "alter",
3729
}
3830

alteriso5/cmd/build/cmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/FascodeNet/alterlinux/alteriso5/cmd/check"
78
"github.com/FascodeNet/alterlinux/alteriso5/utils"
89
"github.com/spf13/cobra"
910
)
@@ -19,13 +20,12 @@ func Cmd() *cobra.Command {
1920
os.Exit(1)
2021
}, os.Interrupt)
2122

22-
if err := check(); err != nil {
23-
cmd.PrintErrln(err)
24-
os.Exit(1)
23+
if err := utils.CallCmd(cmd, *check.Cmd()); err != nil {
24+
return err
2525
}
2626

2727
if err := build(); err != nil {
28-
cmd.PrintErrln(err)
28+
return err
2929
}
3030

3131
return nil

alteriso5/cmd/check.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cmd
2+
3+
import (
4+
"github.com/FascodeNet/alterlinux/alteriso5/cmd/check"
5+
"github.com/Hayao0819/nahi/cobrautils"
6+
)
7+
8+
func init() {
9+
cobrautils.AddSubCmds(check.Cmd())
10+
}

alteriso5/cmd/check/cmd.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package check
2+
3+
import (
4+
"errors"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func check() error {
11+
if os.Getuid() != 0 {
12+
return errors.New("this program must be run as root")
13+
}
14+
return nil
15+
}
16+
17+
func Cmd() *cobra.Command {
18+
cmd := cobra.Command{
19+
Use: "check",
20+
Short: "Check if the system is ready to build the ISO",
21+
SilenceUsage: true,
22+
SilenceErrors: true,
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
return check()
25+
},
26+
}
27+
28+
return &cmd
29+
}

alteriso5/cmd/check/permission.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package check

alteriso5/utils/cobra.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package utils
2+
3+
import "github.com/spf13/cobra"
4+
5+
func CallCmd(me *cobra.Command, target cobra.Command, args ...string) error {
6+
target.SetOut(me.OutOrStdout())
7+
target.SetErr(me.OutOrStderr())
8+
target.SetIn(me.InOrStdin())
9+
target.SetArgs(args)
10+
return target.Execute()
11+
}

0 commit comments

Comments
 (0)