forked from RiotGames/key-conjurer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (51 loc) · 1.47 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"os"
"strings"
"syscall"
"log/slog"
"github.com/riotgames/key-conjurer/command"
"github.com/spf13/cobra"
)
const (
// WSAEACCES is the Windows error code for attempting to access a socket that you don't have permission to access.
//
// This commonly occurs if the socket is in use or was not closed correctly, and can be resolved by restarting the hns service.
WSAEACCES = 10013
)
// IsWindowsPortAccessError determines if the given error is the error WSAEACCES.
func IsWindowsPortAccessError(err error) bool {
var syscallErr *syscall.Errno
return errors.As(err, &syscallErr) && *syscallErr == WSAEACCES
}
func init() {
var opts slog.HandlerOptions
if os.Getenv("DEBUG") == "1" {
opts.Level = slog.LevelDebug
}
handler := slog.NewTextHandler(os.Stdout, &opts)
slog.SetDefault(slog.New(handler))
}
func main() {
args := os.Args[1:]
if flag, ok := os.LookupEnv("KEYCONJURERFLAGS"); ok {
args = append(args, strings.Split(flag, " ")...)
}
err := command.Execute(context.Background(), args)
if IsWindowsPortAccessError(err) {
fmt.Fprintf(os.Stderr, "Encountered an issue when opening the port for KeyConjurer: %s\n", err)
fmt.Fprintln(os.Stderr, "Consider running `net stop hns` and then `net start hns`")
os.Exit(command.ExitCodeConnectivityError)
}
if err != nil {
cobra.CheckErr(err)
errorCode, ok := command.GetExitCode(err)
if !ok {
errorCode = command.ExitCodeUnknownError
}
os.Exit(errorCode)
}
}