Skip to content

Commit

Permalink
feat: fctl autocomplete organizations stacks (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored Jun 21, 2023
1 parent ce140a3 commit 230dda6
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 471 deletions.
4 changes: 2 additions & 2 deletions components/fctl/cmd/cloud/me/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func NewInfoCommand() *cobra.Command {
}

tableData := pterm.TableData{}
tableData = append(tableData, []string{pterm.LightCyan("Subject"), userInfo.GetSubject()})
tableData = append(tableData, []string{pterm.LightCyan("Email"), userInfo.GetEmail()})
tableData = append(tableData, []string{pterm.LightCyan("Subject"), userInfo.Subject})
tableData = append(tableData, []string{pterm.LightCyan("Email"), userInfo.Email})

return pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
Expand Down
21 changes: 11 additions & 10 deletions components/fctl/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"fmt"
"net/url"
"time"

fctl "github.com/formancehq/fctl/pkg"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/zitadel/oidc/pkg/client/rp"
"github.com/zitadel/oidc/pkg/oidc"
"github.com/zitadel/oidc/v2/pkg/client/rp"
"github.com/zitadel/oidc/v2/pkg/oidc"
)

type Dialog interface {
Expand All @@ -21,27 +22,27 @@ func (fn DialogFn) DisplayURIAndCode(uri, code string) {
fn(uri, code)
}

func LogIn(ctx context.Context, dialog Dialog, relyingParty rp.RelyingParty) (*oidc.Tokens, error) {
deviceCode, err := rp.GetDeviceCode(ctx, relyingParty)
func LogIn(ctx context.Context, dialog Dialog, relyingParty rp.RelyingParty) (*oidc.AccessTokenResponse, error) {
deviceCode, err := rp.DeviceAuthorization(relyingParty.OAuthConfig().Scopes, relyingParty)
if err != nil {
return nil, err
}

uri, err := url.Parse(deviceCode.GetVerificationUri())
uri, err := url.Parse(deviceCode.VerificationURI)
if err != nil {
panic(err)
}
query := uri.Query()
query.Set("user_code", deviceCode.GetUserCode())
query.Set("user_code", deviceCode.UserCode)
uri.RawQuery = query.Encode()

dialog.DisplayURIAndCode(deviceCode.GetVerificationUri(), deviceCode.GetUserCode())
dialog.DisplayURIAndCode(deviceCode.VerificationURI, deviceCode.UserCode)

if err := fctl.Open(uri.String()); err != nil {
return nil, err
}

return rp.PollDeviceCode(ctx, deviceCode.GetDeviceCode(), deviceCode.GetInterval(), relyingParty)
return rp.DeviceAccessToken(ctx, deviceCode.DeviceCode, time.Duration(deviceCode.Interval)*time.Second, relyingParty)
}

func NewLoginCommand() *cobra.Command {
Expand All @@ -66,7 +67,7 @@ func NewLoginCommand() *cobra.Command {
membershipUri = profile.GetMembershipURI()
}

relyingParty, err := fctl.GetAuthRelyingParty(cmd, membershipUri)
relyingParty, err := fctl.GetAuthRelyingParty(fctl.GetHttpClient(cmd, map[string][]string{}), membershipUri)
if err != nil {
return err
}
Expand All @@ -80,7 +81,7 @@ func NewLoginCommand() *cobra.Command {
}

profile.SetMembershipURI(membershipUri)
profile.UpdateToken(ret.Token)
profile.UpdateToken(ret)

currentProfileName := fctl.GetCurrentProfileName(cmd, cfg)

Expand Down
2 changes: 1 addition & 1 deletion components/fctl/cmd/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (p *prompt) refreshUserEmail(cmd *cobra.Command, cfg *fctl.Config) error {
p.userEmail = ""
return nil
}
p.userEmail = userInfo.GetEmail()
p.userEmail = userInfo.Email
return nil
}

Expand Down
13 changes: 13 additions & 0 deletions components/fctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ func NewRootCommand() *cobra.Command {
fctl.WithPersistentBoolFlag(fctl.TelemetryFlag, false, "Telemetry enabled"),
)
cmd.Version = Version
cmd.RegisterFlagCompletionFunc(fctl.ProfileFlag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return []string{}, cobra.ShellCompDirectiveError
}
ret := make([]string, 0)
for name := range cfg.GetProfiles() {
ret = append(ret, name)
}
return ret, cobra.ShellCompDirectiveDefault
})
return cmd
}

Expand All @@ -78,6 +89,8 @@ func Execute() {
case errors.Is(err, fctl.ErrMissingApproval):
pterm.Error.WithWriter(os.Stderr).Printfln("Command aborted as you didn't approve.")
os.Exit(1)
case fctl.IsInvalidAuthentication(err):
pterm.Error.WithWriter(os.Stderr).Printfln("Your authentication is invalid, please login :)")
case extractOpenAPIErrorMessage(err) != nil:
pterm.Error.WithWriter(os.Stderr).Printfln(extractOpenAPIErrorMessage(err).Error())
os.Exit(2)
Expand Down
24 changes: 13 additions & 11 deletions components/fctl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ require (
github.com/segmentio/analytics-go/v3 v3.2.1
github.com/segmentio/ksuid v1.0.4
github.com/spf13/cobra v1.6.1
github.com/zitadel/oidc v1.13.0
github.com/zitadel/oidc/v2 v2.6.1
golang.org/x/mod v0.10.0
golang.org/x/oauth2 v0.3.0
golang.org/x/oauth2 v0.8.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -29,7 +29,7 @@ require (
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gookit/color v1.5.2 // indirect
github.com/gorilla/schema v1.2.0 // indirect
Expand All @@ -38,33 +38,35 @@ require (
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/lithammer/fuzzysearch v1.1.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-tty v0.0.4 // indirect
github.com/muhlemmer/gu v0.3.1 // indirect
github.com/pkg/term v1.2.0-beta.2 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/segmentio/backo-go v1.0.1 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/sirupsen/logrus v1.9.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/talal/go-bits v0.0.0-20200204154716-071e9f3e66e1 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.29.1 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
)

replace github.com/formancehq/stack/libs/go-libs => ../../libs/go-libs

replace github.com/zitadel/oidc => github.com/formancehq/oidc v0.0.0-20220923202448-e2960a99b71c
replace github.com/zitadel/oidc/v2 v2.6.1 => github.com/formancehq/oidc/v2 v2.6.2-0.20230526075055-93dc5ecb0149

replace github.com/formancehq/fctl/membershipclient => ./membershipclient

Expand Down
Loading

0 comments on commit 230dda6

Please sign in to comment.