Skip to content

Commit 86a79c3

Browse files
Overhaul of agruments and flags. (#26)
* feat: Add plain output, API overhaul. BREAKING CHANGE: Change in flags. * refactor: Change Arguments and Flags. BREAKING CHANGE: Change in API.
1 parent e838704 commit 86a79c3

13 files changed

+275
-61
lines changed

cmd/aux_choice_flag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (a *tChoice) Set(p string) error {
4040
}
4141

4242
if !isIncluded(a.Allowed, p) {
43-
return fmt.Errorf("%s is not included in {%s}", p, strings.Join(a.Allowed, "|"))
43+
return fmt.Errorf("\"%s\" is not included in {%s}", p, strings.Join(a.Allowed, "|"))
4444
}
4545

4646
/* Set the value */

cmd/columns_sshcerts.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func getSshColumns() []tSshColumn {
2828
columns = append(columns,
2929

3030
tSshColumn{
31-
isShown: func(_ tConfig) bool { return true }, // Always shown.
31+
isShown: func(tc tConfig) bool { return tc.showSerial },
3232
title: func() string { return "Serial number" }, // Static title.
3333
titleColor: color.Bold,
3434

@@ -89,7 +89,7 @@ func getSshColumns() []tSshColumn {
8989
titleColor: color.Bold,
9090

9191
contentSource: func(x tSshCertificateWithRevocation, tc tConfig) string {
92-
if tc.timeFormat.Value == "s" {
92+
if tc.timeFormat.Value == TIME_SHORT {
9393
return time.Unix(int64(x.SshCertificate.ValidAfter), 0).UTC().Format(time.DateOnly)
9494
} else {
9595
return time.Unix(int64(x.SshCertificate.ValidAfter), 0).UTC().Format(time.RFC3339)
@@ -107,7 +107,7 @@ func getSshColumns() []tSshColumn {
107107
titleColor: color.Bold,
108108

109109
contentSource: func(x tSshCertificateWithRevocation, tc tConfig) string {
110-
if tc.timeFormat.Value == "s" {
110+
if tc.timeFormat.Value == TIME_SHORT {
111111
return time.Unix(int64(x.SshCertificate.ValidBefore), 0).UTC().Format(time.DateOnly)
112112
} else {
113113
return time.Unix(int64(x.SshCertificate.ValidBefore), 0).UTC().Format(time.RFC3339)
@@ -126,7 +126,7 @@ func getSshColumns() []tSshColumn {
126126

127127
contentSource: func(x tSshCertificateWithRevocation, tc tConfig) string {
128128
if len(x.SshCertificateRevocation.ProvisionerID) > 0 {
129-
if tc.timeFormat.Value == "s" {
129+
if tc.timeFormat.Value == TIME_SHORT {
130130
return x.SshCertificateRevocation.RevokedAt.UTC().Format(time.DateOnly)
131131
} else {
132132
return x.SshCertificateRevocation.RevokedAt.UTC().Format(time.RFC3339)

cmd/columns_x509certs.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func getX509Columns() []tX509Column {
2727
columns = append(columns,
2828

2929
tX509Column{
30-
isShown: func(_ tConfig) bool { return true }, // Always shown.
30+
isShown: func(tc tConfig) bool { return tc.showSerial },
3131
title: func() string { return "Serial number" }, // Static title.
3232
titleColor: color.Bold,
3333

@@ -54,6 +54,20 @@ func getX509Columns() []tX509Column {
5454
contentEscapeMD: true,
5555
},
5656

57+
tX509Column{
58+
isShown: func(tc tConfig) bool { return tc.showIssuer },
59+
title: func() string { return "Issuer" }, // Static title.
60+
titleColor: color.Bold,
61+
62+
contentSource: func(x tX509CertificateProvisionerRevocation, _ tConfig) string {
63+
return x.X509Certificate.Issuer.CommonName
64+
},
65+
66+
contentColor: func(_ tX509CertificateProvisionerRevocation) color.Attribute { return color.FgWhite }, // Static color.
67+
contentAlignMD: ALIGN_LEFT,
68+
contentEscapeMD: true,
69+
},
70+
5771
tX509Column{
5872
isShown: func(tc tConfig) bool { return tc.showDNSNames },
5973
title: func() string { return "DNS names" }, // Static title.
@@ -152,7 +166,7 @@ func getX509Columns() []tX509Column {
152166
titleColor: color.Bold,
153167

154168
contentSource: func(x tX509CertificateProvisionerRevocation, tc tConfig) string {
155-
if tc.timeFormat.Value == "s" {
169+
if tc.timeFormat.Value == TIME_SHORT {
156170
return x.X509Certificate.NotBefore.UTC().Format(time.DateOnly)
157171
} else {
158172
return x.X509Certificate.NotBefore.UTC().Format(time.RFC3339)
@@ -170,7 +184,7 @@ func getX509Columns() []tX509Column {
170184
titleColor: color.Bold,
171185

172186
contentSource: func(x tX509CertificateProvisionerRevocation, tc tConfig) string {
173-
if tc.timeFormat.Value == "s" {
187+
if tc.timeFormat.Value == TIME_SHORT {
174188
return x.X509Certificate.NotAfter.UTC().Format(time.DateOnly)
175189
} else {
176190
return x.X509Certificate.NotAfter.UTC().Format(time.RFC3339)
@@ -189,7 +203,7 @@ func getX509Columns() []tX509Column {
189203

190204
contentSource: func(x tX509CertificateProvisionerRevocation, tc tConfig) string {
191205
if len(x.X509Revocation.ProvisionerID) > 0 {
192-
if tc.timeFormat.Value == "s" {
206+
if tc.timeFormat.Value == TIME_SHORT {
193207
return x.X509Revocation.RevokedAt.UTC().Format(time.DateOnly)
194208
} else {
195209
return x.X509Revocation.RevokedAt.UTC().Format(time.RFC3339)

cmd/command_dbtable.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,21 @@ import (
1111

1212
// dbTableCmd represents the shell command.
1313
var dbTableCmd = &cobra.Command{
14-
Use: "dbTable PATH TABLE",
15-
Short: "Export badger table.",
16-
Long: `Export data table out of the badger database of step-ca. For list of tables see: https://raw.githubusercontent.com/smallstep/certificates/master/db/db.go`,
14+
Long: `
15+
Export data table out of the badger database of step-ca.`,
1716

17+
Short: "Export badger table.",
18+
DisableFlagsInUseLine: true,
19+
Use: `dbTable <PATH> <TABLE> [flags]
20+
21+
Arguments:
22+
PATH location of the source database
23+
TABLE name of Badger table to export
24+
25+
Note:
26+
For list of tables see: https://raw.githubusercontent.com/smallstep/certificates/master/db/db.go`,
27+
28+
Aliases: []string{"dbtable"},
1829
Example: " step-badger dbTable ./db ssh_host_principals",
1930

2031
Args: cobra.ExactArgs(2),

cmd/command_markdownDocs.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright © 2024 Lukasz Lobocki
3+
*/
4+
package cmd
5+
6+
import (
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/cobra/doc"
9+
)
10+
11+
// markdownDocsCmd represents the markdownDocs command
12+
var markdownDocsCmd = &cobra.Command{
13+
Long: `
14+
Generate markdown docs for the entire command tree.`,
15+
Short: "Generate markdown docs.",
16+
DisableFlagsInUseLine: true,
17+
Use: `markdownDocs <PATH> [flags]
18+
19+
Arguments:
20+
PATH location for the result`,
21+
22+
Aliases: []string{"markdowndocs"},
23+
24+
Args: cobra.ExactArgs(1),
25+
26+
Example: ` step-badger markdownDocs ~/tmp`,
27+
28+
Hidden: true, // Hide command from the tree.
29+
30+
Run: func(cmd *cobra.Command, args []string) {
31+
exportMarkdownMain(args)
32+
},
33+
}
34+
35+
func init() {
36+
rootCmd.AddCommand(markdownDocsCmd)
37+
38+
// Hide help command.
39+
markdownDocsCmd.SetHelpCommand(&cobra.Command{Hidden: true})
40+
41+
//Do not sort flags.
42+
markdownDocsCmd.Flags().SortFlags = false
43+
}
44+
45+
func exportMarkdownMain(args []string) {
46+
47+
checkLogginglevel(args)
48+
49+
err := doc.GenMarkdownTree(rootCmd, args[0])
50+
if err != nil {
51+
logError.Fatalln(err)
52+
}
53+
}

cmd/command_root.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ var config tConfig // Holds configuration.
3939

4040
// rootCmd represents the base command when called without any subcommands.
4141
var rootCmd = &cobra.Command{
42-
Use: "step-badger",
43-
Short: "Export step-ca data from badger.",
44-
Long: `Export certificate or table data from the badger database of step-ca. Requires off-line database directory.`,
45-
Version: semReleaseVersion,
42+
Use: "step-badger",
43+
Short: "Export step-ca data from badger.",
44+
Long: `Export certificate or table data from the badger database of step-ca. Requires off-line database directory.`,
45+
Version: semReleaseVersion,
46+
DisableAutoGenTag: true, // Do not add footer to autogenerated help.
4647

4748
CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true},
4849

cmd/command_sshcerts.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ import (
1515

1616
// sshCertsCmd represents the shell command.
1717
var sshCertsCmd = &cobra.Command{
18-
Use: "sshCerts PATH",
19-
Short: "Export ssh certificates.",
20-
Long: `Export ssh certificates' data out of the badger database of step-ca.`,
18+
Long: `
19+
Export ssh certificates' data out of the badger database of step-ca.`,
2120

22-
Example: " step-badger ssCerts ./db",
21+
Short: "Export ssh certificates.",
22+
DisableFlagsInUseLine: true,
23+
Use: `sshCerts <PATH> [flags]
24+
25+
Arguments:
26+
PATH location of the source database`,
27+
28+
Aliases: []string{"sshcerts"},
29+
Example: " step-badger sshCerts ./db",
2330

2431
Args: cobra.ExactArgs(1),
2532

@@ -42,16 +49,17 @@ func init() {
4249

4350
// Records selection criteria.
4451
sshCertsCmd.Flags().BoolVarP(&config.showValid, "valid", "v", true, "valid certificates shown")
45-
sshCertsCmd.Flags().BoolVarP(&config.showRevoked, "revoked", "r", true, "revoked certificates shown")
46-
sshCertsCmd.Flags().BoolVarP(&config.showExpired, "expired", "x", false, "expired certificates shown")
52+
sshCertsCmd.Flags().BoolVarP(&config.showRevoked, "revoked", "r", false, "revoked certificates shown")
53+
sshCertsCmd.Flags().BoolVarP(&config.showExpired, "expired", "e", false, "expired certificates shown")
4754

4855
// Format choice
49-
sshCertsCmd.Flags().VarP(config.emitSshFormat, "emit", "e", "emit format: table|json|markdown")
50-
sshCertsCmd.Flags().VarP(config.timeFormat, "time", "t", "time format: iso|short")
51-
sshCertsCmd.Flags().VarP(config.sortOrder, "sort", "s", "sort order: start|finish")
56+
sshCertsCmd.Flags().Var(config.emitSshFormat, "emit", "emit format: "+FORMAT_TABLE+"|"+FORMAT_JSON+"|"+FORMAT_MARKDOWN+"|"+FORMAT_PLAIN)
57+
sshCertsCmd.Flags().Var(config.timeFormat, "time", "time format: "+TIME_ISO+"|"+TIME_SHORT)
58+
sshCertsCmd.Flags().Var(config.sortOrder, "sort", "sort order: "+SORT_START+"|"+SORT_FINISH)
5259

5360
// Columns selection criteria.
54-
sshCertsCmd.Flags().BoolVarP(&config.showKeyId, "keyid", "", false, "key id column shown")
61+
sshCertsCmd.Flags().BoolVar(&config.showSerial, "serial", true, "serial column shown")
62+
sshCertsCmd.Flags().BoolVar(&config.showKeyId, "keyid", false, "key id column shown")
5563
}
5664

5765
/*
@@ -138,24 +146,26 @@ func exportSshMain(args []string) {
138146

139147
// Sort.
140148
switch thisSort := config.sortOrder.Value; thisSort {
141-
case "f":
149+
case SORT_FINISH:
142150
sort.SliceStable(sshCertificatesWithRevocations, func(i, j int) bool {
143151
return sshCertificatesWithRevocations[i].SshCertificate.ValidBefore < sshCertificatesWithRevocations[j].SshCertificate.ValidBefore
144152
})
145-
case "s":
153+
case SORT_START:
146154
sort.SliceStable(sshCertificatesWithRevocations, func(i, j int) bool {
147155
return sshCertificatesWithRevocations[i].SshCertificate.ValidAfter < sshCertificatesWithRevocations[j].SshCertificate.ValidAfter
148156
})
149157
}
150158

151159
// Output.
152160
switch format := config.emitSshFormat.Value; format {
153-
case "j":
161+
case FORMAT_JSON:
154162
emitSshCertsJson(sshCertificatesWithRevocations)
155-
case "t":
163+
case FORMAT_TABLE:
156164
emitSshCertsTable(sshCertificatesWithRevocations)
157-
case "m":
165+
case FORMAT_MARKDOWN:
158166
emitSshCertsMarkdown(sshCertificatesWithRevocations)
167+
case FORMAT_PLAIN:
168+
emitSshCertsPlain(sshCertificatesWithRevocations)
159169
}
160170
}
161171

cmd/command_x509certs.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ import (
1717

1818
// x509certsCmd represents the shell command.
1919
var x509certsCmd = &cobra.Command{
20-
Use: "x509Certs PATH",
21-
Short: "Export x509 certificates.",
22-
Long: `Export x509 certificates' data out of the badger database of step-ca.`,
20+
Long: `
21+
Export x509 certificates' data out of the badger database of step-ca.`,
2322

24-
Example: " step-badger x509certs ./db",
23+
Short: "Export x509 certificates.",
24+
DisableFlagsInUseLine: true,
25+
Use: `x509Certs <PATH> [flags]
26+
27+
Arguments:
28+
PATH location of the source database`,
29+
30+
Aliases: []string{"x509certs"},
31+
Example: ` step-badger x509certs ./db
32+
step-badger x509Certs ./db --revoked --valid=false --emit=openssl`,
2533

2634
Args: cobra.ExactArgs(1),
2735

@@ -44,21 +52,24 @@ func init() {
4452

4553
// Records selection criteria.
4654
x509certsCmd.Flags().BoolVarP(&config.showValid, "valid", "v", true, "valid certificates shown")
47-
x509certsCmd.Flags().BoolVarP(&config.showRevoked, "revoked", "r", true, "revoked certificates shown")
48-
x509certsCmd.Flags().BoolVarP(&config.showExpired, "expired", "x", false, "expired certificates shown")
55+
x509certsCmd.Flags().BoolVarP(&config.showRevoked, "revoked", "r", false, "revoked certificates shown")
56+
x509certsCmd.Flags().BoolVarP(&config.showExpired, "expired", "e", false, "expired certificates shown")
4957

5058
// Format choice
51-
x509certsCmd.Flags().VarP(config.emitX509Format, "emit", "e", "emit format: table|json|markdown|openssl")
52-
x509certsCmd.Flags().VarP(config.timeFormat, "time", "t", "time format: iso|short")
53-
x509certsCmd.Flags().VarP(config.sortOrder, "sort", "s", "sort order: start|finish")
59+
x509certsCmd.Flags().Var(config.emitX509Format, "emit", "emit format: "+FORMAT_TABLE+"|"+FORMAT_JSON+"|"+FORMAT_MARKDOWN+
60+
"|"+FORMAT_OPENSSL+"|"+FORMAT_PLAIN)
61+
x509certsCmd.Flags().Var(config.timeFormat, "time", "time format: "+TIME_ISO+"|"+TIME_SHORT)
62+
x509certsCmd.Flags().Var(config.sortOrder, "sort", "sort order: "+SORT_START+"|"+SORT_FINISH)
5463

5564
// Columns selection criteria.
56-
x509certsCmd.Flags().BoolVarP(&config.showDNSNames, "dnsnames", "", false, "dns names column shown")
57-
x509certsCmd.Flags().BoolVarP(&config.showEmailAddresses, "emailaddresses", "", false, "email addresses column shown")
58-
x509certsCmd.Flags().BoolVarP(&config.showIPAddresses, "ipaddresses", "", false, "ip addresses column shown")
59-
x509certsCmd.Flags().BoolVarP(&config.showURIs, "uris", "", false, "uris column shown")
60-
x509certsCmd.Flags().BoolVarP(&config.showCrl, "crl", "", false, "crl column shown")
61-
x509certsCmd.Flags().BoolVarP(&config.showProvisioner, "provisioner", "", false, "provisioner column shown")
65+
x509certsCmd.Flags().BoolVar(&config.showSerial, "serial", true, "serial number column shown")
66+
x509certsCmd.Flags().BoolVar(&config.showDNSNames, "dnsnames", false, "dns names column shown")
67+
x509certsCmd.Flags().BoolVar(&config.showEmailAddresses, "emailaddresses", false, "email addresses column shown")
68+
x509certsCmd.Flags().BoolVar(&config.showIPAddresses, "ipaddresses", false, "ip addresses column shown")
69+
x509certsCmd.Flags().BoolVar(&config.showURIs, "uris", false, "uris column shown")
70+
x509certsCmd.Flags().BoolVar(&config.showIssuer, "issuer", false, "issuer column shown")
71+
x509certsCmd.Flags().BoolVar(&config.showCrl, "crl", false, "crl column shown")
72+
x509certsCmd.Flags().BoolVar(&config.showProvisioner, "provisioner", false, "provisioner column shown")
6273
}
6374

6475
/*
@@ -154,12 +165,12 @@ func exportX509Main(args []string) {
154165

155166
// Sort.
156167
switch thisSort := config.sortOrder.Value; thisSort {
157-
case "f":
168+
case SORT_FINISH:
158169
sort.SliceStable(x509CertificatesProvisionersRevocations, func(i, j int) bool {
159170
return x509CertificatesProvisionersRevocations[i].X509Certificate.NotAfter.
160171
Before(x509CertificatesProvisionersRevocations[j].X509Certificate.NotAfter)
161172
})
162-
case "s":
173+
case SORT_START:
163174
sort.SliceStable(x509CertificatesProvisionersRevocations, func(i, j int) bool {
164175
return x509CertificatesProvisionersRevocations[i].X509Certificate.NotBefore.
165176
Before(x509CertificatesProvisionersRevocations[j].X509Certificate.NotBefore)
@@ -168,14 +179,16 @@ func exportX509Main(args []string) {
168179

169180
// Output.
170181
switch format := config.emitX509Format.Value; format {
171-
case "j":
182+
case FORMAT_JSON:
172183
emitX509CertsWithRevocationsJson(x509CertificatesProvisionersRevocations)
173-
case "t":
184+
case FORMAT_TABLE:
174185
emitX509Table(x509CertificatesProvisionersRevocations)
175-
case "m":
186+
case FORMAT_MARKDOWN:
176187
emitX509Markdown(x509CertificatesProvisionersRevocations)
177-
case "o":
178-
emitOpenSsl(x509CertificatesProvisionersRevocations)
188+
case FORMAT_OPENSSL:
189+
emitX509OpenSsl(x509CertificatesProvisionersRevocations)
190+
case FORMAT_PLAIN:
191+
emitX509Plain(x509CertificatesProvisionersRevocations)
179192
}
180193
}
181194

0 commit comments

Comments
 (0)