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

3008 cli implement end to end tests #3221

Merged
merged 36 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3d435ad
test
rakeshsharma14317 Oct 5, 2023
25bc37d
inital commit
rakeshsharma14317 Oct 11, 2023
455791e
nit changes
rakeshsharma14317 Oct 11, 2023
f4383ed
fix test and linter
rakeshsharma14317 Oct 11, 2023
3fdad3b
small changes
rakeshsharma14317 Oct 11, 2023
1613b7b
Added test for org switch
rakeshsharma14317 Oct 11, 2023
22a1d9a
Merge remote-tracking branch 'origin/main' into 3008-cli-implement-en…
rakeshsharma14317 Oct 11, 2023
1248697
changes for output as csv
rakeshsharma14317 Oct 11, 2023
d2b923c
added test for service cmd
rakeshsharma14317 Oct 12, 2023
fff5c22
merge with main branch
rakeshsharma14317 Oct 12, 2023
9aae3d9
Merge remote-tracking branch 'origin/main' into 3008-cli-implement-en…
rakeshsharma14317 Oct 16, 2023
ee52507
linter fix
rakeshsharma14317 Oct 16, 2023
80cad0f
Merge remote-tracking branch 'origin/main' into 3008-cli-implement-en…
rakeshsharma14317 Oct 17, 2023
e5e3101
test sleep
rakeshsharma14317 Oct 17, 2023
5098082
increased sleep time to test
rakeshsharma14317 Oct 17, 2023
7e8f528
tidy
rakeshsharma14317 Oct 17, 2023
868c07a
test
rakeshsharma14317 Oct 17, 2023
22fb5ba
merge with origin
rakeshsharma14317 Oct 17, 2023
85d1724
added tests for user command
rakeshsharma14317 Oct 18, 2023
b5a7e0e
other commands added with helper
rakeshsharma14317 Oct 18, 2023
cd1a2d1
merge with main branch
rakeshsharma14317 Oct 18, 2023
bc61e52
nit
rakeshsharma14317 Oct 18, 2023
f515a2b
removed unwanted comments
rakeshsharma14317 Oct 18, 2023
8cf83d9
few changes
rakeshsharma14317 Oct 19, 2023
2cfc4d9
nit
rakeshsharma14317 Oct 23, 2023
b856d91
updated printer functions
rakeshsharma14317 Oct 23, 2023
6dee94e
nit
rakeshsharma14317 Oct 23, 2023
b5cc38a
merge with main branch
rakeshsharma14317 Oct 31, 2023
8ad16f8
review changes
rakeshsharma14317 Nov 15, 2023
8d0ce56
nit
rakeshsharma14317 Nov 15, 2023
b67af51
PR changes
rakeshsharma14317 Nov 17, 2023
8537d40
merge with main
rakeshsharma14317 Nov 17, 2023
e632d37
nit
rakeshsharma14317 Nov 17, 2023
942f333
ci fix
rakeshsharma14317 Nov 17, 2023
532caf9
resolve conflicts
rakeshsharma14317 Nov 28, 2023
d20f60a
resolved conflict
rakeshsharma14317 Nov 30, 2023
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
12 changes: 6 additions & 6 deletions cli/cmd/admin/admin.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package admin

import (
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/spf13/cobra"
)

