Skip to content
Draft
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
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ jobs:
run: echo "$HOME/go/bin" >> $GITHUB_PATH
- name: Run commands generation command
run: make gen
- name: Run proto generation command
run: make proto
- name: Verify no changes were generated
run: |
# Check if there are any uncommitted changes in the working directory
if [ -z "$(git status --porcelain)" ]; then
echo "::notice::Generated files are up to date. No changes detected."
else
echo "::error::Detected uncommitted changes after running the generation command. Please run the generator and commit the changes."
echo "::error::Detected uncommitted changes after running the generation command. Please run `make gen` and `make proto` and commit the changes."
git status
git diff
exit 1
Expand Down
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all install gen build test mocks
.PHONY: all install buf-install gen proto build test mocks

# Load .env file if it exists (for local development)
# In CI/CD, environment variables are provided by the environment
Expand All @@ -13,7 +13,7 @@ VERSION := $(shell git describe --tags --always --dirty 2>/dev/null)
VERSION_PKG := github.com/temporalio/cloud-cli/temporalcloudcli
LDFLAGS := $(if $(VERSION),-X $(VERSION_PKG).Version=$(VERSION))

all: gen build mocks test
all: gen proto build mocks test

# we need to install gen-commands directly because gen-commands does not have its
# own go.mod
Expand All @@ -25,6 +25,15 @@ install:
gen: install
gen-commands -input ./temporalcloudcli/commands.yml -pkg temporalcloudcli > ./temporalcloudcli/commands.gen.go

buf-install:
go install github.com/bufbuild/buf/cmd/buf@v1.50.0

# Regenerates the vendored onboarding service client (proto/ -> internal/).
# The protoc plugins are pinned as go tools in go.mod.
proto: buf-install
buf lint
buf generate

build:
go build -ldflags "$(LDFLAGS)" ./cmd/temporal-cloud

Expand Down
10 changes: 10 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: v2
inputs:
- directory: proto
plugins:
- local: ["go", "tool", "protoc-gen-go"]
out: internal
opt: [paths=source_relative]
- local: ["go", "tool", "protoc-gen-go-grpc"]
out: internal
opt: [paths=source_relative]
12 changes: 12 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: v2
modules:
- path: proto
lint:
use:
- STANDARD
except:
- PACKAGE_DIRECTORY_MATCH
- RPC_REQUEST_RESPONSE_UNIQUE
- RPC_REQUEST_STANDARD_NAME
- RPC_RESPONSE_STANDARD_NAME
- SERVICE_SUFFIX
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
go.temporal.io/sdk/contrib/envconfig v1.0.0
golang.org/x/oauth2 v0.36.0
golang.org/x/term v0.46.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa
google.golang.org/grpc v1.82.1
google.golang.org/protobuf v1.36.11
)
Expand Down Expand Up @@ -79,8 +80,12 @@ require (
golang.org/x/time v0.15.0 // indirect
golang.org/x/tools v0.51.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa // indirect
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

tool github.com/vektra/mockery/v3
tool (
github.com/vektra/mockery/v3
google.golang.org/grpc/cmd/protoc-gen-go-grpc
google.golang.org/protobuf/cmd/protoc-gen-go
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa h1:
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
google.golang.org/grpc v1.82.1 h1:NnAxzGRA0677vCa4BUkOAnO5+FfQqVl9iUXeD0IqcGE=
google.golang.org/grpc v1.82.1/go.mod h1:yzTZ1TB1Z3SG+LIYaI+WiE8D5+PZ3ArnrSp8zF3+/ZA=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 h1:F29+wU6Ee6qgu9TddPgooOdaqsxTMunOoj8KA5yuS5A=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1/go.mod h1:5KF+wpkbTSbGcR9zteSqZV6fqFOWBl4Yde8En8MryZA=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
37 changes: 37 additions & 0 deletions internal/onboarding/rejection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package onboarding

import (
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// Reasons the onboarding service gives, as a google.rpc.ErrorInfo reason on a
// FailedPrecondition, for refusing a CLI signup.
const (
// ReasonAlreadyOnboarded: the user already had an account before this signup.
ReasonAlreadyOnboarded = "ALREADY_ONBOARDED"
// ReasonTrialSignupUnavailable: CLI signup is turned off.
ReasonTrialSignupUnavailable = "TRIAL_SIGNUP_UNAVAILABLE"
// ReasonBrowserSignupInProgress: the user has a signup running in the browser.
ReasonBrowserSignupInProgress = "BROWSER_SIGNUP_IN_PROGRESS"
// ReasonTrialSignupCancelled: the user's signup was cancelled.
ReasonTrialSignupCancelled = "TRIAL_SIGNUP_CANCELLED"
)

const errorInfoDomain = "cloud.temporal.io"

// RejectionReason returns the ErrorInfo reason of a signup refused by the
// onboarding service, or "" when err is not one. err may be wrapped.
func RejectionReason(err error) string {
st, ok := status.FromError(err)
if !ok || st.Code() != codes.FailedPrecondition {
return ""
}
for _, detail := range st.Details() {
if info, ok := detail.(*errdetails.ErrorInfo); ok && info.GetDomain() == errorInfoDomain {
return info.GetReason()
}
}
return ""
}
66 changes: 66 additions & 0 deletions internal/onboarding/rejection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package onboarding_test

import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/temporalio/cloud-cli/internal/onboarding"
)

func rejection(t *testing.T, code codes.Code, domain, reason string) error {
t.Helper()
st, err := status.New(code, "refused").WithDetails(&errdetails.ErrorInfo{Domain: domain, Reason: reason})
require.NoError(t, err)
return st.Err()
}

func TestRejectionReason(t *testing.T) {
tests := []struct {
name string
err error
expected string
}{
{
name: "signup rejection",
err: rejection(t, codes.FailedPrecondition, "cloud.temporal.io", onboarding.ReasonAlreadyOnboarded),
expected: onboarding.ReasonAlreadyOnboarded,
},
{
name: "wrapped signup rejection",
err: fmt.Errorf("failed to get onboarding state: %w",
rejection(t, codes.FailedPrecondition, "cloud.temporal.io", onboarding.ReasonTrialSignupCancelled)),
expected: onboarding.ReasonTrialSignupCancelled,
},
{
name: "other domain",
err: rejection(t, codes.FailedPrecondition, "example.com", onboarding.ReasonAlreadyOnboarded),
},
{
name: "other code",
err: rejection(t, codes.PermissionDenied, "cloud.temporal.io", onboarding.ReasonAlreadyOnboarded),
},
{
name: "no details",
err: status.Error(codes.FailedPrecondition, "refused"),
},
{
name: "not a grpc error",
err: errors.New("boom"),
},
{
name: "nil",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, onboarding.RejectionReason(tt.err))
})
}
}
117 changes: 117 additions & 0 deletions internal/onboarding/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package onboarding

