Skip to content

Commit 59ccf54

Browse files
committed
cleanup un-used code
1 parent e61fe4c commit 59ccf54

File tree

14 files changed

+20
-489
lines changed

14 files changed

+20
-489
lines changed

cmd/debug/root.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ func Commands() *cobra.Command {
1313
}
1414

1515
cmd.AddCommand(
16-
GetUserCommand(),
1716
GetIntrinsicCommand(),
1817
)
1918

cmd/debug/user.go

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

cmd/root.go

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@ package cmd
22

33
//goland:noinspection GoSnakeCaseUsage
44
import (
5-
"fmt"
65
"github.com/bcdevtools/devd/cmd/convert"
76
"github.com/bcdevtools/devd/cmd/debug"
87
"github.com/bcdevtools/devd/cmd/hash"
98
"github.com/bcdevtools/devd/cmd/query"
109
"github.com/bcdevtools/devd/cmd/types"
11-
"github.com/bcdevtools/devd/cmd/utils"
1210
"github.com/bcdevtools/devd/constants"
1311
"github.com/spf13/cobra"
1412
"os"
15-
"strings"
16-
"time"
1713
)
1814

1915
// rootCmd represents the base command when called without any subcommands
@@ -28,26 +24,9 @@ func Execute() {
2824
rootCmd.CompletionOptions.HiddenDefaultCmd = true // hide the 'completion' subcommand
2925
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true}) // hide the 'help' subcommand
3026

31-
operationUserInfo, err := utils.GetOperationUserInfo()
32-
utils.ExitOnErr(err, "failed to get operation user info")
27+
ctx := types.NewContext()
3328

34-
if operationUserInfo.OperatingAsSuperUser {
35-
utils.PrintlnStdErr("WARN: Running as super user")
36-
}
37-
38-
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
39-
switch cmd.Name() {
40-
case "version":
41-
return
42-
}
43-
44-
changeWorkingUser(cmd)
45-
ensureRequireWorkingUsername(cmd)
46-
}
47-
48-
ctx := types.NewContext(operationUserInfo)
49-
50-
err = rootCmd.ExecuteContext(ctx)
29+
err := rootCmd.ExecuteContext(ctx)
5130
if err != nil {
5231
os.Exit(1)
5332
}
@@ -59,59 +38,5 @@ func init() {
5938
rootCmd.AddCommand(query.Commands())
6039
rootCmd.AddCommand(hash.Commands())
6140

62-
rootCmd.PersistentFlags().String(constants.FLAG_USE_WORKING_USERNAME, "", "Use the specified username as working context username, must be either effective user or real user, if not specified, will use default selected working user")
63-
rootCmd.PersistentFlags().String(constants.FLAG_REQUIRE_WORKING_USERNAME, "", "Ensure working user is the specified user: if working user selected by application has username different with the specified username, application will exit with error")
6441
rootCmd.PersistentFlags().Bool("help", false, "show help")
6542
}
66-
67-
func changeWorkingUser(cmd *cobra.Command) {
68-
selectedWorkingUsername, err := cmd.Flags().GetString(constants.FLAG_USE_WORKING_USERNAME)
69-
utils.ExitOnErr(err, "failed to read flag")
70-
71-
selectedWorkingUsername = strings.TrimSpace(selectedWorkingUsername)
72-
73-
if len(selectedWorkingUsername) < 1 {
74-
return
75-
}
76-
77-
ctx := types.UnwrapAppContext(cmd.Context())
78-
if ctx.GetWorkingUserInfo().Username == selectedWorkingUsername {
79-
return
80-
}
81-
82-
var newWorkingUser *types.UserInfo
83-
84-
operationUserInfo := ctx.GetOperationUserInfo()
85-
86-
if operationUserInfo.EffectiveUserInfo.Username == selectedWorkingUsername {
87-
newWorkingUser = operationUserInfo.EffectiveUserInfo
88-
} else if operationUserInfo.RealUserInfo.Username == selectedWorkingUsername {
89-
newWorkingUser = operationUserInfo.RealUserInfo
90-
} else {
91-
utils.PrintfStdErr("ERR: selected working user %s is not either effective user %s or real user %s\n", selectedWorkingUsername, operationUserInfo.EffectiveUserInfo.Username, operationUserInfo.RealUserInfo.Username)
92-
os.Exit(1)
93-
}
94-
95-
fmt.Println("WARN: changing working user to", newWorkingUser.Username, "instead of default", ctx.GetWorkingUserInfo().Username)
96-
time.Sleep(2 * time.Second)
97-
98-
ctx = ctx.WithWorkingUserInfo(newWorkingUser)
99-
cmd.SetContext(ctx)
100-
}
101-
102-
func ensureRequireWorkingUsername(cmd *cobra.Command) {
103-
requireWorkingUsername, err := cmd.Flags().GetString(constants.FLAG_REQUIRE_WORKING_USERNAME)
104-
utils.ExitOnErr(err, "failed to read flag")
105-
106-
requireWorkingUsername = strings.TrimSpace(requireWorkingUsername)
107-
108-
if len(requireWorkingUsername) < 1 {
109-
return
110-
}
111-
112-
workingUser := types.UnwrapAppContext(cmd.Context()).GetWorkingUserInfo()
113-
if workingUser.Username != requireWorkingUsername {
114-
utils.PrintfStdErr("ERR: working user is %s, but required user is %s\n", workingUser.Username, requireWorkingUsername)
115-
os.Exit(1)
116-
}
117-
}

