/
utils_unix.go
80 lines (69 loc) · 2.11 KB
/
utils_unix.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//go:build linux || darwin
// +build linux darwin
package commands
import (
"fmt"
"os"
"os/user"
"syscall"
"github.com/isacikgoz/prompt"
"github.com/pkg/errors"
)
func checkValidSocket(socketPath string) error {
// check file mode and permissions
fi, err := os.Stat(socketPath)
if err != nil && os.IsNotExist(err) {
return fmt.Errorf("socket file %q doesn't exists, please check the server configuration for local mode", socketPath)
} else if err != nil {
return err
}
if fi.Mode() != expectedSocketMode {
return fmt.Errorf("invalid file mode for file %q, it must be a socket with 0600 permissions", socketPath)
}
// check matching user
cUser, err := user.Current()
if err != nil {
return err
}
s, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return fmt.Errorf("cannot get owner of the file %q", socketPath)
}
// if user id is "0", they are root and we should avoid this check
if fmt.Sprint(s.Uid) != cUser.Uid && cUser.Uid != "0" {
return fmt.Errorf("owner of the file %q must be the same user running mmctl", socketPath)
}
return nil
}
func getConfirmation(question string, dbConfirmation bool) error {
if err := checkInteractiveTerminal(); err != nil {
return fmt.Errorf("could not proceed, either enable --confirm flag or use an interactive shell to complete operation: %w", err)
}
if dbConfirmation {
s, err := prompt.NewSelection("Have you performed a database backup?", []string{"no", "yes"}, "", 2)
if err != nil {
return fmt.Errorf("could not initiate prompt: %w", err)
}
ans, err := s.Run()
if err != nil {
return fmt.Errorf("error running prompt: %w", err)
}
if ans != "yes" {
return errors.New("aborted")
}
}
s, err := prompt.NewSelection(question, []string{"no", "yes"}, "WARNING: This operation is not reversible.", 2)
if err != nil {
return fmt.Errorf("could not initiate prompt: %w", err)
}
ans, err := s.Run()
if err != nil {
return fmt.Errorf("error running prompt: %w", err)
}
if ans != "yes" {
return errors.New("aborted")
}
return nil
}