Browse Source

Add folder list

dtluna 3 months ago
parent
commit
72f1e26890
6 changed files with 115 additions and 21 deletions
  1. 41
    20
      api/config.go
  2. 2
    1
      commands/device.go
  3. 21
    0
      commands/folder.go
  4. 23
    0
      format/device.go
  5. 23
    0
      format/folder.go
  6. 5
    0
      main.go

+ 41
- 20
api/config.go View File

@@ -18,11 +18,11 @@ func indent(s string, depth int) string {
18 18
 }
19 19
 
20 20
 func indentStringSlice(slice []string, depth int) string {
21
-	sep := strings.Repeat(" ", depth)
22 21
 	if len(slice) == 0 {
23 22
 		return "None"
24 23
 	}
25 24
 
25
+	sep := strings.Repeat(" ", depth)
26 26
 	lines := []string{}
27 27
 
28 28
 	for i, el := range slice {
@@ -39,9 +39,38 @@ func (fd FolderDevice) String() string {
39 39
 	return fmt.Sprint("ID: ", fd.DeviceID)
40 40
 }
41 41
 
42
+func indentFolderDevices(fds []FolderDevice, depth int) string {
43
+	if len(fds) == 0 {
44
+		return "None"
45
+	}
46
+
47
+	sep := strings.Repeat(" ", depth)
48
+	lines := []string{}
49
+
50
+	for i, el := range fds {
51
+		line := strings.ReplaceAll(fmt.Sprintf("%v. %s", i+1, el), "\n", "\n"+sep)
52
+		lines = append(lines, line)
53
+	}
54
+	return "\n" + strings.Join(lines, "\n")
55
+}
56
+
57
+type VersioningInfoParams map[string]interface{}
58
+
59
+func (vip VersioningInfoParams) String() string {
60
+	if len(vip) == 0 {
61
+		return "None"
62
+	}
63
+
64
+	slice := []string{}
65
+	for key, value := range vip {
66
+		slice = append(slice, fmt.Sprintf("%v: %v", key, value))
67
+	}
68
+	return strings.Join(slice, "\n")
69
+}
70
+
42 71
 type VersioningInfo struct {
43 72
 	Type   string
44
-	Params map[string]interface{}
73
+	Params VersioningInfoParams
45 74
 }
46 75
 
47 76
 func (vi VersioningInfo) String() string {
@@ -49,7 +78,7 @@ func (vi VersioningInfo) String() string {
49 78
 		`Type: %v
50 79
 Params: %v`,
51 80
 		vi.Type,
52
-		vi.Params,
81
+		indent(vi.Params.String(), 2),
53 82
 	)
54 83
 }
55 84
 
@@ -80,6 +109,13 @@ type Folder struct {
80 109
 }
81 110
 
82 111
 func (f Folder) String() string {
112
+	var viString string
113
+	if f.Versioning.Type == "" {
114
+		viString = "None"
115
+	} else {
116
+		viString = indent(f.Versioning.String(), 2)
117
+	}
118
+
83 119
 	return fmt.Sprintf(
84 120
 		`ID: %v
85 121
 Label: %v
@@ -91,8 +127,8 @@ Versioning: %v`,
91 127
 		f.Label,
92 128
 		f.Path,
93 129
 		f.Type,
94
-		f.Devices,
95
-		f.Versioning,
130
+		indentFolderDevices(f.Devices, 2),
131
+		viString,
96 132
 	)
97 133
 }
98 134
 
@@ -122,21 +158,6 @@ Introducer: %v`,
122 158
 	)
123 159
 }
124 160
 
125
-func IndentDevices(ds []Device, depth int) string {
126
-	sep := strings.Repeat(" ", depth)
127
-	if len(ds) == 0 {
128
-		return "None"
129
-	}
130
-
131
-	lines := []string{}
132
-
133
-	for i, el := range ds {
134
-		line := strings.ReplaceAll(fmt.Sprintf("%v. %s", i+1, el), "\n", "\n"+sep)
135
-		lines = append(lines, line)
136
-	}
137
-	return "\n" + strings.Join(lines, "\n")
138
-}
139
-
140 161
 type GUIConfig struct {
141 162
 	Enabled             bool
142 163
 	Address             string

+ 2
- 1
commands/device.go View File

@@ -5,6 +5,7 @@ import (
5 5
 
6 6
 	"git.dtluna.net/dtluna/syncthing-cli/api"
7 7
 	"git.dtluna.net/dtluna/syncthing-cli/config"
8
+	"git.dtluna.net/dtluna/syncthing-cli/format"
8 9
 
9 10
 	"github.com/hashicorp/errwrap"
10 11
 )
@@ -15,7 +16,7 @@ func DeviceList(cfg *config.Config) error {
15 16
 		return errwrap.Wrapf("getting config: {{err}}", err)
16 17
 	}
17 18
 
18
-	fmt.Println(api.IndentDevices(stconfig.Devices, 2))
19
+	fmt.Println(format.IndentDevices(stconfig.Devices, 2))
19 20
 	return nil
20 21
 }
21 22
 

+ 21
- 0
commands/folder.go View File

@@ -0,0 +1,21 @@
1
+package commands
2
+
3
+import (
4
+	"fmt"
5
+
6
+	"git.dtluna.net/dtluna/syncthing-cli/api"
7
+	"git.dtluna.net/dtluna/syncthing-cli/config"
8
+	"git.dtluna.net/dtluna/syncthing-cli/format"
9
+
10
+	"github.com/hashicorp/errwrap"
11
+)
12
+
13
+func FolderList(cfg *config.Config) error {
14
+	stconfig, err := api.GetConfig(cfg)
15
+	if err != nil {
16
+		return errwrap.Wrapf("getting config: {{err}}", err)
17
+	}
18
+
19
+	fmt.Println(format.IndentFolders(stconfig.Folders, 2))
20
+	return nil
21
+}

+ 23
- 0
format/device.go View File

@@ -0,0 +1,23 @@
1
+package format
2
+
3
+import (
4
+	"fmt"
5
+	"strings"
6
+
7
+	"git.dtluna.net/dtluna/syncthing-cli/api"
8
+)
9
+
10
+func IndentDevices(ds []api.Device, depth int) string {
11
+	if len(ds) == 0 {
12
+		return "None"
13
+	}
14
+
15
+	sep := strings.Repeat(" ", depth)
16
+	lines := []string{}
17
+
18
+	for i, el := range ds {
19
+		line := strings.ReplaceAll(fmt.Sprintf("%v. %s", i+1, el), "\n", "\n"+sep)
20
+		lines = append(lines, line)
21
+	}
22
+	return "\n" + strings.Join(lines, "\n")
23
+}

+ 23
- 0
format/folder.go View File

@@ -0,0 +1,23 @@
1
+package format
2
+
3
+import (
4
+	"fmt"
5
+	"strings"
6
+
7
+	"git.dtluna.net/dtluna/syncthing-cli/api"
8
+)
9
+
10
+func IndentFolders(ds []api.Folder, depth int) string {
11
+	if len(ds) == 0 {
12
+		return "None"
13
+	}
14
+
15
+	sep := strings.Repeat(" ", depth)
16
+	lines := []string{}
17
+
18
+	for i, el := range ds {
19
+		line := strings.ReplaceAll(fmt.Sprintf("%v. %s", i+1, el), "\n", "\n"+sep)
20
+		lines = append(lines, line)
21
+	}
22
+	return "\n" + strings.Join(lines, "\n")
23
+}

+ 5
- 0
main.go View File

@@ -67,6 +67,9 @@ func main() {
67 67
 	deviceList := device.Command("list", "List devices.").Alias("l").Alias("ls")
68 68
 	deviceStats := device.Command("stats", "Show device stats.").Alias("s").Alias("st")
69 69
 
70
+	folder := app.Command("folder", "Work with folders.").Alias("f").Alias("fl").Alias("fold")
71
+	folderList := folder.Command("list", "List folders.").Alias("l").Alias("ls")
72
+
70 73
 	commandName := kingpin.MustParse(app.Parse(os.Args[1:]))
71 74
 
72 75
 	cfg, err := config.Parse(*configPath)
@@ -85,6 +88,8 @@ func main() {
85 88
 			return commands.DeviceList(cfg)
86 89
 		case deviceStats.FullCommand():
87 90
 			return commands.DeviceStats(cfg)
91
+		case folderList.FullCommand():
92
+			return commands.FolderList(cfg)
88 93
 		}
89 94
 		return nil
90 95
 	}()

Loading…
Cancel
Save