cmd/types/context.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77
)
88

99
type Context struct {
10-
baseCtx context.Context
11-
operationUserInfo *OperationUserInfo
12-
workingUserInfo *UserInfo
10+
baseCtx context.Context
1311
}
1412

1513
func (c Context) Deadline() (deadline time.Time, ok bool) {
@@ -32,36 +30,17 @@ func (c Context) Value(key any) any {
3230
return c.baseCtx.Value(key)
3331
}
3432

35-
func NewContext(operationUserInfo *OperationUserInfo) Context {
36-
var workingUser *UserInfo
37-
if operationUserInfo != nil {
38-
workingUser = operationUserInfo.GetDefaultWorkingUser()
39-
}
33+
func NewContext() Context {
4034
return Context{
41-
baseCtx: context.Background(),
42-
operationUserInfo: operationUserInfo,
43-
workingUserInfo: workingUser,
35+
baseCtx: context.Background(),
4436
}
4537
}
4638

47-
func (c Context) GetOperationUserInfo() *OperationUserInfo {
48-
return c.operationUserInfo
49-
}
50-
51-
func (c Context) GetWorkingUserInfo() *UserInfo {
52-
return c.workingUserInfo
53-
}
54-
5539
func (c Context) WithContext(ctx context.Context) Context {
5640
c.baseCtx = ctx
5741
return c
5842
}
5943

60-
func (c Context) WithWorkingUserInfo(workingUserInfo *UserInfo) Context {
61-
c.workingUserInfo = workingUserInfo
62-
return c
63-
}
64-
6544
var _ context.Context = Context{}
6645

6746
// ContextKey defines a type alias for a stdlib Context key.

cmd/types/context_test.go

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,25 @@
11
package types
22

33
import (
4+
"github.com/stretchr/testify/require"
45
"testing"
56
)
67

78
func TestNewContext(t *testing.T) {
8-
appCtx := NewContext(nil)
9-
if appCtx.operationUserInfo != nil {
10-
t.Errorf("NewContext() = want operationUserInfo == nil")
11-
return
12-
}
13-
if appCtx.workingUserInfo != nil {
14-
t.Errorf("NewContext() = want workingUserInfo == nil")
15-
return
16-
}
17-
18-
appCtx = NewContext(&OperationUserInfo{
19-
EffectiveUserInfo: &UserInfo{},
20-
RealUserInfo: &UserInfo{},
21-
})
22-
if appCtx.operationUserInfo == nil {
23-
t.Errorf("NewContext() = want operationUserInfo != nil")
24-
return
25-
}
26-
if appCtx.workingUserInfo == nil {
27-
t.Errorf("NewContext() = want workingUserInfo != nil")
28-
return
29-
}
9+
_ = NewContext()
3010
}
3111

3212
func TestWrapAppContext(t *testing.T) {
33-
appCtx := NewContext(nil)
13+
appCtx := NewContext()
3414
ctx := WrapAppContext(appCtx)
35-
if ctx == nil {
36-
t.Errorf("WrapAppContext() = nil, want not nil")
37-
return
38-
}
39-
if ctx != appCtx {
40-
t.Errorf("WrapAppContext() = want the same context as original")
41-
return
42-
}
15+
require.NotNil(t, ctx)
16+
require.Equal(t, appCtx, ctx, "want the same context as original")
4317
}
4418

4519
func TestUnwrapAppContext(t *testing.T) {
46-
appCtx := NewContext(nil)
47-
if appCtx.operationUserInfo != nil {
48-
panic("appCtx.operationUserInfo != nil")
49-
}
20+
appCtx := NewContext()
5021
ctx := WrapAppContext(appCtx)
5122
appCtx2 := UnwrapAppContext(ctx)
52-
if appCtx != appCtx2 {
53-
t.Errorf("UnwrapAppContext() = want the same app context as original")
54-
return
55-
}
56-
if appCtx.baseCtx != appCtx2.baseCtx {
57-
t.Errorf("UnwrapAppContext() = want the same base context as original")
58-
return
59-
}
60-
if appCtx.operationUserInfo != appCtx2.operationUserInfo {
61-
t.Errorf("UnwrapAppContext() = want the same operationUserInfo as original")
62-
return
63-
}
64-
if appCtx.operationUserInfo != appCtx2.operationUserInfo {
65-
t.Errorf("UnwrapAppContext() = want the same operationUserInfo as original")
66-
return
67-
}
68-
69-
//
70-
71-
appCtx = NewContext(&OperationUserInfo{})
72-
if appCtx.operationUserInfo == nil {
73-
panic("appCtx.operationUserInfo == nil")
74-
}
75-
ctx = WrapAppContext(appCtx)
76-
appCtx2 = UnwrapAppContext(ctx)
77-
if appCtx.operationUserInfo != appCtx2.operationUserInfo {
78-
t.Errorf("UnwrapAppContext() = want the same operationUserInfo as original")
79-
return
80-
}
23+
require.Equal(t, appCtx, appCtx2, "want the same app context as original")
24+
require.Equal(t, appCtx.baseCtx, appCtx2.baseCtx, "want the same base context as original")
8125
}

cmd/types/operation_user.go

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

cmd/types/user_info.go

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

cmd/utils/input_util.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func ProvidedArgsOrFromPipe(providedArgs []string) (outputArgs []string, err err
2727
// RequireArgs will exit program if no arg provided.
2828
func RequireArgs(args []string, cmd *cobra.Command) {
2929
if len(args) == 0 {
30-
utils.PrintlnStdErr("ERR: require arg(s)\n")
30+
PrintlnStdErr("ERR: require arg(s)\n")
3131
_ = cmd.Help()
3232
os.Exit(1)
3333
}
@@ -37,11 +37,11 @@ func RequireArgs(args []string, cmd *cobra.Command) {
3737
func RequireExactArgsCount(args []string, want int, cmd *cobra.Command) {
3838
if len(args) != want {
3939
if want == 0 {
40-
utils.PrintlnStdErr("ERR: require no arg\n")
40+
PrintlnStdErr("ERR: require no arg\n")
4141
} else if len(args) == 0 {
42-
utils.PrintlnStdErr(fmt.Sprintf("ERR: require %d arg(s)\n", want))
42+
PrintlnStdErr(fmt.Sprintf("ERR: require %d arg(s)\n", want))
4343
} else {
44-
utils.PrintlnStdErr(fmt.Sprintf("ERR: require %d arg(s), got %d\n", want, len(args)))
44+
PrintlnStdErr(fmt.Sprintf("ERR: require %d arg(s), got %d\n", want, len(args)))
4545
}
4646
_ = cmd.Help()
4747
os.Exit(1)

0 commit comments

Comments
 (0)