Skip to content

Commit

Permalink
chore(rhoc): add some config inspection commands
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Nov 29, 2022
1 parent fc78113 commit 0e8de01
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
9 changes: 6 additions & 3 deletions rhoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/openshift-online/ocm-cli v0.1.60
github.com/openshift-online/ocm-sdk-go v0.1.287
github.com/pkg/errors v0.9.1
github.com/redhat-developer/app-services-cli v0.51.3
github.com/redhat-developer/app-services-cli v0.51.7
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.8.1
Expand All @@ -29,7 +29,10 @@ require (
github.com/jeremywohl/flatten/v2 v2.0.0-20211013061545-07e4a09fb8e4
)

require github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.9.0 // indirect
)

require (
github.com/BurntSushi/toml v1.2.0 // indirect
Expand Down Expand Up @@ -108,7 +111,7 @@ require (
github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.8.0 // indirect
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.9.0 // indirect
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.13.0 // indirect
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.1 // indirect
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.2 // indirect
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.10.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/segmentio/ksuid v1.0.3 // indirect
Expand Down
10 changes: 6 additions & 4 deletions rhoc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,8 @@ github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/redhat-developer/app-services-cli v0.51.3 h1:Lcyp8XCQou7y+ehH430sPYM0iHMEkgJDocl0+2dAT5g=
github.com/redhat-developer/app-services-cli v0.51.3/go.mod h1:ymaEMnece1pgH8dW5iWtcaXvjcFSOogT6lTjJbWNRbs=
github.com/redhat-developer/app-services-cli v0.51.7 h1:iWorpI9P8l7NpjRrY4nno/sPO6M48hNUsFKV0zwmCuk=
github.com/redhat-developer/app-services-cli v0.51.7/go.mod h1:gDm1pe3MnII8woC8k0Zmy5Wb/TSLcTLVZELTrhp86PM=
github.com/redhat-developer/app-services-sdk-go v0.10.0/go.mod h1:enn8Zz6IT0HZYzS6LSttiME2apwnvfVWZnGRS81A4rk=
github.com/redhat-developer/app-services-sdk-go v0.11.0 h1:4r5YKDW2LN2oa51PXrYidByiWfTXB9Z+1YqjtnPaBZ0=
github.com/redhat-developer/app-services-sdk-go v0.11.0/go.mod h1:XAuy1xpY6OxnZ2VhJFTM0HM0WMJ0R31AFRFDvYcvkTE=
Expand All @@ -779,10 +779,12 @@ github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.9.0 h1:AEjtq7k3
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.9.0/go.mod h1:yazwUm4IHuIWrQ0CCsqN0h7rHZx51nlFbYWKnUn7B84=
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.13.0 h1:aSuONBf3znnotUX7ywLh6xvOVVFsPRIUCfsEBWTWEM0=
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.13.0/go.mod h1:ILvcakLEXMLZyRdO//WJZNk9fdFbnU+cM3XrBvubE64=
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.1 h1:S2nGyknlqS1vibgI8ya5KWch/bbxm5QkwWasVSrQhz0=
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.1/go.mod h1:HkNzOWHTW/SomobQ4343+yR4oTmiyvm85BIWlsh0qbA=
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.2 h1:U2je87d/DIeOaQIycg2Y7TLiESmGu0/0rQC5n64Od0Y=
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.2/go.mod h1:HkNzOWHTW/SomobQ4343+yR4oTmiyvm85BIWlsh0qbA=
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.10.0 h1:9kqTCGoz5jR6VE+cgz8fl5Zt9Ctctg9GN1XlkzheVQw=
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.10.0/go.mod h1:UoxuqkUN+g5Ni8zgsCA7zidR5s774m9fqhZ5o4eOSIM=
github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.9.0 h1:kMvH66RXnxrF7FKraWu7n1BnaWrCUchw2unYa9rl/IM=
github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.9.0/go.mod h1:kpEKXWqyD6GUiQjBHCGzp/LIbCBfkTWpPo4VqkQ9zq4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down
2 changes: 2 additions & 0 deletions rhoc/pkg/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/cmd/config/context"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/cmd/config/reset"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/cmd/config/show"
"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/cmd/config/token"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
Expand All @@ -24,6 +25,7 @@ func NewConfigCommand(f *factory.Factory) *cobra.Command {
}

cmd.AddCommand(show.NewConfigShowCommand(f))
cmd.AddCommand(reset.NewConfigResetCommand(f))
cmd.AddCommand(token.NewConfigTokenCommand(f))
cmd.AddCommand(context.NewContextCommand(f))

Expand Down
27 changes: 27 additions & 0 deletions rhoc/pkg/cmd/config/reset/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package reset

import (
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
)

func NewConfigResetCommand(f *factory.Factory) *cobra.Command {

cmd := &cobra.Command{
Use: "reset",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := f.Config.Remove(); err != nil {
return err
}

if err := f.ServiceContext.Remove(); err != nil {
return err
}

return nil
},
}

return cmd
}
13 changes: 13 additions & 0 deletions rhoc/pkg/cmd/config/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type keyVal struct {
}

func NewConfigShowCommand(f *factory.Factory) *cobra.Command {

cmd := &cobra.Command{
Use: "show",
Args: cobra.NoArgs,
Expand Down Expand Up @@ -40,7 +41,19 @@ func NewConfigShowCommand(f *factory.Factory) *cobra.Command {
},
}

configLocation, err := f.Config.Location()
if err != nil {
return err
}

contextLocation, err := f.ServiceContext.Location()
if err != nil {
return err
}

dumper.DumpTable(config, f.IOStreams.Out, []keyVal{
{key: "Config Path", val: configLocation},
{key: "Context Path", val: contextLocation},
{key: "API URL", val: c.APIUrl},
{key: "Auth URL", val: c.AuthURL},
})
Expand Down
29 changes: 25 additions & 4 deletions rhoc/pkg/cmd/whoami/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package whoami

import (
"fmt"
"strings"

"github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/service"
"github.com/redhat-developer/app-services-cli/pkg/core/auth/token"
Expand All @@ -21,13 +22,33 @@ func NewWhoAmICommand(f *factory.Factory) *cobra.Command {
return err
}

userName, ok := token.GetUsername(a.GetConfig().AccessToken)
if a.GetConfig().AccessToken == "" {
return nil
}

accessTkn, err := token.Parse(a.GetConfig().AccessToken)
if err != nil {
return err
}

tknClaims, _ := token.MapClaims(accessTkn)

realms, ok := tknClaims["realm_access"].(map[string]interface{})
if !ok {
userName = "unknown"
return nil
}
roles, ok := realms["roles"].([]interface{})
if !ok {
return nil
}

_, _ = fmt.Fprintln(f.IOStreams.Out, "roles:")
for i := range roles {
role := roles[i].(string)

if ok {
_, _ = fmt.Fprintf(f.IOStreams.Out, "%s@%s\n", userName, a.GetConfig().ApiURL.String())
if strings.HasPrefix(role, "cos-fleet-manager-") {
_, _ = fmt.Fprintf(f.IOStreams.Out, " %s\n", role)
}
}

return nil
Expand Down

0 comments on commit 0e8de01

Please sign in to comment.