Skip to content
Merged
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
54 changes: 27 additions & 27 deletions internal/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/siliconwitchery/superstack-cli/internal/api"
)

func Balance(session api.Session, arguments []string) error {
func Balance(invocation api.Invocation, arguments []string) error {
positionals, jsonOutput := api.TakeJsonFlag(arguments)

if len(positionals) > 1 {
Expand All @@ -33,7 +33,7 @@ func Balance(session api.Session, arguments []string) error {
chosenFleetId = parsed
}

fleets, err := api.FetchFleets(session)
fleets, err := api.FetchFleets(invocation)

if err != nil {
return err
Expand All @@ -51,7 +51,7 @@ func Balance(session api.Session, arguments []string) error {
}
}

fetched, err := api.FetchBalances(session)
fetched, err := api.FetchBalances(invocation)

if err != nil {
return err
Expand All @@ -66,16 +66,16 @@ func Balance(session api.Session, arguments []string) error {
}

if jsonOutput {
err = json.NewEncoder(session.Out).Encode(balances)
err = json.NewEncoder(invocation.Out).Encode(balances)

return err
}

if len(balances) == 0 {
if chosenFleetId == 0 {
fmt.Fprintln(session.Out, "No fleets yet. Create one with fleet create.")
fmt.Fprintln(invocation.Out, "No fleets yet. Create one with fleet create.")
} else {
fmt.Fprintln(session.Out, "No credit on that fleet yet.")
fmt.Fprintln(invocation.Out, "No credit on that fleet yet.")
}

return nil
Expand All @@ -101,16 +101,16 @@ func Balance(session api.Session, arguments []string) error {
nameWidth = max(nameWidth, len(nameValues[index]))
}

fmt.Fprintf(session.Out, "%-*s %-*s %s\n", idWidth, "ID", nameWidth, "NAME", "BALANCE")
fmt.Fprintf(invocation.Out, "%-*s %-*s %s\n", idWidth, "ID", nameWidth, "NAME", "BALANCE")

for index, balance := range balances {
fmt.Fprintf(session.Out, "%-*d %-*s %s\n", idWidth, balance.Fleet, nameWidth, nameValues[index], amountValues[index])
fmt.Fprintf(invocation.Out, "%-*d %-*s %s\n", idWidth, balance.Fleet, nameWidth, nameValues[index], amountValues[index])
}

return nil
}

func Topup(session api.Session, arguments []string) error {
func TopUp(invocation api.Invocation, arguments []string) error {
if len(arguments) != 1 {
return errors.New("account topup takes a fleet id")
}
Expand All @@ -121,17 +121,17 @@ func Topup(session api.Session, arguments []string) error {
return errors.New("the fleet id is the number shown by fleet list")
}

request, err := api.AuthenticatedRequest(session, http.MethodPost,
request, err := api.AuthenticatedRequest(invocation, http.MethodPost,
"/fleets/"+strconv.FormatInt(fleetId, 10)+"/topup", nil)

if err != nil {
return err
}

response, err := session.Client.Do(request)
response, err := invocation.Client.Do(request)

if err != nil {
return errors.New("the server could not be reached, check your connection")
return errors.New("the server could not be reached, check your internet access")
}

defer response.Body.Close()
Expand All @@ -150,34 +150,34 @@ func Topup(session api.Session, arguments []string) error {
return errors.New("could not open the top-up page, try again")
}

fmt.Fprintf(session.Out, "Open this link to choose an amount and pay:\n\n %s\n\nThe credit appears on the balance once the top-up completes.\nPress enter to open the browser.\n", api.Printable(opened.Url))
fmt.Fprintf(invocation.Out, "Open this page to choose an amount and pay:\n\n %s\n\nThe credit appears on the balance once the top-up completes.\nPress enter to open the browser.\n", api.Printable(opened.Url))

_, err = bufio.NewReader(session.In).ReadString('\n')
_, err = bufio.NewReader(invocation.In).ReadString('\n')

if err != nil {
return nil
}

session.OpenBrowser(opened.Url)
invocation.OpenBrowser(opened.Url)

return nil
}

func Delete(session api.Session, arguments []string) error {
func Delete(invocation api.Invocation, arguments []string) error {
if len(arguments) != 0 {
return errors.New("account delete takes no arguments")
}

request, err := api.AuthenticatedRequest(session, http.MethodGet, "/fleets", nil)
request, err := api.AuthenticatedRequest(invocation, http.MethodGet, "/fleets", nil)

if err != nil {
return err
}

response, err := session.Client.Do(request)
response, err := invocation.Client.Do(request)

if err != nil {
return errors.New("the server could not be reached, check your connection")
return errors.New("the server could not be reached, check your internet access")
}

if response.StatusCode != http.StatusOK {
Expand All @@ -190,27 +190,27 @@ func Delete(session api.Session, arguments []string) error {

response.Body.Close()

fmt.Fprint(session.Out, "Delete your account, its logins, and your access to every fleet? This cannot be undone. [y/N] ")
fmt.Fprint(invocation.Out, "Delete your account, its logins, and your access to every fleet? This cannot be undone. [y/N] ")

answer, _ := bufio.NewReader(session.In).ReadString('\n')
answer, _ := bufio.NewReader(invocation.In).ReadString('\n')

answer = strings.ToLower(strings.TrimSpace(answer))

if answer != "y" && answer != "yes" {
fmt.Fprintln(session.Out, "Nothing deleted.")
fmt.Fprintln(invocation.Out, "Nothing deleted.")
return nil
}

request, err = api.AuthenticatedRequest(session, http.MethodDelete, "/account", nil)
request, err = api.AuthenticatedRequest(invocation, http.MethodDelete, "/account", nil)

if err != nil {
return err
}

response, err = session.Client.Do(request)
response, err = invocation.Client.Do(request)

if err != nil {
return errors.New("the server could not be reached, check your connection")
return errors.New("the server could not be reached, check your internet access")
}

defer response.Body.Close()
Expand All @@ -219,9 +219,9 @@ func Delete(session api.Session, arguments []string) error {
return api.ServerError(response)
}

fmt.Fprintln(session.Out, "Account deleted.")
fmt.Fprintln(invocation.Out, "Account deleted.")

path, err := api.KeyPath()
path, err := api.LoginKeyPath()

if err != nil {
return err
Expand Down
42 changes: 21 additions & 21 deletions internal/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ func TestAccountBalance(t *testing.T) {
fmt.Fprint(w, test.balances)
})

session, out := apitest.LoggedInSession(t, mux)
invocation, out := apitest.LoggedInInvocation(t, mux)

err := Balance(session, test.arguments)
err := Balance(invocation, test.arguments)

printed := out.String()

Expand Down Expand Up @@ -169,7 +169,7 @@ func TestAccountBalance(t *testing.T) {
}
}

func TestAccountTopup(t *testing.T) {
func TestAccountTopUp(t *testing.T) {
tests := []struct {
name string
arguments []string
Expand All @@ -181,14 +181,14 @@ func TestAccountTopup(t *testing.T) {
wantError string
}{
{
name: "a top-up link opened on enter",
name: "the top-up page opened on enter",
arguments: []string{"3"},
stdin: "\n",
wantPath: "/fleets/3/topup",
wantBrowser: true,
},
{
name: "a top-up link left alone",
name: "the top-up page left alone",
arguments: []string{"3"},
wantPath: "/fleets/3/topup",
},
Expand Down Expand Up @@ -245,13 +245,13 @@ func TestAccountTopup(t *testing.T) {
fmt.Fprint(w, `{"url":"https://checkout.stripe.com/c/pay/cs_test_1"}`)
})

session, out := apitest.LoggedInSession(t, mux)
session.In = strings.NewReader(test.stdin)
invocation, out := apitest.LoggedInInvocation(t, mux)
invocation.In = strings.NewReader(test.stdin)

browserOpens := make(chan string, 1)
session.OpenBrowser = func(url string) { browserOpens <- url }
invocation.OpenBrowser = func(url string) { browserOpens <- url }

err := Topup(session, test.arguments)
err := TopUp(invocation, test.arguments)

printed := out.String()

Expand All @@ -268,7 +268,7 @@ func TestAccountTopup(t *testing.T) {
}

if !strings.Contains(printed, "https://checkout.stripe.com/c/pay/cs_test_1") {
t.Errorf("the output %q does not show the payment link", printed)
t.Errorf("the output %q does not show the top-up page", printed)
}

if !strings.Contains(printed, "The credit appears on the balance once the top-up completes.") {
Expand All @@ -280,7 +280,7 @@ func TestAccountTopup(t *testing.T) {
if !test.wantBrowser {
t.Errorf("the browser opened %q although enter was never pressed", url)
} else if url != "https://checkout.stripe.com/c/pay/cs_test_1" {
t.Errorf("the browser opened %q, want the payment link", url)
t.Errorf("the browser opened %q, want the top-up page", url)
}

default:
Expand Down Expand Up @@ -367,17 +367,17 @@ func TestAccountDelete(t *testing.T) {
w.WriteHeader(http.StatusNoContent)
})

session, out := apitest.LoggedInSession(t, mux)
invocation, out := apitest.LoggedInInvocation(t, mux)

path, err := api.KeyPath()
path, err := api.LoginKeyPath()

if err != nil {
t.Fatal(err)
}

session.In = strings.NewReader(test.answer)
invocation.In = strings.NewReader(test.answer)

err = Delete(session, test.arguments)
err = Delete(invocation, test.arguments)

printed := out.String()

Expand Down Expand Up @@ -411,14 +411,14 @@ func TestAccountDelete(t *testing.T) {
}

func TestAccountDeleteAsksNothingWhenTheServerIsGone(t *testing.T) {
session, out := apitest.LoggedInSession(t, http.NewServeMux())
invocation, out := apitest.LoggedInInvocation(t, http.NewServeMux())

gone := httptest.NewServer(http.NotFoundHandler())
gone.Close()

session.Base = gone.URL
invocation.Base = gone.URL

err := Delete(session, nil)
err := Delete(invocation, nil)

if err == nil || !strings.Contains(err.Error(), "could not be reached") {
t.Fatalf("error = %v, want it to mention the server could not be reached", err)
Expand Down Expand Up @@ -470,11 +470,11 @@ func TestAccountDeleteStopsWhenTheProbeIsRefused(t *testing.T) {
deleted = true
})

session, out := apitest.LoggedInSession(t, mux)
invocation, out := apitest.LoggedInInvocation(t, mux)

session.In = strings.NewReader("y\n")
invocation.In = strings.NewReader("y\n")

err := Delete(session, nil)
err := Delete(invocation, nil)

if err == nil || !strings.Contains(err.Error(), test.wantError) {
t.Fatalf("error = %v, want the server's own refusal", err)
Expand Down
Loading