Browse Source

Preserve newlines in example config

Tulir Asokan 3 years ago
parent
commit
e997df8c49
3 changed files with 39 additions and 17 deletions
  1. 29 4
      config/upgrade.go
  2. 10 8
      config/upgradehelper.go
  3. 0 5
      example-config.yaml

+ 29 - 4
config/upgrade.go

@@ -26,10 +26,6 @@ import (
 	"maunium.net/go/mautrix/appservice"
 )
 
-func (helper *UpgradeHelper) TempMethod() {
-	helper.doUpgrade()
-}
-
 func (helper *UpgradeHelper) doUpgrade() {
 	helper.Copy(Str, "homeserver", "address")
 	helper.Copy(Str, "homeserver", "domain")
@@ -134,6 +130,34 @@ func Upgrade(path string, save bool) ([]byte, bool, error) {
 	return upgrade(path, save, nil)
 }
 
+func (helper *UpgradeHelper) addSpaceBeforeComment(path ...string) {
+	node := helper.GetBaseNode(path...)
+	if node.Key == nil {
+		panic(fmt.Errorf("didn't find key at %+v", path))
+	}
+	node.Key.HeadComment = "\n" + node.Key.HeadComment
+}
+
+func (helper *UpgradeHelper) addSpaces() {
+	// The yaml package doesn't preserve blank lines, so re-add them manually where appropriate
+	helper.addSpaceBeforeComment("homeserver", "asmux")
+	helper.addSpaceBeforeComment("appservice")
+	helper.addSpaceBeforeComment("appservice", "hostname")
+	helper.addSpaceBeforeComment("appservice", "database")
+	helper.addSpaceBeforeComment("appservice", "provisioning")
+	helper.addSpaceBeforeComment("appservice", "id")
+	helper.addSpaceBeforeComment("appservice", "as_token")
+	helper.addSpaceBeforeComment("metrics")
+	helper.addSpaceBeforeComment("whatsapp")
+	helper.addSpaceBeforeComment("bridge")
+	helper.addSpaceBeforeComment("bridge", "command_prefix")
+	helper.addSpaceBeforeComment("bridge", "management_room_text")
+	helper.addSpaceBeforeComment("bridge", "encryption")
+	helper.addSpaceBeforeComment("bridge", "permissions")
+	helper.addSpaceBeforeComment("bridge", "relay")
+	helper.addSpaceBeforeComment("logging")
+}
+
 func upgrade(configPath string, save bool, mutate func(helper *UpgradeHelper)) ([]byte, bool, error) {
 	sourceData, err := os.ReadFile(configPath)
 	if err != nil {
@@ -154,6 +178,7 @@ func upgrade(configPath string, save bool, mutate func(helper *UpgradeHelper)) (
 	if mutate != nil {
 		mutate(helper)
 	}
+	helper.addSpaces()
 
 	output, err := yaml.Marshal(&base)
 	if err != nil {

+ 10 - 8
config/upgradehelper.go

@@ -31,6 +31,7 @@ type YAMLNode struct {
 	*yaml.Node
 	Map  YAMLMap
 	List YAMLList
+	Key  *yaml.Node
 }
 
 type YAMLType uint32
@@ -109,16 +110,17 @@ const (
 	BinaryTag    = "!!binary"
 )
 
-func fromNode(node *yaml.Node) YAMLNode {
+func fromNode(node, key *yaml.Node) YAMLNode {
 	switch node.Kind {
 	case yaml.DocumentNode:
-		return fromNode(node.Content[0])
+		return fromNode(node.Content[0], nil)
 	case yaml.AliasNode:
-		return fromNode(node.Alias)
+		return fromNode(node.Alias, nil)
 	case yaml.MappingNode:
 		return YAMLNode{
 			Node: node,
 			Map:  parseYAMLMap(node),
+			Key:  key,
 		}
 	case yaml.SequenceNode:
 		return YAMLNode{
@@ -126,7 +128,7 @@ func fromNode(node *yaml.Node) YAMLNode {
 			List: parseYAMLList(node),
 		}
 	default:
-		return YAMLNode{Node: node}
+		return YAMLNode{Node: node, Key: key}
 	}
 }
 
@@ -143,7 +145,7 @@ func (yn *YAMLNode) toNode() *yaml.Node {
 func parseYAMLList(node *yaml.Node) YAMLList {
 	data := make(YAMLList, len(node.Content))
 	for i, item := range node.Content {
-		data[i] = fromNode(item)
+		data[i] = fromNode(item, nil)
 	}
 	return data
 }
@@ -165,7 +167,7 @@ func parseYAMLMap(node *yaml.Node) YAMLMap {
 		key := node.Content[i]
 		value := node.Content[i+1]
 		if key.Kind == yaml.ScalarNode {
-			data[key.Value] = fromNode(value)
+			data[key.Value] = fromNode(value, key)
 		}
 	}
 	return data
@@ -195,8 +197,8 @@ type UpgradeHelper struct {
 
 func NewUpgradeHelper(base, cfg *yaml.Node) *UpgradeHelper {
 	return &UpgradeHelper{
-		base: fromNode(base),
-		cfg:  fromNode(cfg),
+		base: fromNode(base, nil),
+		cfg:  fromNode(cfg, nil),
 	}
 }
 

+ 0 - 5
example-config.yaml

@@ -87,7 +87,6 @@ bridge:
     # {{.FullName}}  - full name from contact list
     # {{.FirstName}} - first name from contact list
     displayname_template: "{{if .PushName}}{{.PushName}}{{else if .BusinessName}}{{.BusinessName}}{{else}}{{.JID}}{{end}} (WA)"
-
     # Should the bridge send a read receipt from the bridge bot when a message has been sent to WhatsApp?
     delivery_receipts: false
     # Should incoming calls send a message to the Matrix room?
@@ -96,9 +95,7 @@ bridge:
     identity_change_notices: false
     # Should a "reactions not yet supported" warning be sent to the Matrix room when a user reacts to a message?
     reaction_notices: true
-
     portal_message_buffer: 128
-
     # Settings for handling history sync payloads. These settings only apply right after login,
     # because the phone only sends the history sync data once, and there's no way to re-request it
     # (other than logging out and back in again).
@@ -124,7 +121,6 @@ bridge:
     user_avatar_sync: true
     # Should Matrix users leaving groups be bridged to WhatsApp?
     bridge_matrix_leave: true
-
     # Should the bridge sync with double puppeting to receive EDUs that aren't normally sent to appservices.
     sync_with_custom_puppets: true
     # Should the bridge update the m.direct account data event when double puppeting is enabled.
@@ -148,7 +144,6 @@ bridge:
     # manually.
     login_shared_secret_map:
         example.com: foobar
-
     # Should the bridge explicitly set the avatar and room name for private chat portal rooms?
     private_chat_portal_meta: false
     # Should Matrix m.notice-type messages be bridged?