-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmd.go
81 lines (69 loc) · 1.7 KB
/
cmd.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
81
// Package vpncscript contains the vpncscript.
package vpncscript
import (
"errors"
"flag"
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/telekom-mms/oc-daemon/internal/daemon"
)
const (
runDir = "/run/oc-daemon"
socketFile = runDir + "/daemon.sock"
)
// run is the main function.
func run(args []string) error {
// parse command line
flags := flag.NewFlagSet(args[0], flag.ContinueOnError)
verbose := flags.Bool("verbose", false, "enable verbose output")
version := flags.Bool("version", false, "print version")
if err := flags.Parse(args[1:]); err != nil {
return err
}
// print version?
if *version {
fmt.Println(daemon.Version)
return nil
}
// parse environment variables
e := parseEnvironment()
// set verbosity from command line or environment
if *verbose || e.verbose {
log.SetLevel(log.DebugLevel)
}
// set socket file from environment
socketFile := socketFile
if e.socketFile != "" {
socketFile = e.socketFile
}
printDebugEnvironment()
log.WithField("env", e).Debug("VPNCScript parsed environment")
// handle reason environment variable
switch e.reason {
case "pre-init":
return nil
case "connect", "disconnect":
c, err := createConfigUpdate(e)
if err != nil {
return fmt.Errorf("VPNCScript could not create config update: %w", err)
}
log.WithField("update", c).Debug("VPNCScript created config update")
return runClient(socketFile, c)
case "attempt-reconnect":
return nil
case "reconnect":
return nil
default:
return errors.New("VPNCScript called with unknown reason")
}
}
// Run is the main entry point of vpnc script.
func Run() {
if err := run(os.Args); err != nil {
if err != flag.ErrHelp {
log.Fatal(err)
}
return
}
}