Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor to support varying print output #350

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
jakedoublev marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
23 changes: 10 additions & 13 deletions cmd/auth-clientCredentials.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"fmt"

"github.com/opentdf/otdfctl/pkg/auth"
"github.com/opentdf/otdfctl/pkg/cli"
"github.com/opentdf/otdfctl/pkg/man"
Expand All @@ -16,9 +14,8 @@ var clientCredentialsCmd = man.Docs.GetCommand("auth/client-credentials",
)

func auth_clientCredentials(cmd *cobra.Command, args []string) {
cp := InitProfile(cmd, false)

p := cli.NewPrinter(true)
c := cli.New(cmd, args)
cp := InitProfile(c, false)

var clientId string
var clientSecret string
Expand All @@ -45,20 +42,20 @@ func auth_clientCredentials(cmd *cobra.Command, args []string) {
})

// Validate the client credentials
p.Printf("Validating client credentials for %s... ", cp.GetEndpoint())
c.Printf("Validating client credentials for %s... ", cp.GetEndpoint())
if err := auth.ValidateProfileAuthCredentials(cmd.Context(), cp); err != nil {
fmt.Println("failed")
cli.ExitWithError("An error occurred during login. Please check your credentials and try again", err)
c.Println("failed")
c.ExitWithError("An error occurred during login. Please check your credentials and try again", err)
}
p.Println("ok")
c.Println("ok")

// Save the client credentials
p.Print("Storing client ID and secret in keyring... ")
c.Print("Storing client ID and secret in keyring... ")
if err := cp.Save(); err != nil {
p.Println("failed")
cli.ExitWithError("An error occurred while storing client credentials", err)
c.Println("failed")
c.ExitWithError("An error occurred while storing client credentials", err)
}
p.Println("ok")
c.Println("ok")
}

