From ca4a7f5962d505fa37b57a8c031c03126b0f4321 Mon Sep 17 00:00:00 2001
From: Link Dupont
Date: Thu, 18 Jan 2024 13:54:33 -0500
Subject: [PATCH] feat: add service-account subcommand
xrhidgen can now generate service-account records using the
'service-account' subcommand.
---
cmd/xrhidgen/main.go | 42 +++++++++++++-------------
cmd/xrhidgen/service_account.go | 53 +++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 20 deletions(-)
create mode 100644 cmd/xrhidgen/service_account.go
diff --git a/cmd/xrhidgen/main.go b/cmd/xrhidgen/main.go
index a731ff8..2f69c72 100644
--- a/cmd/xrhidgen/main.go
+++ b/cmd/xrhidgen/main.go
@@ -1,25 +1,26 @@
// xrhidgen generates X-Rh-Identity records.
//
-// Usage:
-// xrhidgen [flags]
-
-// 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]
+//
+// 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 (
@@ -61,6 +62,7 @@ func main() {
internalCommand,
systemCommand,
associateCommand,
+ serviceAccountCommand,
},
Exec: func(context.Context, []string) error {
return flag.ErrHelp
diff --git a/cmd/xrhidgen/service_account.go b/cmd/xrhidgen/service_account.go
new file mode 100644
index 0000000..27dc280
--- /dev/null
+++ b/cmd/xrhidgen/service_account.go
@@ -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
+ },
+}