Skip to content

Commit 564dc68

Browse files
authored
feat(core): make root command exportable and add config bootstrapping (#120)
- Allow root command to be exported for ease of consumption in wrapper CLIs and processes - Add new `ExecuteWithBootstrap` command for injection of a separate config - Remove `config-file` CLI flag suffering from race condition between Viper & Cobra loading first in our "update format" flow
1 parent 382dbaa commit 564dc68

File tree

12 files changed

+50
-40
lines changed

12 files changed

+50
-40
lines changed

cmd/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ func init() {
8686
cmd := man.Docs.GetCommand("auth",
8787
man.WithSubcommands(clientCredentialsCmd),
8888
)
89-
rootCmd.AddCommand(&cmd.Command)
89+
RootCmd.AddCommand(&cmd.Command)
9090
}

cmd/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ func config_updateOutput(cmd *cobra.Command, args []string) {
1616
flagHelper := cli.NewFlagHelper(cmd)
1717
format := flagHelper.GetRequiredString("format")
1818

19-
config.UpdateOutputFormat(format)
19+
config.UpdateOutputFormat(cfgKey, format)
2020
fmt.Println(cli.SuccessMessage(fmt.Sprintf("Output format updated to %s", format)))
2121
}
2222

2323
func init() {
24-
2524
outputCmd := man.Docs.GetCommand("config/output",
2625
man.WithRun(config_updateOutput),
2726
)
@@ -34,5 +33,5 @@ func init() {
3433
cmd := man.Docs.GetCommand("config",
3534
man.WithSubcommands(outputCmd),
3635
)
37-
rootCmd.AddCommand(&cmd.Command)
36+
RootCmd.AddCommand(&cmd.Command)
3837
}

cmd/dev-selectors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func dev_selectorsGen(cmd *cobra.Command, args []string) {
5454

5555
rows := [][]string{}
5656
for _, r := range result {
57-
rows = append(rows, []string{r.ExternalField, r.ExternalValue})
57+
rows = append(rows, []string{r.ExternalSelectorValue, r.ExternalValue})
5858
}
5959

6060
t := cli.NewTabular().Rows(rows...)
@@ -97,7 +97,7 @@ func dev_selectorsTest(cmd *cobra.Command, args []string) {
9797

9898
rows := [][]string{}
9999
for _, r := range result {
100-
rows = append(rows, []string{r.ExternalField, r.ExternalValue})
100+
rows = append(rows, []string{r.ExternalSelectorValue, r.ExternalValue})
101101
}
102102

103103
t := cli.NewTabular().Rows(rows...)

cmd/dev.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,5 @@ func init() {
119119
man.WithRun(dev_designSystem),
120120
)
121121
devCmd.AddCommand(&designCmd.Command)
122-
rootCmd.AddCommand(&devCmd.Command)
122+
RootCmd.AddCommand(&devCmd.Command)
123123
}

cmd/interactive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ func init() {
1212
tui.StartTea()
1313
}),
1414
)
15-
rootCmd.AddCommand(&cmd.Command)
15+
RootCmd.AddCommand(&cmd.Command)
1616
}

cmd/policy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ func init() {
2020
doc.GetDocFlag("json").DefaultAsBool(),
2121
doc.GetDocFlag("json").Description,
2222
)
23-
rootCmd.AddCommand(policyCmd)
23+
RootCmd.AddCommand(policyCmd)
2424
}

cmd/root.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,49 @@ import (
1212
)
1313

1414
var (
15-
cfgFile string
15+
cfgKey string
1616
OtdfctlCfg config.Config
1717

1818
configFlagOverrides = config.ConfigFlagOverrides{}
1919
)
2020

21-
// rootCmd represents the base command when called without any subcommands
21+
// RootCmd represents the base command when called without any subcommands.
2222
var (
23-
rootCmd = &man.Docs.GetDoc("<root>").Command
23+
RootCmd = &man.Docs.GetDoc("<root>").Command
2424
)
2525

