From b9ef49b51c6e2570ef3aae824ab44374997b6b25 Mon Sep 17 00:00:00 2001
From: Ruben Pollan <meskio@sindominio.net>
Date: Mon, 4 Feb 2019 23:23:16 +0100
Subject: [PATCH] [feat] add -disable-autostart flag and config

Make possible to disable the autostart configuration for the next run. I
can be done by configuring "DisableAustostart" to true in the
systra.json or by passing the '-disable-autostart' flag to the binary.

To make that possible I have rework how the Config struct work
separating the file configuration with the runtime configuration.

- Resolves: #100
---
 cmd/bitmask-vpn/main.go  |  9 ++++++++-
 pkg/bitmask/autostart.go |  6 +++---
 pkg/bitmask/bitmaskd.go  |  2 +-
 pkg/systray/config.go    | 39 ++++++++++++++++++++++++++-------------
 pkg/systray/run.go       |  9 +++++++--
 5 files changed, 45 insertions(+), 20 deletions(-)

diff --git a/cmd/bitmask-vpn/main.go b/cmd/bitmask-vpn/main.go
index a9b2389..ad85b0e 100644
--- a/cmd/bitmask-vpn/main.go
+++ b/cmd/bitmask-vpn/main.go
@@ -49,13 +49,20 @@ func main() {
 
 	conf := systray.ParseConfig()
 
-	flag.BoolVar(&conf.SelectGateway, "select-gateway", false, "Enable gateway selection")
+	selectGateway := flag.Bool("select-gateway", false, "Enable gateway selection")
+	disableAutostart := flag.Bool("disable-autostart", false, "Disable the autostart for the next run")
 	versionFlag := flag.Bool("version", false, "Version of the bitmask-systray")
 	flag.Parse()
 	if *versionFlag {
 		fmt.Println(version)
 		os.Exit(0)
 	}
+	if *selectGateway {
+		conf.SelectGateway = *selectGateway
+	}
+	if *disableAutostart {
+		conf.DisableAustostart = *disableAutostart
+	}
 
 	conf.Version = version
 	conf.Printer = initPrinter()
diff --git a/pkg/bitmask/autostart.go b/pkg/bitmask/autostart.go
index ebab428..32b931a 100644
--- a/pkg/bitmask/autostart.go
+++ b/pkg/bitmask/autostart.go
@@ -21,12 +21,12 @@ type Autostart interface {
 	Enable() error
 }
 
-type dummyAutostart struct{}
+type DummyAutostart struct{}
 
-func (a *dummyAutostart) Disable() error {
+func (a *DummyAutostart) Disable() error {
 	return nil
 }
 
-func (a *dummyAutostart) Enable() error {
+func (a *DummyAutostart) Enable() error {
 	return nil
 }
diff --git a/pkg/bitmask/bitmaskd.go b/pkg/bitmask/bitmaskd.go
index 04b965c..a8c4e5d 100644
--- a/pkg/bitmask/bitmaskd.go
+++ b/pkg/bitmask/bitmaskd.go
@@ -40,5 +40,5 @@ func Init(printer *message.Printer) (Bitmask, error) {
 
 // NewAutostart creates a handler for the autostart of your platform
 func NewAutostart(appName string, iconPath string) Autostart {
-	return &dummyAutostart{}
+	return &DummyAutostart{}
 }
diff --git a/pkg/systray/config.go b/pkg/systray/config.go
index c40a279..2755851 100644
--- a/pkg/systray/config.go
+++ b/pkg/systray/config.go
@@ -36,12 +36,17 @@ var (
 
 // Config holds the configuration of the systray
 type Config struct {
-	LastNotification time.Time
-	Donated          time.Time
-	SelectGateway    bool
-	UserStoppedVPN   bool
-	Version          string           `json:"-"`
-	Printer          *message.Printer `json:"-"`
+	file struct {
+		LastNotification  time.Time
+		Donated           time.Time
+		SelectGateway     bool
+		UserStoppedVPN    bool
+		DisableAustostart bool
+	}
+	SelectGateway     bool
+	DisableAustostart bool
+	Version           string
+	Printer           *message.Printer
 }
 
 // ParseConfig reads the configuration from the configuration file
@@ -56,30 +61,37 @@ func ParseConfig() *Config {
 	defer f.Close()
 
 	dec := json.NewDecoder(f)
-	err = dec.Decode(&conf)
+	err = dec.Decode(&conf.file)
+
+	conf.SelectGateway = conf.file.SelectGateway
+	conf.DisableAustostart = conf.file.DisableAustostart
 	return &conf
 }
 
 func (c *Config) setUserStoppedVPN(vpnStopped bool) error {
-	c.UserStoppedVPN = vpnStopped
+	c.file.UserStoppedVPN = vpnStopped
 	return c.save()
 }
 
+func (c *Config) wasUserStopped() bool {
+	return c.file.UserStoppedVPN
+}
+
 func (c *Config) hasDonated() bool {
-	return c.Donated.Add(oneMonth).After(time.Now())
+	return c.file.Donated.Add(oneMonth).After(time.Now())
 }
 
 func (c *Config) needsNotification() bool {
-	return !c.hasDonated() && c.LastNotification.Add(oneDay).Before(time.Now())
+	return !c.hasDonated() && c.file.LastNotification.Add(oneDay).Before(time.Now())
 }
 
 func (c *Config) setNotification() error {
-	c.LastNotification = time.Now()
+	c.file.LastNotification = time.Now()
 	return c.save()
 }
 
 func (c *Config) setDonated() error {
-	c.Donated = time.Now()
+	c.file.Donated = time.Now()
 	return c.save()
 }
 
@@ -91,5 +103,6 @@ func (c *Config) save() error {
 	defer f.Close()
 
 	enc := json.NewEncoder(f)
-	return enc.Encode(c)
+	enc.SetIndent("", "  ")
+	return enc.Encode(c.file)
 }
diff --git a/pkg/systray/run.go b/pkg/systray/run.go
index 2a513d8..2878928 100644
--- a/pkg/systray/run.go
+++ b/pkg/systray/run.go
@@ -51,7 +51,12 @@ func initialize(conf *Config, bt *bmTray) {
 	go checkAndStartBitmask(b, notify, conf)
 	go listenSignals(b)
 
-	as := bitmask.NewAutostart(config.ApplicationName, getIconPath())
+	var as bitmask.Autostart
+	if conf.DisableAustostart {
+		as = &bitmask.DummyAutostart{}
+	} else {
+		as = bitmask.NewAutostart(config.ApplicationName, getIconPath())
+	}
 	err = as.Enable()
 	if err != nil {
 		log.Printf("Error enabling autostart: %v", err)
@@ -93,7 +98,7 @@ func checkAndInstallHelpers(b bitmask.Bitmask, notify *notificator) error {
 }
 
 func maybeStartVPN(b bitmask.Bitmask, conf *Config) error {
-	if conf.UserStoppedVPN {
+	if conf.wasUserStopped() {
 		return nil
 	}
 
-- 
GitLab