-
Notifications
You must be signed in to change notification settings - Fork 0
/
show.go
52 lines (42 loc) · 1.02 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package user_console
import (
"encoding/json"
"fmt"
"github.com/evgeniums/go-backend-helpers/pkg/console_tool"
"github.com/evgeniums/go-backend-helpers/pkg/user"
"github.com/evgeniums/go-backend-helpers/pkg/utils"
)
const ShowCmd string = "show"
const ShowDescription string = "Show object"
func Show[T user.User]() console_tool.Handler[*UserCommands[T]] {
a := &ListHandler[T]{}
a.Init(ShowCmd, ShowDescription)
return a
}
type ShowHandler[T user.User] struct {
HandlerBase[T]
LoginData
}
func (a *ShowHandler[T]) Data() interface{} {
return &a.LoginData
}
func (a *ShowHandler[T]) Execute(args []string) error {
ctx, ctrl, err := a.Context(a.Data())
if err != nil {
return err
}
defer ctx.Close()
user, err := ctrl.FindByLogin(ctx, a.Login)
if err != nil {
return err
}
if utils.IsNil(user) {
return fmt.Errorf("user not found")
}
b, err := json.MarshalIndent(user, "", " ")
if err != nil {
return fmt.Errorf("failed to serialize result: %s", err)
}
fmt.Printf("\n\n%s\n\n", string(b))
return nil
}