2626
func init() {
2727
doc := man.Docs.GetDoc("<root>")
28-
rootCmd = &doc.Command
29-
rootCmd.PersistentFlags().String(
28+
RootCmd = &doc.Command
29+
RootCmd.PersistentFlags().String(
3030
doc.GetDocFlag("host").Name,
3131
doc.GetDocFlag("host").Default,
3232
doc.GetDocFlag("host").Description,
3333
)
34-
rootCmd.PersistentFlags().StringVar(
35-
&cfgFile,
36-
doc.GetDocFlag("config-file").Name,
37-
doc.GetDocFlag("config-file").Default,
38-
doc.GetDocFlag("config-file").Description,
39-
)
40-
rootCmd.PersistentFlags().String(
34+
RootCmd.PersistentFlags().String(
4135
doc.GetDocFlag("log-level").Name,
4236
doc.GetDocFlag("log-level").Default,
4337
doc.GetDocFlag("log-level").Description,
4438
)
39+
}
40+
41+
// Execute adds all child commands to the root command and sets flags appropriately.
42+
// The config file and key are defaulted to otdfctl.yaml.
43+
func Execute() {
44+
ExecuteWithBootstrap("", "")
45+
}
4546

46-
cfg, err := config.LoadConfig("otdfctl")
47+
// Execute adds all child commands to the root command and sets flags appropriately.
48+
// It also allows the config file & key to be bootstrapped for wrapping the CLI.
49+
func ExecuteWithBootstrap(configFile, configKey string) {
50+
cfgKey = configKey
51+
cfg, err := config.LoadConfig(configFile, configKey)
4752
if err != nil {
4853
fmt.Println("Error loading config:", err)
4954
os.Exit(1)
5055
}
5156
OtdfctlCfg = *cfg
52-
}
53-
54-
// Execute adds all child commands to the root command and sets flags appropriately.
55-
func Execute() {
56-
err := rootCmd.Execute()
57+
err = RootCmd.Execute()
5758
if err != nil {
5859
os.Exit(1)
5960
}

docs/man/_index.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ command:
88
- name: host
99
description: host:port of the Virtru Data Security Platform gRPC server
1010
default: localhost:8080
11-
- name: config-file
12-
description: config file (default is $HOME/.otdfctl.yaml)
13-
default: ''
1411
- name: log-level
1512
description: log level (debug, info, warn, error, fatal, panic)
1613
default: info

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/golang-jwt/jwt/v4 v4.5.0
1515
github.com/itchyny/gojq v0.12.15
1616
github.com/muesli/reflow v0.3.0
17-
github.com/opentdf/platform/protocol/go v0.0.0-20240328192545-ab689ebe9123
17+
github.com/opentdf/platform/protocol/go v0.0.0-20240419180709-f27ab98e49a2
1818
github.com/opentdf/platform/sdk v0.0.0-20240328192545-ab689ebe9123
1919
github.com/spf13/cobra v1.8.0
2020
github.com/spf13/viper v1.18.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2sz
177177
github.com/opentdf/platform/protocol/go v0.0.0-20240328192545-ab689ebe9123 h1:otRtkhiBu075GliWAxX12QpiK4dlJdeOtSjomfJ25h0=
178178
github.com/opentdf/platform/protocol/go v0.0.0-20240328192545-ab689ebe9123/go.mod h1:QcLUArzpnfaLehOin8EBM77dCyyUwlRg/kH6uhy+HVE=
179179
github.com/opentdf/platform/protocol/go v0.0.0-20240403220730-0dc1115f9822/go.mod h1:QcLUArzpnfaLehOin8EBM77dCyyUwlRg/kH6uhy+HVE=
180+
github.com/opentdf/platform/protocol/go v0.0.0-20240419180709-f27ab98e49a2 h1:bZyQ2PdXlZiq/eEx/Iw9utIrrb75XrqEbUYbcO5YxNU=
181+
github.com/opentdf/platform/protocol/go v0.0.0-20240419180709-f27ab98e49a2/go.mod h1:qOBx0d9F2dGeTc703tp+HCGhW6nLYXKZ+vmZ2H9xcPI=
180182
github.com/opentdf/platform/sdk v0.0.0-20240328192545-ab689ebe9123 h1:flVFbXMjPRZ8t9GRxoGAva14qEMia1DOQbkJvb3ImXs=
181183
github.com/opentdf/platform/sdk v0.0.0-20240328192545-ab689ebe9123/go.mod h1:+HlyE1QyT7HsW5UlDGnBZVm0YdEfdv7g+K6pWDV8OAg=
182184
github.com/opentdf/platform/sdk v0.0.0-20240403220730-0dc1115f9822/go.mod h1:qRjuZshMizjv7u0K7smgdfSuKz4x7XZb7sJqSdzop+c=

0 commit comments

Comments
 (0)