import (
"context"
"errors"
"fmt"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

onboardingservice "github.com/temporalio/cloud-cli/internal/onboardingservice/v1"
)

type Client interface {
GetOrCreateCLIOnboardingState(
ctx context.Context,
in *onboardingservice.GetOrCreateCLIOnboardingStateRequest,
opts ...grpc.CallOption,
) (*onboardingservice.GetOrCreateCLIOnboardingStateResponse, error)
}

type PollConfig struct {
Interval time.Duration
MaxInterval time.Duration
FastAttempts int
// MaxConsecutiveFailures is how many retryable errors in a row Wait
// tolerates before returning the last one. A successful response resets
// the count. Zero means no limit.
MaxConsecutiveFailures int
}

// DefaultPollConfig matches the website's waitForStep:
// 1s for 10 polls, then doubling to a 30s cap. 15 failures in a row gives up
// after about 40s if the first polls fail, or about 7.5 minutes once polling
// has slowed to 30s.
var DefaultPollConfig = PollConfig{
Interval: time.Second,
MaxInterval: 30 * time.Second,
FastAttempts: 10,
MaxConsecutiveFailures: 15,
}

func IsComplete(resp *onboardingservice.GetOrCreateCLIOnboardingStateResponse) bool {
return resp.GetUserAlreadyExists() ||
resp.GetState().GetCurrentStep() == onboardingservice.StepType_STEP_TYPE_END_OF_FUNNEL
}

// Wait calls GetOrCreateCLIOnboardingState until onboarding is complete.
func Wait(
ctx context.Context,
client Client,
req *onboardingservice.GetOrCreateCLIOnboardingStateRequest,
cfg PollConfig,
onStep func(onboardingservice.StepType),
) (*onboardingservice.GetOrCreateCLIOnboardingStateResponse, error) {
interval := cfg.Interval
lastStep := onboardingservice.StepType(-1)
failures := 0

for attempt := 1; ; attempt++ {
resp, err := client.GetOrCreateCLIOnboardingState(ctx, req)
switch {
case ctx.Err() != nil:
return nil, waitError(ctx, lastStep)
case err == nil:
failures = 0
if IsComplete(resp) {
return resp, nil
}
if step := resp.GetState().GetCurrentStep(); step != lastStep {
lastStep = step
if onStep != nil {
onStep(step)
}
}
case !isRetryable(err):
return nil, fmt.Errorf("failed to get onboarding state: %w", err)
default:
failures++
if cfg.MaxConsecutiveFailures > 0 && failures >= cfg.MaxConsecutiveFailures {
return nil, fmt.Errorf("failed to get onboarding state: %w", err)
}
}

if attempt > cfg.FastAttempts {
interval = min(interval*2, cfg.MaxInterval)
}
timer := time.NewTimer(interval)
select {
case <-ctx.Done():
timer.Stop()
return nil, waitError(ctx, lastStep)
case <-timer.C:
}
}
}

func isRetryable(err error) bool {
switch status.Code(err) {
case codes.Unavailable, codes.ResourceExhausted:
return true
default:
return false
}
}

func waitError(ctx context.Context, lastStep onboardingservice.StepType) error {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
if lastStep < 0 {
return errors.New("timed out waiting for onboarding to start")
}
return fmt.Errorf("timed out waiting for onboarding to complete (last step: %s)", lastStep)
}
return ctx.Err()
}
Loading
Loading