func init() {
Expand Down
34 changes: 18 additions & 16 deletions cmd/auth-login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ import (
)

func auth_codeLogin(cmd *cobra.Command, args []string) {
fh := cli.NewFlagHelper(cmd)
clientID := fh.GetOptionalString("client-id")
tlsNoVerify := fh.GetOptionalBool("tls-no-verify")

cp := InitProfile(cmd, false)
printer := cli.NewPrinter(true)

printer.Println("Initiating login...")
tok, publicClientID, err := auth.LoginWithPKCE(cmd.Context(), cp.GetEndpoint(), clientID, tlsNoVerify)
c := cli.New(cmd, args)
cp := InitProfile(c, false)

c.Print("Initiating login...")
tok, publicClientID, err := auth.LoginWithPKCE(
cmd.Context(),
cp.GetEndpoint(),
c.FlagHelper.GetOptionalString("client-id"),
c.FlagHelper.GetOptionalBool("tls-no-verify"),
)
if err != nil {
cli.ExitWithError("could not authenticate", err)
c.Println("failed")
c.ExitWithError("could not authenticate", err)
}
printer.Println("ok")
c.Println("ok")

// Set the auth credentials to profile
if err := cp.SetAuthCredentials(profiles.AuthCredentials{
Expand All @@ -33,15 +35,15 @@ func auth_codeLogin(cmd *cobra.Command, args []string) {
RefreshToken: tok.RefreshToken,
},
}); err != nil {
cli.ExitWithError("failed to set auth credentials", err)
c.ExitWithError("failed to set auth credentials", err)
}

printer.Println("Storing credentials to profile in keyring...")
c.Print("Storing credentials to profile in keyring...")
if err := cp.Save(); err != nil {
printer.Println("failed")
cli.ExitWithError("An error occurred while storing authentication credentials", err)
c.Println("failed")
c.ExitWithError("An error occurred while storing authentication credentials", err)
}
printer.Println("ok")
c.Println("ok")
}

var codeLoginCmd *man.Doc
Expand Down
22 changes: 10 additions & 12 deletions cmd/auth-logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,31 @@ import (
)

func auth_logout(cmd *cobra.Command, args []string) {
fh := cli.NewFlagHelper(cmd)
tlsNoVerify := fh.GetOptionalBool("tls-no-verify")
cp := InitProfile(cmd, false)
printer := cli.NewPrinter(true)
printer.Println("Initiating logout...")
c := cli.New(cmd, args)
cp := InitProfile(c, false)
c.Println("Initiating logout...")

// we can only revoke access tokens stored for the code login flow, not client credentials
creds := cp.GetAuthCredentials()
if creds.AuthType == profiles.PROFILE_AUTH_TYPE_ACCESS_TOKEN {
printer.Println("Revoking access token...")
c.Println("Revoking access token...")
if err := auth.RevokeAccessToken(
cmd.Context(),
cp.GetEndpoint(),
creds.AccessToken.PublicClientID,
creds.AccessToken.RefreshToken,
tlsNoVerify,
c.FlagHelper.GetOptionalBool("tls-no-verify"),
); err != nil {
printer.Println("failed")
cli.ExitWithError("An error occurred while revoking the access token", err)
c.Println("failed")
c.ExitWithError("An error occurred while revoking the access token", err)
}
}

if err := cp.SetAuthCredentials(profiles.AuthCredentials{}); err != nil {
printer.Println("failed")
cli.ExitWithError("An error occurred while logging out", err)
c.Println("failed")
c.ExitWithError("An error occurred while logging out", err)
}
printer.Println("ok")
c.Println("ok")
}

var codeLogoutCmd *man.Doc
Expand Down
34 changes: 9 additions & 25 deletions cmd/auth-printAccessToken.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cmd

import (
"encoding/json"
"fmt"

"github.com/opentdf/otdfctl/pkg/auth"
"github.com/opentdf/otdfctl/pkg/cli"
"github.com/opentdf/otdfctl/pkg/man"
Expand All @@ -15,40 +12,27 @@ var auth_printAccessTokenCmd = man.Docs.GetCommand("auth/print-access-token",
man.WithRun(auth_printAccessToken))

func auth_printAccessToken(cmd *cobra.Command, args []string) {
flagHelper := cli.NewFlagHelper(cmd)
jsonOut := flagHelper.GetOptionalBool("json")

cp := InitProfile(cmd, false)

printEnabled := !jsonOut
p := cli.NewPrinter(printEnabled)
c := cli.New(cmd, args)
cp := InitProfile(c, false)

ac := cp.GetAuthCredentials()
switch ac.AuthType {
case profiles.PROFILE_AUTH_TYPE_CLIENT_CREDENTIALS:
p.Printf("Getting access token for %s... ", ac.ClientId)
c.Printf("Getting access token for %s... ", ac.ClientId)
case profiles.PROFILE_AUTH_TYPE_ACCESS_TOKEN:
p.Printf("Getting profile's stored access token... ")
c.Printf("Getting profile's stored access token... ")
default:
cli.ExitWithError("Invalid auth type", nil)
c.ExitWithError("Invalid auth type", nil)
}
tok, err := auth.GetTokenWithProfile(cmd.Context(), cp)
if err != nil {
p.Println("failed")
c.Println("failed")
cli.ExitWithError("Failed to get token", err)
}
p.Println("ok")
p.Printf("Access Token: %s\n", tok.AccessToken)
c.Println("ok")
c.Printf("Access Token: %s\n", tok.AccessToken)

if jsonOut {
d, err := json.MarshalIndent(tok, "", " ")
if err != nil {
cli.ExitWithError("Failed to marshal token to json", err)
}

fmt.Println(string(d))
return
}
c.PrintIfJson(tok)
}

func init() {
Expand Down
6 changes: 3 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

func config_updateOutput(cmd *cobra.Command, args []string) {
h := NewHandler(cmd)
c := cli.New(cmd, args)
h := NewHandler(c)
defer h.Close()

flagHelper := cli.NewFlagHelper(cmd)
format := flagHelper.GetRequiredString("format")
format := c.Flags.GetRequiredString("format")

config.UpdateOutputFormat(cfgKey, format)
fmt.Println(cli.SuccessMessage(fmt.Sprintf("Output format updated to %s", format)))
Expand Down
18 changes: 9 additions & 9 deletions cmd/dev-selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ var (
)

func dev_selectorsGen(cmd *cobra.Command, args []string) {
h := NewHandler(cmd)
c := cli.New(cmd, args)
h := NewHandler(c)
defer h.Close()

flagHelper := cli.NewFlagHelper(cmd)
subject := flagHelper.GetRequiredString("subject")
contextType := flagHelper.GetRequiredString("type")
subject := c.Flags.GetRequiredString("subject")
contextType := c.Flags.GetRequiredString("type")

var value any
if contextType == "json" || contextType == "" {
Expand Down Expand Up @@ -62,13 +62,13 @@ func dev_selectorsGen(cmd *cobra.Command, args []string) {
}

func dev_selectorsTest(cmd *cobra.Command, args []string) {
h := NewHandler(cmd)
c := cli.New(cmd, args)
h := NewHandler(c)
defer h.Close()

flagHelper := cli.NewFlagHelper(cmd)
subject := flagHelper.GetRequiredString("subject")
contextType := flagHelper.GetRequiredString("type")
selectors := flagHelper.GetStringSlice("selectors", selectors, cli.FlagHelperStringSliceOptions{Min: 1})
subject := c.Flags.GetRequiredString("subject")
contextType := c.Flags.GetRequiredString("type")
selectors := c.Flags.GetStringSlice("selectors", selectors, cli.FlagsStringSliceOptions{Min: 1})

var value any
if contextType == "json" || contextType == "" {
Expand Down
7 changes: 2 additions & 5 deletions cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,9 @@ func getMetadataUpdateBehavior() common.MetadataUpdateEnum {

// HandleSuccess prints a success message according to the configured format (styled table or JSON)
func HandleSuccess(command *cobra.Command, id string, t table.Model, policyObject interface{}) {
c := cli.New(command, []string{})
if OtdfctlCfg.Output.Format == config.OutputJSON || configFlagOverrides.OutputFormatJSON {
if output, err := json.MarshalIndent(policyObject, "", " "); err != nil {
cli.ExitWithError("Error marshalling policy object", err)
} else {
fmt.Println(string(output))
}
c.PrintJson(policyObject)
jrschumacher marked this conversation as resolved.
Show resolved Hide resolved
return
}
cli.PrintSuccessTable(command, id, t)
Expand Down
4 changes: 3 additions & 1 deletion cmd/interactive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/opentdf/otdfctl/pkg/cli"
"github.com/opentdf/otdfctl/pkg/man"
"github.com/opentdf/otdfctl/tui"
"github.com/spf13/cobra"
Expand All @@ -9,7 +10,8 @@ import (
func init() {
cmd := man.Docs.GetCommand("interactive",
man.WithRun(func(cmd *cobra.Command, args []string) {
h := NewHandler(cmd)
c := cli.New(cmd, args)
h := NewHandler(c)
tui.StartTea(h)
}),
)
Expand Down
26 changes: 13 additions & 13 deletions cmd/kas-grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
var forceFlagValue = false

func policy_assignKasGrant(cmd *cobra.Command, args []string) {
h := NewHandler(cmd)
c := cli.New(cmd, args)
h := NewHandler(c)
defer h.Close()

flagHelper := cli.NewFlagHelper(cmd)
nsID := flagHelper.GetOptionalString("namespace-id")
attrID := flagHelper.GetOptionalString("attribute-id")
valID := flagHelper.GetOptionalString("value-id")
kasID := flagHelper.GetRequiredString("kas-id")
nsID := c.Flags.GetOptionalString("namespace-id")
attrID := c.Flags.GetOptionalString("attribute-id")
valID := c.Flags.GetOptionalString("value-id")
kasID := c.Flags.GetRequiredString("kas-id")

count := 0
for _, v := range []string{nsID, attrID, valID} {
Expand Down Expand Up @@ -69,15 +69,15 @@ func policy_assignKasGrant(cmd *cobra.Command, args []string) {
}

func policy_unassignKasGrant(cmd *cobra.Command, args []string) {
h := NewHandler(cmd)
c := cli.New(cmd, args)
h := NewHandler(c)
defer h.Close()

flagHelper := cli.NewFlagHelper(cmd)
nsID := flagHelper.GetOptionalString("namespace-id")
attrID := flagHelper.GetOptionalString("attribute-id")
valID := flagHelper.GetOptionalString("value-id")
kasID := flagHelper.GetRequiredString("kas-id")
force := flagHelper.GetOptionalBool("force")
nsID := c.Flags.GetOptionalString("namespace-id")
attrID := c.Flags.GetOptionalString("attribute-id")
valID := c.Flags.GetOptionalString("value-id")
kasID := c.Flags.GetRequiredString("kas-id")
force := c.Flags.GetOptionalBool("force")

count := 0
for _, v := range []string{nsID, attrID, valID} {
Expand Down
Loading
Loading