bridge: validate config before use

Michael Muré created

Change summary

bridge/core/bridge.go     |  5 +++++
bridge/core/interfaces.go |  3 +++
bridge/github/config.go   | 16 ++++++++++++++++
3 files changed, 24 insertions(+)

Detailed changes

bridge/core/bridge.go 🔗

@@ -200,6 +200,11 @@ func (b Bridge) loadConfig() (Configuration, error) {
 		result[key] = value
 	}
 
+	err = b.impl.ValidateConfig(result)
+	if err != nil {
+		return nil, errors.Wrap(err, "invalid configuration")
+	}
+
 	return result, nil
 }
 

bridge/core/interfaces.go 🔗

@@ -15,6 +15,9 @@ type BridgeImpl interface {
 	// for future use
 	Configure(repo repository.RepoCommon) (Configuration, error)
 
+	// ValidateConfig check the configuration for error
+	ValidateConfig(conf Configuration) error
+
 	// Importer return an Importer implementation if the import is supported
 	Importer() Importer
 

bridge/github/config.go 🔗

@@ -93,6 +93,22 @@ func (*Github) Configure(repo repository.RepoCommon) (core.Configuration, error)
 	return nil, nil
 }
 
+func (*Github) ValidateConfig(conf core.Configuration) error {
+	if _, ok := conf[keyToken]; !ok {
+		return fmt.Errorf("missing %s key", keyToken)
+	}
+
+	if _, ok := conf[keyUser]; !ok {
+		return fmt.Errorf("missing %s key", keyUser)
+	}
+
+	if _, ok := conf[keyProject]; !ok {
+		return fmt.Errorf("missing %s key", keyProject)
+	}
+
+	return nil
+}
+
 func requestToken(note, username, password string) (*http.Response, error) {
 	return requestTokenWith2FA(note, username, password, "")
 }