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

fix!: rename the concept of account to environment #125

Merged
merged 2 commits into from
Nov 14, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ jobs:
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
args: --fast
120 changes: 60 additions & 60 deletions cmd/humanlog/account.go → cmd/humanlog/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
)

const (
accountCmdName = "account"
environmentCmdName = "environment"
)

func accountCmd(
func environmentCmd(
getCtx func(cctx *cli.Context) context.Context,
getLogger func(cctx *cli.Context) *slog.Logger,
getCfg func(cctx *cli.Context) *config.Config,
Expand All @@ -35,8 +35,8 @@ func accountCmd(
) cli.Command {
return cli.Command{
Hidden: hideUnreleasedFeatures == "true",
Name: accountCmdName,
Usage: "Manage accounts for the current user or org.",
Name: environmentCmdName,
Usage: "Manage environments for the current user or org.",
Before: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
state := getState(cctx)
Expand All @@ -60,19 +60,19 @@ func accountCmd(
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx, apiURL)

accountName := cctx.Args().First()
if accountName == "" {
environmentName := cctx.Args().First()
if environmentName == "" {
logerror("missing argument: <name>")
return cli.ShowSubcommandHelp(cctx)
}

// lookup `accountName` and set its ID in `state`
// lookup `environmentName` and set its ID in `state`
_ = ctx
_ = state
_ = tokenSource
_ = apiURL
_ = httpClient
_ = accountName
_ = environmentName

return nil
},
Expand All @@ -91,28 +91,28 @@ func accountCmd(
if err != nil {
return err
}
accountID, err := ensureAccountSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient, orgID)
environmentID, err := ensureEnvironmentSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient, orgID)
if err != nil {
return err
}
clOpts := connect.WithInterceptors(
auth.Interceptors(ll, tokenSource)...,
)
orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts)
iter := ListAccounts(ctx, orgID, orgClient)
a, ok, err := iterapi.Find(iter, func(el *organizationv1.ListAccountResponse_ListItem) bool {
return el.Account.Id == accountID
iter := ListEnvironments(ctx, orgID, orgClient)
a, ok, err := iterapi.Find(iter, func(el *organizationv1.ListEnvironmentResponse_ListItem) bool {
return el.Environment.Id == environmentID
})
if err != nil {
return err
}
if !ok {
logwarn("account with id %d doesn't exist anymore, select another one")
state.CurrentAccountID = nil
logwarn("environment with id %d doesn't exist anymore, select another one")
state.CurrentEnvironmentID = nil
return state.WriteBack()
}
printFact("id", a.Account.Id)
printFact("name", a.Account.Name)
printFact("id", a.Environment.Id)
printFact("name", a.Environment.Name)
return nil
},
},
Expand All @@ -136,15 +136,15 @@ func accountCmd(
)
orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts)

res, err := orgClient.CreateAccount(ctx, connect.NewRequest(&organizationv1.CreateAccountRequest{
res, err := orgClient.CreateEnvironment(ctx, connect.NewRequest(&organizationv1.CreateEnvironmentRequest{
OrganizationId: orgID,
}))
if err != nil {
return err
}
account := res.Msg.Account
printFact("created id", account.Id)
printFact("created name", account.Name)
environment := res.Msg.Environment
printFact("created id", environment.Id)
printFact("created name", environment.Name)
return nil
},
},
Expand All @@ -159,8 +159,8 @@ func accountCmd(
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx, apiURL)

accountName := cctx.Args().First()
if accountName == "" {
environmentName := cctx.Args().First()
if environmentName == "" {
logerror("missing argument: <name>")
return cli.ShowSubcommandHelp(cctx)
}
Expand All @@ -175,23 +175,23 @@ func accountCmd(
)
orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts)

el, ok, err := iterapi.Find(ListAccounts(ctx, orgID, orgClient), func(el *organizationv1.ListAccountResponse_ListItem) bool {
return el.Account.Name == accountName
el, ok, err := iterapi.Find(ListEnvironments(ctx, orgID, orgClient), func(el *organizationv1.ListEnvironmentResponse_ListItem) bool {
return el.Environment.Name == environmentName
})
if err != nil {
return err
}
if !ok {
return fmt.Errorf("no account with name %q", accountName)
return fmt.Errorf("no environment with name %q", environmentName)
}
printFact("id", el.Account.Id)
printFact("name", el.Account.Name)
printFact("id", el.Environment.Id)
printFact("name", el.Environment.Name)
return nil
},
},
{
Name: "list",
Usage: "list the accounts for the current user or org",
Usage: "list the environments for the current user or org",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
ll := getLogger(cctx)
Expand All @@ -209,12 +209,12 @@ func accountCmd(
return err
}

iter := ListAccounts(ctx, orgID, orgClient)
iter := ListEnvironments(ctx, orgID, orgClient)

for iter.Next() {
li := iter.Current()
account := li.Account
printFact("account name", account.Name)
environment := li.Environment
printFact("environment name", environment.Name)
return nil
}
if err := iter.Err(); err != nil {
Expand All @@ -225,7 +225,7 @@ func accountCmd(
},
{
Name: "generate-token",
Usage: "generate an API token for the current account",
Usage: "generate an API token for the current environment",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
ll := getLogger(cctx)
Expand All @@ -238,15 +238,15 @@ func accountCmd(
if err != nil {
return err
}
accountID, err := ensureAccountSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient, orgID)
environmentID, err := ensureEnvironmentSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient, orgID)
if err != nil {
return err
}
expiresAt, err := hubAskTokenExpiry("Creating an account token")
expiresAt, err := hubAskTokenExpiry("Creating an environment token")
if err != nil {
return err
}
roles, err := hubAskTokenRoles("Creating an account token")
roles, err := hubAskTokenRoles("Creating an environment token")
if err != nil {
return err
}
Expand All @@ -256,17 +256,17 @@ func accountCmd(

tokenClient := tokenv1connect.NewTokenServiceClient(httpClient, apiURL, clOpts)

res, err := tokenClient.GenerateAccountToken(ctx, connect.NewRequest(&tokenv1.GenerateAccountTokenRequest{
AccountId: accountID,
ExpiresAt: timestamppb.New(expiresAt),
Roles: roles,
res, err := tokenClient.GenerateEnvironmentToken(ctx, connect.NewRequest(&tokenv1.GenerateEnvironmentTokenRequest{
EnvironmentId: environmentID,
ExpiresAt: timestamppb.New(expiresAt),
Roles: roles,
}))
if err != nil {
return fmt.Errorf("generating account token: %v", err)
return fmt.Errorf("generating environment token: %v", err)
}
token := res.Msg.Token
printFact("id", token.TokenId)
printFact("account id", token.AccountId)
printFact("environment id", token.EnvironmentId)
printFact("expires at", token.ExpiresAt.AsTime())
printFact("roles", token.Roles)
printFact("token (secret! do not lose)", token.Token)
Expand All @@ -275,7 +275,7 @@ func accountCmd(
},
{
Name: "revoke-token",
Usage: "revoke an API token for the current account",
Usage: "revoke an API token for the current environment",
ArgsUsage: "<token id>",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
Expand All @@ -289,7 +289,7 @@ func accountCmd(
if err != nil {
return err
}
accountID, err := ensureAccountSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient, orgID)
environmentID, err := ensureEnvironmentSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient, orgID)
if err != nil {
return err
}
Expand All @@ -311,13 +311,13 @@ func accountCmd(

tokenClient := tokenv1connect.NewTokenServiceClient(httpClient, apiURL, clOpts)

loginfo("revoking token %d on account %d", tokenID, accountID)
res, err := tokenClient.RevokeAccountToken(ctx, connect.NewRequest(&tokenv1.RevokeAccountTokenRequest{
AccountId: accountID,
TokenId: tokenID,
loginfo("revoking token %d on environment %d", tokenID, environmentID)
res, err := tokenClient.RevokeEnvironmentToken(ctx, connect.NewRequest(&tokenv1.RevokeEnvironmentTokenRequest{
EnvironmentId: environmentID,
TokenId: tokenID,
}))
if err != nil {
return fmt.Errorf("revoking account token: %v", err)
return fmt.Errorf("revoking environment token: %v", err)
}
_ = res
loginfo("token revoked")
Expand All @@ -326,7 +326,7 @@ func accountCmd(
},
{
Name: "view-token",
Usage: "view the details of an API token for the current account",
Usage: "view the details of an API token for the current environment",
ArgsUsage: "<token id>",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
Expand All @@ -340,7 +340,7 @@ func accountCmd(
if err != nil {
return err
}
accountID, err := ensureAccountSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient, orgID)
environmentID, err := ensureEnvironmentSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient, orgID)
if err != nil {
return err
}
Expand All @@ -362,16 +362,16 @@ func accountCmd(

tokenClient := tokenv1connect.NewTokenServiceClient(httpClient, apiURL, clOpts)

res, err := tokenClient.GetAccountToken(ctx, connect.NewRequest(&tokenv1.GetAccountTokenRequest{
AccountId: accountID,
TokenId: tokenID,
res, err := tokenClient.GetEnvironmentToken(ctx, connect.NewRequest(&tokenv1.GetEnvironmentTokenRequest{
EnvironmentId: environmentID,
TokenId: tokenID,
}))
if err != nil {
return fmt.Errorf("revoking account token: %v", err)
return fmt.Errorf("revoking environment token: %v", err)
}
token := res.Msg.Token
printFact("id", token.TokenId)
printFact("account id", token.AccountId)
printFact("environment id", token.EnvironmentId)
printFact("roles", token.Roles)
printFact("expires at", token.ExpiresAt.AsTime())
if token.LastUsedAt != nil {
Expand All @@ -389,7 +389,7 @@ func accountCmd(
},
{
Name: "list-tokens",
Usage: "list the API tokens for the current account",
Usage: "list the API tokens for the current environment",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
ll := getLogger(cctx)
Expand All @@ -402,7 +402,7 @@ func accountCmd(
if err != nil {
return err
}
accountID, err := ensureAccountSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient, orgID)
environmentID, err := ensureEnvironmentSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient, orgID)
if err != nil {
return err
}
Expand All @@ -414,7 +414,7 @@ func accountCmd(
tokenClient := tokenv1connect.NewTokenServiceClient(httpClient, apiURL, clOpts)

hasAny := false
iter := ListAccountTokens(ctx, accountID, tokenClient)
iter := ListEnvironmentTokens(ctx, environmentID, tokenClient)
for iter.Next() {
hasAny = true
token := iter.Current().Token
Expand All @@ -424,7 +424,7 @@ func accountCmd(
return fmt.Errorf("listing tokens: %v", err)
}
if !hasAny {
loginfo("no account token found")
loginfo("no environment token found")
}

return nil
Expand All @@ -434,7 +434,7 @@ func accountCmd(
}
}

func promptCreateAccount(ctx context.Context,
func promptCreateEnvironment(ctx context.Context,
ll *slog.Logger,
cctx *cli.Context,
state *state.State,
Expand Down
Loading
Loading