Skip to content

Commit

Permalink
feat: add service-account subcommand
Browse files Browse the repository at this point in the history
xrhidgen can now generate service-account records using the
'service-account' subcommand.
  • Loading branch information
subpop committed Jan 18, 2024
1 parent 40e96bc commit ca4a7f5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 20 deletions.
42 changes: 22 additions & 20 deletions cmd/xrhidgen/main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
// xrhidgen generates X-Rh-Identity records.
//
// Usage:
// xrhidgen [flags] <subcommand>

// xrhidgen can be used to generate JSON records suitable for passing in to
// the X-Rh-Identity header. Each subcommand will generate a record of the
// specified type. Any flag set will be inserted instead of a random value.
// All remaining fields will be filled with a suitably random value.

// SUBCOMMANDS
// user generate a user identity JSON record
// internal generate an internal identity JSON record
// system generate a system identity JSON record
// associate generate an associate identity JSON record

// FLAGS
// -account-number ... set the identity.account_number field (string)
// -auth-type ... set the identity.authtype field (string)
// -employe-account-number ... set the identity.employee_account_number field (string)
// -org-id ... set the identity.org_id field (string)
// -type ... set the identity.type field (string)
// USAGE
// xrhidgen [flags] <subcommand>
//
// xrhidgen can be used to generate JSON records suitable for passing in to
// the X-Rh-Identity header. Each subcommand will generate a record of the
// specified type. Any flag set will be inserted instead of a random value.
// All remaining fields will be filled with a suitably random value.
//
// SUBCOMMANDS
// user generate a user identity JSON record
// internal generate an internal identity JSON record
// system generate a system identity JSON record
// associate generate an associate identity JSON record
// service-account generate a service account identity JSON record
//
// FLAGS
// -account-number value set the identity.account_number field (string)
// -auth-type value set the identity.authtype field (string)
// -employe-account-number value set the identity.employee_account_number field (string)
// -org-id value set the identity.org_id field (string)
// -type value set the identity.type field (string)
package main

import (
Expand Down Expand Up @@ -61,6 +62,7 @@ func main() {
internalCommand,
systemCommand,
associateCommand,
serviceAccountCommand,
},
Exec: func(context.Context, []string) error {
return flag.ErrHelp
Expand Down
53 changes: 53 additions & 0 deletions cmd/xrhidgen/service_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"

"github.com/peterbourgon/ff/v3/ffcli"
"github.com/subpop/xrhidgen"
)

var serviceAccountFlags struct {
clientID StringFlag
username StringFlag
}

func NewServiceAccountFlagSet(name string, errorHandling flag.ErrorHandling) *flag.FlagSet {
fs := flag.NewFlagSet(name, errorHandling)

fs.Var(&serviceAccountFlags.clientID, "client-id", "set the identity.service_account.client_id field (string)")
fs.Var(&serviceAccountFlags.username, "username", "set the identity.service_account.username field (string)")

return fs
}

var serviceAccountCommand = &ffcli.Command{
Name: "service-account",
ShortUsage: "service-account [flags]",
ShortHelp: "generate a service account identity JSON record",
LongHelp: WordWrap("Generate a service account identity record, populating fields with values provided by the matching flag. Any omitted flags will have their corresponding fields populated with a suitable random value", 72),
FlagSet: NewServiceAccountFlagSet("service-account", flag.ExitOnError),
Exec: func(ctx context.Context, args []string) error {
serviceAccount := xrhidgen.ServiceAccount{
ClientID: serviceAccountFlags.clientID.Value,
Username: serviceAccountFlags.username.Value,
}

id, err := xrhidgen.NewServiceAccountIdentity(mainIdentity(), serviceAccount)
if err != nil {
return err
}

data, err := json.Marshal(id)
if err != nil {
return fmt.Errorf("cannot marshal data: %w", err)
}

fmt.Println(string(data))

return nil
},
}

0 comments on commit ca4a7f5

Please sign in to comment.