// AdminCmd represents the admin command
func AdminCmd(cfg *config.Config) *cobra.Command {
func AdminCmd(ch *cmdutil.Helper) *cobra.Command {
adminCmd := &cobra.Command{
Use: "admin",
Hidden: !cfg.IsDev(),
Hidden: !ch.Config.IsDev(),
Short: "Manage an admin server",
}
adminCmd.AddCommand(PingCmd(cfg))
adminCmd.AddCommand(StartCmd(cfg))
adminCmd.AddCommand(SwitchCmd(cfg))
adminCmd.AddCommand(PingCmd(ch))
adminCmd.AddCommand(StartCmd(ch))
adminCmd.AddCommand(SwitchCmd(ch))
return adminCmd
}
7 changes: 3 additions & 4 deletions cli/cmd/admin/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package admin

import (
"context"
"fmt"

"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/config"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)

func PingCmd(cfg *config.Config) *cobra.Command {
func PingCmd(ch *cmdutil.Helper) *cobra.Command {
var adminURL string

pingCmd := &cobra.Command{
Use: "ping",
Short: "Ping",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ch.Config
// Must set here to avoid flag parser overriding it globally
cfg.AdminURL = adminURL

Expand All @@ -31,7 +30,7 @@ func PingCmd(cfg *config.Config) *cobra.Command {
return err
}

fmt.Printf("Pong: %s\n", pong.Time.AsTime().String())
ch.Printer.Printf("Pong: %s\n", pong.Time.AsTime().String())
return nil
},
}
Expand Down
14 changes: 8 additions & 6 deletions cli/cmd/admin/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/rilldata/rill/admin"
"github.com/rilldata/rill/admin/server"
"github.com/rilldata/rill/admin/worker"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/runtime/pkg/activity"
"github.com/rilldata/rill/runtime/pkg/email"
"github.com/rilldata/rill/runtime/pkg/graceful"
Expand Down Expand Up @@ -75,20 +75,22 @@ type Config struct {
}

// StartCmd starts an admin server. It only allows configuration using environment variables.
func StartCmd(cliCfg *config.Config) *cobra.Command {
func StartCmd(ch *cmdutil.Helper) *cobra.Command {
startCmd := &cobra.Command{
Use: "start [jobs|server|worker]",
Short: "Start admin service",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cliCfg := ch.Config
printer := ch.Printer
// Load .env (note: fails silently if .env has errors)
_ = godotenv.Load()

// Init config
var conf Config
err := envconfig.Process("rill_admin", &conf)
if err != nil {
fmt.Printf("failed to load config: %s\n", err.Error())
printer.Printf("failed to load config: %s\n", err.Error())
os.Exit(1)
}

Expand All @@ -97,7 +99,7 @@ func StartCmd(cliCfg *config.Config) *cobra.Command {
cfg.Level.SetLevel(conf.LogLevel)
logger, err := cfg.Build()
if err != nil {
fmt.Printf("error: failed to create logger: %s\n", err.Error())
printer.Printf("error: failed to create logger: %s\n", err.Error())
os.Exit(1)
}

Expand All @@ -114,12 +116,12 @@ func StartCmd(cliCfg *config.Config) *cobra.Command {
// Validate frontend and external URLs
_, err = url.Parse(conf.FrontendURL)
if err != nil {
fmt.Printf("error: invalid frontend URL: %s\n", err.Error())
printer.Printf("error: invalid frontend URL: %s\n", err.Error())
os.Exit(1)
}
_, err = url.Parse(conf.ExternalURL)
if err != nil {
fmt.Printf("error: invalid external URL: %s\n", err.Error())
printer.Printf("error: invalid external URL: %s\n", err.Error())
os.Exit(1)
}
_, err = url.Parse(conf.ExternalGRPCURL)
Expand Down
7 changes: 4 additions & 3 deletions cli/cmd/admin/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ const (
devAdminURL = "http://localhost:9090"
)

func SwitchCmd(cfg *config.Config) *cobra.Command {
func SwitchCmd(ch *cmdutil.Helper) *cobra.Command {
var env string
switchCmd := &cobra.Command{
Use: "switch {stage|prod|dev}",
Short: "switch",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ch.Config
if len(args) > 0 {
env = args[0]
}
Expand Down Expand Up @@ -61,8 +62,8 @@ func SwitchCmd(cfg *config.Config) *cobra.Command {

cfg.AdminURL = url

cmdutil.PrintlnSuccess(fmt.Sprintf("Set default env to %q, url is %q", env, url))
err = auth.SelectOrgFlow(cmd.Context(), cfg)
ch.Printer.PrintlnSuccess(fmt.Sprintf("Set default env to %q, url is %q", env, url))
err = auth.SelectOrgFlow(cmd.Context(), ch)
if err != nil {
return err
}
Expand Down
31 changes: 15 additions & 16 deletions cli/cmd/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,39 @@ import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/rilldata/rill/cli/pkg/browser"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/deviceauth"
"github.com/rilldata/rill/cli/pkg/dotrill"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)

// LoginCmd is the command for logging into a Rill account.
func LoginCmd(cfg *config.Config) *cobra.Command {
func LoginCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Short: "Authenticate with the Rill API",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ch.Config
ctx := cmd.Context()

// updating this as its not required to logout first and login again
if cfg.AdminTokenDefault != "" {
err := Logout(ctx, cfg)
err := Logout(ctx, ch)
if err != nil {
return err
}
}

// Login user
err := Login(ctx, cfg, "")
err := Login(ctx, ch, "")
if err != nil {
return err
}

// Set default org after login
err = SelectOrgFlow(ctx, cfg)
err = SelectOrgFlow(ctx, ch)
if err != nil {
return err
}
Expand All @@ -50,10 +49,11 @@ func LoginCmd(cfg *config.Config) *cobra.Command {
return cmd
}

func Login(ctx context.Context, cfg *config.Config, redirectURL string) error {
func Login(ctx context.Context, ch *cmdutil.Helper, redirectURL string) error {
// In production, the REST and gRPC endpoints are the same, but in development, they're served on different ports.
// We plan to move to connect.build for gRPC, which will allow us to serve both on the same port in development as well.
// Until we make that change, this is a convenient hack for local development (assumes gRPC on port 9090 and REST on port 8080).
cfg := ch.Config
authURL := cfg.AdminURL
if strings.Contains(authURL, "http://localhost:9090") {
authURL = "http://localhost:8080"
Expand All @@ -69,12 +69,10 @@ func Login(ctx context.Context, cfg *config.Config, redirectURL string) error {
return err
}

bold := color.New(color.Bold)
bold.Printf("\nConfirmation Code: ")
boldGreen := color.New(color.FgGreen).Add(color.Bold)
boldGreen.Fprintln(color.Output, deviceVerification.UserCode)
ch.Printer.PrintBold("\nConfirmation Code: ")
ch.Printer.PrintlnSuccess(deviceVerification.UserCode)

bold.Printf("\nOpen this URL in your browser to confirm the login: %s\n\n", deviceVerification.VerificationCompleteURL)
ch.Printer.PrintBold(fmt.Sprintf("\nOpen this URL in your browser to confirm the login: %s\n\n", deviceVerification.VerificationCompleteURL))

_ = browser.Open(deviceVerification.VerificationCompleteURL)

Expand All @@ -89,11 +87,12 @@ func Login(ctx context.Context, cfg *config.Config, redirectURL string) error {
}
// set the default token to the one we just got
cfg.AdminTokenDefault = res1.AccessToken
bold.Print("Successfully logged in. Welcome to Rill!\n")
ch.Printer.PrintBold("Successfully logged in. Welcome to Rill!\n")
return nil
}

func SelectOrgFlow(ctx context.Context, cfg *config.Config) error {
func SelectOrgFlow(ctx context.Context, ch *cmdutil.Helper) error {
cfg := ch.Config
client, err := cmdutil.Client(cfg)
if err != nil {
return err
Expand All @@ -106,7 +105,7 @@ func SelectOrgFlow(ctx context.Context, cfg *config.Config) error {
}

if len(res.Organizations) == 0 {
cmdutil.PrintlnWarn("You are not part of an org. Run `rill org create` or `rill deploy` to create one.")
ch.Printer.PrintlnWarn("You are not part of an org. Run `rill org create` or `rill deploy` to create one.")
return nil
}

Expand All @@ -127,6 +126,6 @@ func SelectOrgFlow(ctx context.Context, cfg *config.Config) error {

cfg.Org = defaultOrg

fmt.Printf("Set default organization to %q. Change using `rill org switch`.\n", defaultOrg)
ch.Printer.Print(fmt.Sprintf("Set default organization to %q. Change using `rill org switch`.\n", defaultOrg))
return nil
}
17 changes: 8 additions & 9 deletions cli/cmd/auth/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@ package auth

import (
"context"
"fmt"

"github.com/fatih/color"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/dotrill"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)

// LogoutCmd is the command for logging out of a Rill account.
func LogoutCmd(cfg *config.Config) *cobra.Command {
func LogoutCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "logout",
Short: "Logout of the Rill API",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ch.Config
ctx := cmd.Context()

token := cfg.AdminToken()
if token == "" {
cmdutil.PrintlnWarn("You are already logged out.")
ch.Printer.PrintlnWarn("You are already logged out.")
return nil
}

err := Logout(ctx, cfg)
err := Logout(ctx, ch)
if err != nil {
return err
}

color.New(color.FgGreen).Println("Successfully logged out.")
ch.Printer.PrintlnSuccess("Successfully logged out.")
return nil
},
}
return cmd
}

func Logout(ctx context.Context, cfg *config.Config) error {
func Logout(ctx context.Context, ch *cmdutil.Helper) error {
cfg := ch.Config
client, err := cmdutil.Client(cfg)
if err != nil {
return err
Expand All @@ -47,7 +46,7 @@ func Logout(ctx context.Context, cfg *config.Config) error {

_, err = client.RevokeCurrentAuthToken(ctx, &adminv1.RevokeCurrentAuthTokenRequest{})
if err != nil {
fmt.Printf("Failed to revoke token (did you revoke it manually?). Clearing local token anyway.\n")
ch.Printer.Printf("Failed to revoke token (did you revoke it manually?). Clearing local token anyway.\n")
}

err = dotrill.SetAccessToken("")
Expand Down
Loading