소스 검색

lib/config: Change folder type attribute to a FolderType type

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3032
Jakob Borg 9 년 전
부모
커밋
09832abe50
7개의 변경된 파일48개의 추가작업 그리고 11개의 파일을 삭제
  1. 1 1
      cmd/syncthing/main.go
  2. 0 3
      lib/config/config.go
  3. 2 3
      lib/config/folderconfiguration.go
  4. 41 0
      lib/config/foldertype.go
  5. 2 2
      lib/model/model.go
  6. 1 1
      lib/model/model_test.go
  7. 1 1
      test/override_test.go

+ 1 - 1
cmd/syncthing/main.go

@@ -988,7 +988,7 @@ func defaultConfig(myName string) config.Configuration {
 	if !noDefaultFolder {
 		l.Infoln("Default folder created and/or linked to new config")
 
-		defaultFolder = config.NewFolderConfiguration("default", locations[locDefFolder], config.FolderTypeReadWrite)
+		defaultFolder = config.NewFolderConfiguration("default", locations[locDefFolder])
 		defaultFolder.RescanIntervalS = 60
 		defaultFolder.MinDiskFreePct = 1
 		defaultFolder.Devices = []config.FolderDeviceConfiguration{{DeviceID: myID}}

+ 0 - 3
lib/config/config.go

@@ -23,9 +23,6 @@ const (
 	OldestHandledVersion = 10
 	CurrentVersion       = 13
 	MaxRescanIntervalS   = 365 * 24 * 60 * 60
-
-	FolderTypeReadWrite = "readwrite"
-	FolderTypeReadOnly  = "readonly"
 )
 
 var (

+ 2 - 3
lib/config/folderconfiguration.go

@@ -20,7 +20,7 @@ type FolderConfiguration struct {
 	ID                    string                      `xml:"id,attr" json:"id"`
 	Label                 string                      `xml:"label,attr" json:"label"`
 	RawPath               string                      `xml:"path,attr" json:"path"`
-	Type                  string                      `xml:"type,attr" json:"type"`
+	Type                  FolderType                  `xml:"type,attr" json:"type"`
 	Devices               []FolderDeviceConfiguration `xml:"device" json:"devices"`
 	RescanIntervalS       int                         `xml:"rescanIntervalS,attr" json:"rescanIntervalS"`
 	IgnorePerms           bool                        `xml:"ignorePerms,attr" json:"ignorePerms"`
@@ -49,11 +49,10 @@ type FolderDeviceConfiguration struct {
 	DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
 }
 
-func NewFolderConfiguration(id, path, foldertype string) FolderConfiguration {
+func NewFolderConfiguration(id, path string) FolderConfiguration {
 	f := FolderConfiguration{
 		ID:      id,
 		RawPath: path,
-		Type:    foldertype,
 	}
 	f.prepare()
 	return f

+ 41 - 0
lib/config/foldertype.go

@@ -0,0 +1,41 @@
+// Copyright (C) 2016 The Syncthing Authors.
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this file,
+// You can obtain one at http://mozilla.org/MPL/2.0/.
+
+package config
+
+type FolderType int
+
+const (
+	FolderTypeReadWrite FolderType = iota // default is readwrite
+	FolderTypeReadOnly
+)
+
+func (t FolderType) String() string {
+	switch t {
+	case FolderTypeReadWrite:
+		return "readwrite"
+	case FolderTypeReadOnly:
+		return "readonly"
+	default:
+		return "unknown"
+	}
+}
+
+func (t FolderType) MarshalText() ([]byte, error) {
+	return []byte(t.String()), nil
+}
+
+func (t *FolderType) UnmarshalText(bs []byte) error {
+	switch string(bs) {
+	case "readwrite":
+		*t = FolderTypeReadWrite
+	case "readonly":
+		*t = FolderTypeReadOnly
+	default:
+		*t = FolderTypeReadWrite
+	}
+	return nil
+}

+ 2 - 2
lib/model/model.go

@@ -104,7 +104,7 @@ type folderFactory func(*Model, config.FolderConfiguration, versioner.Versioner)
 
 var (
 	symlinkWarning  = stdsync.Once{}
-	folderFactories = make(map[string]folderFactory, 0)
+	folderFactories = make(map[config.FolderType]folderFactory, 0)
 )
 
 // NewModel creates and starts a new model. The model starts in read-only mode,
@@ -176,7 +176,7 @@ func (m *Model) StartFolder(folder string) {
 
 	folderFactory, ok := folderFactories[cfg.Type]
 	if !ok {
-		panic("unknown folder type " + cfg.Type)
+		panic(fmt.Sprintf("unknown folder type 0x%x", cfg.Type))
 	}
 
 	var ver versioner.Versioner

+ 1 - 1
lib/model/model_test.go

@@ -34,7 +34,7 @@ func init() {
 	device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
 	device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
 
-	defaultFolderConfig = config.NewFolderConfiguration("default", "testdata", config.FolderTypeReadWrite)
+	defaultFolderConfig = config.NewFolderConfiguration("default", "testdata")
 	defaultFolderConfig.Devices = []config.FolderDeviceConfiguration{{DeviceID: device1}}
 	_defaultConfig := config.Configuration{
 		Folders: []config.FolderConfiguration{defaultFolderConfig},

+ 1 - 1
test/override_test.go

@@ -27,7 +27,7 @@ func TestOverride(t *testing.T) {
 	id, _ := protocol.DeviceIDFromString(id1)
 	cfg, _ := config.Load("h1/config.xml", id)
 	fld := cfg.Folders()["default"]
-	fld.ReadOnly = true
+	fld.Type = config.FolderTypeReadOnly
 	cfg.SetFolder(fld)
 	os.Rename("h1/config.xml", "h1/config.xml.orig")
 	defer osutil.Rename("h1/config.xml.orig", "h1/config.xml")