-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add service-account subcommand
xrhidgen can now generate service-account records using the 'service-account' subcommand.
- Loading branch information
Showing
2 changed files
with
75 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} |