Skip to content

Commit 1b36845

Browse files
Improve show-prefix command (cozy#1597)
* Rename show-prefix to show-db-prefix for cozy-stack instances CLI * Improve show-db-prefix CLI output * Update docs
1 parent 71db422 commit 1b36845

6 files changed

+85
-32
lines changed

client/instances.go

+7
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ type ImportOptions struct {
101101
IncreaseQuota bool
102102
}
103103

104+
func (i *Instance) DBPrefix() string {
105+
if i.Attrs.Prefix != "" {
106+
return i.Attrs.Prefix
107+
}
108+
return i.Attrs.Domain
109+
}
110+
104111
// GetInstance returns the instance associated with the specified domain.
105112
func (c *Client) GetInstance(domain string) (*Instance, error) {
106113
res, err := c.Req(&request.Options{

cmd/instances.go

+62-17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"net/url"
1010
"os"
11+
"reflect"
1112
"strconv"
1213
"strings"
1314
"text/tabwriter"
@@ -16,6 +17,7 @@ import (
1617
"github.com/cozy/cozy-stack/client"
1718
"github.com/cozy/cozy-stack/client/request"
1819
"github.com/cozy/cozy-stack/pkg/consts"
20+
"github.com/cozy/cozy-stack/pkg/couchdb"
1921
"github.com/cozy/cozy-stack/pkg/instance"
2022
humanize "github.com/dustin/go-humanize"
2123
"github.com/spf13/cobra"
@@ -50,6 +52,7 @@ var flagOnboardingFinished bool
5052
var flagExpire time.Duration
5153
var flagAllowLoginScope bool
5254
var flagFsckIndexIntegrity bool
55+
var flagAvailableFields bool
5356

5457
// instanceCmdGroup represents the instances command
5558
var instanceCmdGroup = &cobra.Command{
@@ -97,14 +100,14 @@ given domain.
97100
},
98101
}
99102

100-
var showPrefixInstanceCmd = &cobra.Command{
101-
Use: "show-prefix <domain>",
102-
Short: "Show the instance prefix of the specified domain",
103+
var showDBPrefixInstanceCmd = &cobra.Command{
104+
Use: "show-db-prefix <domain>",
105+
Short: "Show the instance DB prefix of the specified domain",
103106
Long: `
104107
cozy-stack instances show allows to show the instance prefix on the cozy for a
105108
given domain. The prefix is used for databases and VFS prefixing.
106109
`,
107-
Example: "$ cozy-stack instances show-prefix cozy.tools:8080",
110+
Example: "$ cozy-stack instances show-db-prefix cozy.tools:8080",
108111
RunE: func(cmd *cobra.Command, args []string) error {
109112
if len(args) == 0 {
110113
return cmd.Usage()
@@ -118,7 +121,7 @@ given domain. The prefix is used for databases and VFS prefixing.
118121
if in.Attrs.Prefix != "" {
119122
fmt.Println(in.Attrs.Prefix)
120123
} else {
121-
fmt.Println(in.Attrs.Domain)
124+
fmt.Println(couchdb.EscapeCouchdbName(in.Attrs.Domain))
122125
}
123126
return nil
124127
},
@@ -328,18 +331,42 @@ by this server.
328331
if err != nil {
329332
return err
330333
}
334+
if flagAvailableFields {
335+
instance := list[0]
336+
val := reflect.ValueOf(instance.Attrs)
337+
t := val.Type()
338+
for i := 0; i < t.NumField(); i++ {
339+
param := t.Field(i).Tag.Get("json")
340+
fmt.Println(strings.TrimSuffix(param, ",omitempty"))
341+
}
342+
fmt.Println("db_prefix")
343+
return nil
344+
}
331345
if flagJSON {
332346
if len(flagListFields) > 0 {
333347
for _, inst := range list {
334-
var values []interface{}
348+
var values map[string]interface{}
335349
values, err = extractFields(inst.Attrs, flagListFields)
336350
if err != nil {
337351
return err
338352
}
353+
354+
// Insert the db_prefix value if needed
355+
for _, v := range flagListFields {
356+
if v == "db_prefix" {
357+
values["db_prefix"] = couchdb.EscapeCouchdbName(inst.DBPrefix())
358+
}
359+
}
360+
339361
m := make(map[string]interface{}, len(flagListFields))
340-
for i, fieldName := range flagListFields {
341-
m[fieldName] = values[i]
362+
for _, fieldName := range flagListFields {
363+
if v, ok := values[fieldName]; ok {
364+
m[fieldName] = v
365+
} else {
366+
m[fieldName] = nil
367+
}
342368
}
369+
343370
if err = json.NewEncoder(os.Stdout).Encode(m); err != nil {
344371
return err
345372
}
@@ -357,27 +384,44 @@ by this server.
357384
format := strings.Repeat("%v\t", len(flagListFields))
358385
format = format[:len(format)-1] + "\n"
359386
for _, inst := range list {
360-
var values []interface{}
387+
var values map[string]interface{}
388+
var instancesLines []interface{}
389+
361390
values, err = extractFields(inst.Attrs, flagListFields)
362391
if err != nil {
363392
return err
364393
}
365-
fmt.Fprintf(w, format, values...)
394+
395+
// Insert the db_prefix value if needed
396+
for _, v := range flagListFields {
397+
if v == "db_prefix" {
398+
values["db_prefix"] = couchdb.EscapeCouchdbName(inst.DBPrefix())
399+
}
400+
}
401+
// We append to a list to print in the same order as
402+
// requested
403+
for _, fieldName := range flagListFields {
404+
instancesLines = append(instancesLines, values[fieldName])
405+
}
406+
407+
fmt.Fprintf(w, format, instancesLines...)
366408
}
367409
} else {
368410
for _, i := range list {
369411
prefix := i.Attrs.Prefix
412+
DBPrefix := prefix
370413
if prefix == "" {
371-
prefix = i.Attrs.Domain
414+
DBPrefix = couchdb.EscapeCouchdbName(i.Attrs.Domain)
372415
}
373-
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\tv%d\t%s\n",
416+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\tv%d\t%s\t%s\n",
374417
i.Attrs.Domain,
375418
i.Attrs.Locale,
376419
formatSize(i.Attrs.BytesDiskQuota),
377420
formatDev(i.Attrs.Dev),
378421
formatOnboarded(i),
379422
i.Attrs.IndexViewsVersion,
380423
prefix,
424+
DBPrefix,
381425
)
382426
}
383427
}
@@ -387,7 +431,7 @@ by this server.
387431
},
388432
}
389433

390-
func extractFields(data interface{}, fieldsNames []string) (values []interface{}, err error) {
434+
func extractFields(data interface{}, fieldsNames []string) (values map[string]interface{}, err error) {
391435
var m map[string]interface{}
392436
var b []byte
393437
b, err = json.Marshal(data)
@@ -397,10 +441,10 @@ func extractFields(data interface{}, fieldsNames []string) (values []interface{}
397441
if err = json.Unmarshal(b, &m); err != nil {
398442
return
399443
}
400-
values = make([]interface{}, len(fieldsNames))
401-
for i, fieldName := range fieldsNames {
444+
values = make(map[string]interface{}, len(fieldsNames))
445+
for _, fieldName := range fieldsNames {
402446
if v, ok := m[fieldName]; ok {
403-
values[i] = v
447+
values[fieldName] = v
404448
}
405449
}
406450
return
@@ -796,7 +840,7 @@ var showSwiftPrefixInstanceCmd = &cobra.Command{
796840

797841
func init() {
798842
instanceCmdGroup.AddCommand(showInstanceCmd)
799-
instanceCmdGroup.AddCommand(showPrefixInstanceCmd)
843+
instanceCmdGroup.AddCommand(showDBPrefixInstanceCmd)
800844
instanceCmdGroup.AddCommand(addInstanceCmd)
801845
instanceCmdGroup.AddCommand(modifyInstanceCmd)
802846
instanceCmdGroup.AddCommand(lsInstanceCmd)
@@ -852,6 +896,7 @@ func init() {
852896
appTokenInstanceCmd.Flags().DurationVar(&flagExpire, "expire", 0, "Make the token expires in this amount of time")
853897
lsInstanceCmd.Flags().BoolVar(&flagJSON, "json", false, "Show each line as a json representation of the instance")
854898
lsInstanceCmd.Flags().StringSliceVar(&flagListFields, "fields", nil, "Arguments shown for each line in the list")
899+
lsInstanceCmd.Flags().BoolVar(&flagAvailableFields, "available-fields", false, "List available fields for --fields option")
855900
updateCmd.Flags().BoolVar(&flagAllDomains, "all-domains", false, "Work on all domains iterativelly")
856901
updateCmd.Flags().StringVar(&flagDomain, "domain", "", "Specify the domain name of the instance")
857902
updateCmd.Flags().StringVar(&flagContextName, "context-name", "", "Work only on the instances with the given context name")

docs/cli/cozy-stack_instances.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ cozy-stack instances <command> [flags]
5151
* [cozy-stack instances refresh-token-oauth](cozy-stack_instances_refresh-token-oauth.md) - Generate a new OAuth refresh token
5252
* [cozy-stack instances set-disk-quota](cozy-stack_instances_set-disk-quota.md) - Change the disk-quota of the instance
5353
* [cozy-stack instances show](cozy-stack_instances_show.md) - Show the instance of the specified domain
54-
* [cozy-stack instances show-prefix](cozy-stack_instances_show-prefix.md) - Show the instance prefix of the specified domain
54+
* [cozy-stack instances show-db-prefix](cozy-stack_instances_show-db-prefix.md) - Show the instance DB prefix of the specified domain
5555
* [cozy-stack instances show-swift-prefix](cozy-stack_instances_show-swift-prefix.md) - Show the instance swift prefix of the specified domain
5656
* [cozy-stack instances token-app](cozy-stack_instances_token-app.md) - Generate a new application token
5757
* [cozy-stack instances token-cli](cozy-stack_instances_token-cli.md) - Generate a new CLI access token (global access)

docs/cli/cozy-stack_instances_ls.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ cozy-stack instances ls [flags]
1616
### Options
1717

1818
```
19-
--fields strings Arguments shown for each line in the list
20-
-h, --help help for ls
21-
--json Show each line as a json representation of the instance
19+
--available-fields List available fields for --fields option
20+
--fields strings Arguments shown for each line in the list
21+
-h, --help help for ls
22+
--json Show each line as a json representation of the instance
2223
```
2324

2425
### Options inherited from parent commands

docs/cli/cozy-stack_instances_show-prefix.md docs/cli/cozy-stack_instances_show-db-prefix.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## cozy-stack instances show-prefix
1+
## cozy-stack instances show-db-prefix
22

3-
Show the instance prefix of the specified domain
3+
Show the instance DB prefix of the specified domain
44

55
### Synopsis
66

@@ -10,19 +10,19 @@ given domain. The prefix is used for databases and VFS prefixing.
1010

1111

1212
```
13-
cozy-stack instances show-prefix <domain> [flags]
13+
cozy-stack instances show-db-prefix <domain> [flags]
1414
```
1515

1616
### Examples
1717

1818
```
19-
$ cozy-stack instances show-prefix cozy.tools:8080
19+
$ cozy-stack instances show-db-prefix cozy.tools:8080
2020
```
2121

2222
### Options
2323

2424
```
25-
-h, --help help for show-prefix
25+
-h, --help help for show-db-prefix
2626
```
2727

2828
### Options inherited from parent commands

pkg/couchdb/couchdb.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -232,23 +232,23 @@ func (j JSONDoc) Match(field, value string) bool {
232232
return fmt.Sprintf("%v", j.Get(field)) == value
233233
}
234234

235-
func unescapeCouchdbName(name string) string {
235+
func UnescapeCouchdbName(name string) string {
236236
return strings.Replace(name, "-", ".", -1)
237237
}
238238

239-
func escapeCouchdbName(name string) string {
239+
func EscapeCouchdbName(name string) string {
240240
name = strings.Replace(name, ".", "-", -1)
241241
name = strings.Replace(name, ":", "-", -1)
242242
return strings.ToLower(name)
243243
}
244244

245245
func makeDBName(db Database, doctype string) string {
246-
dbname := escapeCouchdbName(db.DBPrefix() + "/" + doctype)
246+
dbname := EscapeCouchdbName(db.DBPrefix() + "/" + doctype)
247247
return url.PathEscape(dbname)
248248
}
249249

250250
func dbNameHasPrefix(dbname, dbprefix string) (bool, string) {
251-
dbprefix = escapeCouchdbName(dbprefix + "/")
251+
dbprefix = EscapeCouchdbName(dbprefix + "/")
252252
if !strings.HasPrefix(dbname, dbprefix) {
253253
return false, ""
254254
}
@@ -360,12 +360,12 @@ func AllDoctypes(db Database) ([]string, error) {
360360
if err := makeRequest(db, "", http.MethodGet, "_all_dbs", nil, &dbs); err != nil {
361361
return nil, err
362362
}
363-
prefix := escapeCouchdbName(db.DBPrefix())
363+
prefix := EscapeCouchdbName(db.DBPrefix())
364364
var doctypes []string
365365
for _, dbname := range dbs {
366366
parts := strings.Split(dbname, "/")
367367
if len(parts) == 2 && parts[0] == prefix {
368-
doctype := unescapeCouchdbName(parts[1])
368+
doctype := UnescapeCouchdbName(parts[1])
369369
doctypes = append(doctypes, doctype)
370370
}
371371
}

0 commit comments

Comments
 (0)