Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ func init() {
cmd := man.Docs.GetCommand("auth",
man.WithSubcommands(clientCredentialsCmd),
)
rootCmd.AddCommand(&cmd.Command)
RootCmd.AddCommand(&cmd.Command)
}
5 changes: 2 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ func config_updateOutput(cmd *cobra.Command, args []string) {
flagHelper := cli.NewFlagHelper(cmd)
format := flagHelper.GetRequiredString("format")

config.UpdateOutputFormat(format)
config.UpdateOutputFormat(cfgKey, format)
fmt.Println(cli.SuccessMessage(fmt.Sprintf("Output format updated to %s", format)))
}

func init() {

outputCmd := man.Docs.GetCommand("config/output",
man.WithRun(config_updateOutput),
)
Expand All @@ -34,5 +33,5 @@ func init() {
cmd := man.Docs.GetCommand("config",
man.WithSubcommands(outputCmd),
)
rootCmd.AddCommand(&cmd.Command)
RootCmd.AddCommand(&cmd.Command)
}
4 changes: 2 additions & 2 deletions cmd/dev-selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func dev_selectorsGen(cmd *cobra.Command, args []string) {

rows := [][]string{}
for _, r := range result {
rows = append(rows, []string{r.ExternalField, r.ExternalValue})
rows = append(rows, []string{r.ExternalSelectorValue, r.ExternalValue})
}

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

rows := [][]string{}
for _, r := range result {
rows = append(rows, []string{r.ExternalField, r.ExternalValue})
rows = append(rows, []string{r.ExternalSelectorValue, r.ExternalValue})
}

t := cli.NewTabular().Rows(rows...)
Expand Down
2 changes: 1 addition & 1 deletion cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@ func init() {
man.WithRun(dev_designSystem),
)
devCmd.AddCommand(&designCmd.Command)
rootCmd.AddCommand(&devCmd.Command)
RootCmd.AddCommand(&devCmd.Command)
}
2 changes: 1 addition & 1 deletion cmd/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ func init() {
tui.StartTea()
}),
)
rootCmd.AddCommand(&cmd.Command)
RootCmd.AddCommand(&cmd.Command)
}
2 changes: 1 addition & 1 deletion cmd/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func init() {
doc.GetDocFlag("json").DefaultAsBool(),
doc.GetDocFlag("json").Description,
)
rootCmd.AddCommand(policyCmd)
RootCmd.AddCommand(policyCmd)
}
37 changes: 19 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,49 @@ import (
)

var (
cfgFile string
cfgKey string
OtdfctlCfg config.Config

configFlagOverrides = config.ConfigFlagOverrides{}
)

// rootCmd represents the base command when called without any subcommands
// RootCmd represents the base command when called without any subcommands.
var (
rootCmd = &man.Docs.GetDoc("<root>").Command
RootCmd = &man.Docs.GetDoc("<root>").Command
)

func init() {
doc := man.Docs.GetDoc("<root>")
rootCmd = &doc.Command
rootCmd.PersistentFlags().String(
RootCmd = &doc.Command
RootCmd.PersistentFlags().String(
doc.GetDocFlag("host").Name,
doc.GetDocFlag("host").Default,
doc.GetDocFlag("host").Description,
)
rootCmd.PersistentFlags().StringVar(
&cfgFile,
doc.GetDocFlag("config-file").Name,
doc.GetDocFlag("config-file").Default,
doc.GetDocFlag("config-file").Description,
)
rootCmd.PersistentFlags().String(
RootCmd.PersistentFlags().String(
doc.GetDocFlag("log-level").Name,
doc.GetDocFlag("log-level").Default,
doc.GetDocFlag("log-level").Description,
)
}

// Execute adds all child commands to the root command and sets flags appropriately.
// The config file and key are defaulted to otdfctl.yaml.
func Execute() {
ExecuteWithBootstrap("", "")
}

cfg, err := config.LoadConfig("otdfctl")
// Execute adds all child commands to the root command and sets flags appropriately.
// It also allows the config file & key to be bootstrapped for wrapping the CLI.
func ExecuteWithBootstrap(configFile, configKey string) {
cfgKey = configKey
cfg, err := config.LoadConfig(configFile, configKey)
if err != nil {
fmt.Println("Error loading config:", err)
os.Exit(1)
}
OtdfctlCfg = *cfg
}

// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
err := rootCmd.Execute()
err = RootCmd.Execute()
if err != nil {
os.Exit(1)
}
Expand Down
3 changes: 0 additions & 3 deletions docs/man/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ command:
- name: host
description: host:port of the Virtru Data Security Platform gRPC server
default: localhost:8080
- name: config-file
description: config file (default is $HOME/.otdfctl.yaml)
default: ''
- name: log-level
description: log level (debug, info, warn, error, fatal, panic)
default: info
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/itchyny/gojq v0.12.15
github.com/muesli/reflow v0.3.0
github.com/opentdf/platform/protocol/go v0.0.0-20240328192545-ab689ebe9123
github.com/opentdf/platform/protocol/go v0.0.0-20240419180709-f27ab98e49a2
github.com/opentdf/platform/sdk v0.0.0-20240328192545-ab689ebe9123
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2sz
github.com/opentdf/platform/protocol/go v0.0.0-20240328192545-ab689ebe9123 h1:otRtkhiBu075GliWAxX12QpiK4dlJdeOtSjomfJ25h0=
github.com/opentdf/platform/protocol/go v0.0.0-20240328192545-ab689ebe9123/go.mod h1:QcLUArzpnfaLehOin8EBM77dCyyUwlRg/kH6uhy+HVE=
github.com/opentdf/platform/protocol/go v0.0.0-20240403220730-0dc1115f9822/go.mod h1:QcLUArzpnfaLehOin8EBM77dCyyUwlRg/kH6uhy+HVE=
github.com/opentdf/platform/protocol/go v0.0.0-20240419180709-f27ab98e49a2 h1:bZyQ2PdXlZiq/eEx/Iw9utIrrb75XrqEbUYbcO5YxNU=
github.com/opentdf/platform/protocol/go v0.0.0-20240419180709-f27ab98e49a2/go.mod h1:qOBx0d9F2dGeTc703tp+HCGhW6nLYXKZ+vmZ2H9xcPI=
github.com/opentdf/platform/sdk v0.0.0-20240328192545-ab689ebe9123 h1:flVFbXMjPRZ8t9GRxoGAva14qEMia1DOQbkJvb3ImXs=
github.com/opentdf/platform/sdk v0.0.0-20240328192545-ab689ebe9123/go.mod h1:+HlyE1QyT7HsW5UlDGnBZVm0YdEfdv7g+K6pWDV8OAg=
github.com/opentdf/platform/sdk v0.0.0-20240403220730-0dc1115f9822/go.mod h1:qRjuZshMizjv7u0K7smgdfSuKz4x7XZb7sJqSdzop+c=
Expand Down
25 changes: 18 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ const (
)

// Load config with viper.
func LoadConfig(key string) (*Config, error) {
if key == "" {
func LoadConfig(file string, key string) (*Config, error) {
// default the config values if not passed in
if file == "" && key == "" {
key = "otdfctl"
slog.Debug("LoadConfig: key not provided, using default", "config file", key)
slog.Debug("LoadConfig: file and key not provided, using default file", "config file", file)
} else {
slog.Debug("LoadConfig", "config file", key)
slog.Debug("LoadConfig", "config file", file, "config key", key)
}

config := &Config{}
Expand All @@ -61,6 +62,12 @@ func LoadConfig(key string) (*Config, error) {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()

// Allow for a custom config file to be passed in
// This takes precedence over the AddConfigPath/SetConfigName
if file != "" {
viper.SetConfigFile(file)
}

if err := viper.ReadInConfig(); err != nil {
return nil, errors.Join(err, ErrLoadingConfig)
}
Expand All @@ -77,13 +84,17 @@ func LoadConfig(key string) (*Config, error) {
return config, nil
}

func UpdateOutputFormat(format string) {
func UpdateOutputFormat(cfgKey, format string) {
v := viper.GetViper()
format = strings.ToLower(format)
formatter := "output.format"
if cfgKey != "" {
formatter = cfgKey + "." + formatter
}
if format == OutputJSON {
v.Set("output.format", OutputJSON)
v.Set(formatter, OutputJSON)
} else {
v.Set("output.format", OutputStyled)
v.Set(formatter, OutputStyled)
}
viper.WriteConfig()
}
4 changes: 2 additions & 2 deletions pkg/handlers/selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func ProcessSubjectContext(subject interface{}, currSelector string, result []*p
reflect.Float64,
reflect.Complex64,
reflect.Complex128:
result = append(result, &policy.SubjectProperty{ExternalField: currSelector + "'", ExternalValue: fmt.Sprintf("%v", subject)})
result = append(result, &policy.SubjectProperty{ExternalSelectorValue: currSelector + "'", ExternalValue: fmt.Sprintf("%v", subject)})

default:
return nil, fmt.Errorf("unsupported type %v", currType.Kind())
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestSubjectContext(subject interface{}, selectors []string) ([]*policy.Subj
// ignore error: we don't have a match but that is not an error state in this case
} else {
if v != nil {
found = append(found, &policy.SubjectProperty{ExternalField: "'" + s + "'", ExternalValue: fmt.Sprintf("%v", v)})
found = append(found, &policy.SubjectProperty{ExternalSelectorValue: "'" + s + "'", ExternalValue: fmt.Sprintf("%v", v)})
}
}
}
Expand Down