Skip to content

Commit

Permalink
Massively simplify arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanratcliffe committed Feb 14, 2024
1 parent 9b7ce7b commit 6d55832
Show file tree
Hide file tree
Showing 18 changed files with 34 additions and 83 deletions.
40 changes: 10 additions & 30 deletions cmd/auth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,64 +33,44 @@ func UnauthenticatedApiKeyClient(ctx context.Context) sdpconnect.ApiKeyServiceCl
// embedded in the context and otel instrumentation
func AuthenticatedBookmarkClient(ctx context.Context) sdpconnect.BookmarksServiceClient {
httpClient := NewAuthenticatedClient(ctx, otelhttp.DefaultClient)
url := viper.GetString("bookmark-url")
if url == "" {
url = viper.GetString("url")
viper.Set("bookmark-url", url)
}
log.WithContext(ctx).WithField("bookmark-url", url).Debug("Connecting to overmind bookmark API")
url := viper.GetString("url")
log.WithContext(ctx).WithField("url", url).Debug("Connecting to overmind bookmark API")
return sdpconnect.NewBookmarksServiceClient(httpClient, url)
}

// AuthenticatedChangesClient Returns a bookmark client that uses the auth
// embedded in the context and otel instrumentation
func AuthenticatedChangesClient(ctx context.Context) sdpconnect.ChangesServiceClient {
httpClient := NewAuthenticatedClient(ctx, otelhttp.DefaultClient)
url := viper.GetString("changes-url")
if url == "" {
url = viper.GetString("url")
viper.Set("changes-url", url)
}
log.WithContext(ctx).WithField("changes-url", url).Debug("Connecting to overmind changes API")
url := viper.GetString("url")
log.WithContext(ctx).WithField("url", url).Debug("Connecting to overmind changes API")
return sdpconnect.NewChangesServiceClient(httpClient, url)
}

// AuthenticatedManagementClient Returns a bookmark client that uses the auth
// embedded in the context and otel instrumentation
func AuthenticatedManagementClient(ctx context.Context) sdpconnect.ManagementServiceClient {
httpClient := NewAuthenticatedClient(ctx, otelhttp.DefaultClient)
url := viper.GetString("management-url")
if url == "" {
url = viper.GetString("url")
viper.Set("management-url", url)
}
log.WithContext(ctx).WithField("management-url", url).Debug("Connecting to overmind management API")
url := viper.GetString("url")
log.WithContext(ctx).WithField("url", url).Debug("Connecting to overmind management API")
return sdpconnect.NewManagementServiceClient(httpClient, url)
}

// AuthenticatedSnapshotsClient Returns a Snapshots client that uses the auth
// embedded in the context and otel instrumentation
func AuthenticatedSnapshotsClient(ctx context.Context) sdpconnect.SnapshotsServiceClient {
httpClient := NewAuthenticatedClient(ctx, otelhttp.DefaultClient)
url := viper.GetString("snapshot-url")
if url == "" {
url = viper.GetString("url")
viper.Set("snapshot-url", url)
}
log.WithContext(ctx).WithField("snapshot-url", url).Debug("Connecting to overmind snapshot API")
url := viper.GetString("url")
log.WithContext(ctx).WithField("url", url).Debug("Connecting to overmind snapshot API")
return sdpconnect.NewSnapshotsServiceClient(httpClient, url)
}

// AuthenticatedInviteClient Returns a Invite client that uses the auth
// embedded in the context and otel instrumentation
func AuthenticatedInviteClient(ctx context.Context) sdpconnect.InviteServiceClient {
httpClient := NewAuthenticatedClient(ctx, otelhttp.DefaultClient)
url := viper.GetString("invite-url")
if url == "" {
url = viper.GetString("url")
viper.Set("invite-url", url)
}
log.WithContext(ctx).WithField("invite-url", url).Debug("Connecting to overmind invite API")
url := viper.GetString("url")
log.WithContext(ctx).WithField("url", url).Debug("Connecting to overmind invite API")
return sdpconnect.NewInviteServiceClient(httpClient, url)
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ executed as a single block.`,
func init() {
rootCmd.AddCommand(bookmarksCmd)

addAPIFlags(bookmarksCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
Expand Down
6 changes: 1 addition & 5 deletions cmd/bookmarks_create_bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func CreateBookmark(ctx context.Context, ready chan bool) int {
})
if err != nil {
log.WithContext(ctx).WithError(err).WithFields(log.Fields{
"bookmark-url": viper.GetString("bookmark-url"),
"url": viper.GetString("url"),
}).Error("failed to get bookmark")
return 1
}
Expand Down Expand Up @@ -142,9 +142,5 @@ func CreateBookmark(ctx context.Context, ready chan bool) int {
func init() {
bookmarksCmd.AddCommand(createBookmarkCmd)

createBookmarkCmd.PersistentFlags().String("bookmark-url", "", "The bookmark service API endpoint (defaults to --url)")

createBookmarkCmd.PersistentFlags().String("file", "", "JSON formatted file to read bookmark. (defaults to stdin)")

createBookmarkCmd.PersistentFlags().String("timeout", "5m", "How long to wait for responses")
}
7 changes: 1 addition & 6 deletions cmd/bookmarks_get_affected_bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func GetAffectedBookmarks(ctx context.Context, ready chan bool) int {
})
if err != nil {
log.WithContext(ctx).WithError(err).WithFields(log.Fields{
"bookmark-url": viper.GetString("bookmark-url"),
"url": viper.GetString("url"),
}).Error("failed to get affected bookmarks")
return 1
}
Expand All @@ -118,11 +118,6 @@ func GetAffectedBookmarks(ctx context.Context, ready chan bool) int {
func init() {
bookmarksCmd.AddCommand(getAffectedBookmarksCmd)

getAffectedBookmarksCmd.PersistentFlags().String("bookmark-url", "", "The bookmark service API endpoint (defaults to --url)")
getAffectedBookmarksCmd.PersistentFlags().String("frontend", "https://app.overmind.tech/", "The frontend base URL")

getAffectedBookmarksCmd.PersistentFlags().String("snapshot-uuid", "", "The UUID of the snapshot that should be checked.")
getAffectedBookmarksCmd.PersistentFlags().String("bookmark-uuids", "", "A comma separated list of UUIDs of the potentially affected bookmarks.")

getAffectedBookmarksCmd.PersistentFlags().String("timeout", "5m", "How long to wait for responses")
}
6 changes: 1 addition & 5 deletions cmd/bookmarks_get_bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func GetBookmark(ctx context.Context, ready chan bool) int {
})
if err != nil {
log.WithContext(ctx).WithError(err).WithFields(log.Fields{
"bookmark-url": viper.GetString("bookmark-url"),
"url": viper.GetString("url"),
}).Error("failed to get bookmark")
return 1
}
Expand All @@ -115,9 +115,5 @@ func GetBookmark(ctx context.Context, ready chan bool) int {
func init() {
bookmarksCmd.AddCommand(getBookmarkCmd)

getBookmarkCmd.PersistentFlags().String("bookmark-url", "", "The bookmark service API endpoint (defaults to --url)")

getBookmarkCmd.PersistentFlags().String("uuid", "", "The UUID of the bookmark that should be displayed.")

getBookmarkCmd.PersistentFlags().String("timeout", "1m", "How long to wait for responses")
}
2 changes: 2 additions & 0 deletions cmd/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ using these commands directly, but they are provided for flexibility.`,
func init() {
rootCmd.AddCommand(changesCmd)

addAPIFlags(changesCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
Expand Down
6 changes: 1 addition & 5 deletions cmd/changes_end_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,5 @@ func EndChange(ctx context.Context, ready chan bool) int {
func init() {
changesCmd.AddCommand(endChangeCmd)

withChangeUuidFlags(endChangeCmd)

endChangeCmd.PersistentFlags().String("frontend", "https://app.overmind.tech/", "The frontend base URL")

endChangeCmd.PersistentFlags().String("timeout", "5m", "How long to wait for responses")
addChangeUuidFlags(endChangeCmd)
}
4 changes: 1 addition & 3 deletions cmd/changes_get_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,9 @@ fetch:
func init() {
changesCmd.AddCommand(getChangeCmd)

withChangeUuidFlags(getChangeCmd)
addChangeUuidFlags(getChangeCmd)
getChangeCmd.PersistentFlags().String("status", "", "The expected status of the change. Use this with --ticket-link. Allowed values: CHANGE_STATUS_UNSPECIFIED, CHANGE_STATUS_DEFINING, CHANGE_STATUS_HAPPENING, CHANGE_STATUS_PROCESSING, CHANGE_STATUS_DONE")

getChangeCmd.PersistentFlags().String("frontend", "https://app.overmind.tech/", "The frontend base URL")
getChangeCmd.PersistentFlags().String("format", "json", "How to render the change. Possible values: json, markdown")

getChangeCmd.PersistentFlags().String("timeout", "5m", "How long to wait for responses")
}
3 changes: 0 additions & 3 deletions cmd/changes_list_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,7 @@ func printJson(ctx context.Context, b []byte, prefix, id string) error {
func init() {
changesCmd.AddCommand(listChangesCmd)

listChangesCmd.PersistentFlags().String("frontend", "https://app.overmind.tech/", "The frontend base URL")
listChangesCmd.PersistentFlags().String("format", "files", "How to render the change. Possible values: files, json")
listChangesCmd.PersistentFlags().String("dir", "./output", "A directory name to use for rendering changes when using the 'files' format")
listChangesCmd.PersistentFlags().Bool("fetch-data", false, "also fetch the blast radius and system state snapshots for each change")

listChangesCmd.PersistentFlags().String("timeout", "5m", "How long to wait for responses")
}
4 changes: 0 additions & 4 deletions cmd/changes_manual_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,6 @@ func ManualChange(ctx context.Context, ready chan bool) int {
func init() {
changesCmd.AddCommand(manualChangeCmd)

manualChangeCmd.PersistentFlags().String("changes-url", "", "The changes service API endpoint (defaults to --url)")
manualChangeCmd.PersistentFlags().String("management-url", "", "The management service API endpoint (defaults to --url)")
manualChangeCmd.PersistentFlags().String("frontend", "https://app.overmind.tech", "The frontend base URL")

manualChangeCmd.PersistentFlags().String("title", "", "Short title for this change.")
Expand All @@ -236,6 +234,4 @@ func init() {
manualChangeCmd.PersistentFlags().String("query-scope", "*", "The scope to query")
manualChangeCmd.PersistentFlags().String("query-type", "*", "The type to query")
manualChangeCmd.PersistentFlags().String("query", "", "The actual query to send")

manualChangeCmd.PersistentFlags().String("timeout", "3m", "How long to wait for responses")
}
4 changes: 0 additions & 4 deletions cmd/changes_submit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,6 @@ func SubmitPlan(ctx context.Context, files []string, ready chan bool) int {
func init() {
changesCmd.AddCommand(submitPlanCmd)

submitPlanCmd.PersistentFlags().String("changes-url", "", "The changes service API endpoint (defaults to --url)")
submitPlanCmd.PersistentFlags().String("management-url", "", "The management service API endpoint (defaults to --url)")
submitPlanCmd.PersistentFlags().String("frontend", "https://app.overmind.tech", "The frontend base URL")

submitPlanCmd.PersistentFlags().String("title", "", "Short title for this change. If this is not specified, overmind will try to come up with one for you.")
Expand All @@ -738,6 +736,4 @@ func init() {

submitPlanCmd.PersistentFlags().String("terraform-plan-output", "", "Filename of cached terraform plan output for this change.")
submitPlanCmd.PersistentFlags().String("code-changes-diff", "", "Fileame of the code diff of this change.")

submitPlanCmd.PersistentFlags().String("timeout", "3m", "How long to wait for responses")
}
6 changes: 1 addition & 5 deletions cmd/chnages_start_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,5 @@ func StartChange(ctx context.Context, ready chan bool) int {
func init() {
changesCmd.AddCommand(startChangeCmd)

withChangeUuidFlags(startChangeCmd)

startChangeCmd.PersistentFlags().String("frontend", "https://app.overmind.tech/", "The frontend base URL")

startChangeCmd.PersistentFlags().String("timeout", "5m", "How long to wait for responses")
addChangeUuidFlags(startChangeCmd)
}
1 change: 1 addition & 0 deletions cmd/invites.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var invitesCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(invitesCmd)

addAPIFlags(invitesCmd)
// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
Expand Down
3 changes: 0 additions & 3 deletions cmd/invites_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,12 @@ func InvitesList(ctx context.Context) int {
func init() {
// list sub-command
invitesCmd.AddCommand(listCmd)
listCmd.PersistentFlags().String("invite-url", "", "A custom URL for the invites API (optional)")

// create sub-command
invitesCmd.AddCommand(createCmd)
createCmd.PersistentFlags().String("invite-url", "", "A custom URL for the invites API (optional)")
createCmd.PersistentFlags().StringSlice("emails", []string{}, "A list of emails to invite")

// revoke sub-command
invitesCmd.AddCommand(revokeCmd)
revokeCmd.PersistentFlags().String("invite-url", "", "A custom URL for the invites API (optional)")
revokeCmd.PersistentFlags().String("email", "", "The email address to revoke")
}
11 changes: 7 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,20 +413,23 @@ func parseChangeUrl(changeUrlString string) (uuid.UUID, error) {
return changeUuid, nil
}

func withChangeUuidFlags(cmd *cobra.Command) {
func addChangeUuidFlags(cmd *cobra.Command) {
cmd.PersistentFlags().String("change", "", "The frontend URL of the change to get")
cmd.PersistentFlags().String("ticket-link", "", "Link to the ticket for this change.")
cmd.PersistentFlags().String("uuid", "", "The UUID of the change that should be displayed.")
cmd.MarkFlagsMutuallyExclusive("change", "ticket-link", "uuid")
}

// Adds common flags to API commands e.g. timeout
func addAPIFlags(cnd *cobra.Command) {
createBookmarkCmd.PersistentFlags().String("timeout", "5m", "How long to wait for responses")
}

func init() {
cobra.OnInitialize(initConfig)

// General Config
rootCmd.PersistentFlags().StringVar(&logLevel, "log", "info", "Set the log level. Valid values: panic, fatal, error, warn, info, debug, trace")

// api endpoint
rootCmd.PersistentFlags().String("url", "https://api.prod.overmind.tech", "The overmind API endpoint")

// Support API Keys in the environment
Expand All @@ -440,7 +443,7 @@ func init() {
// Mark this as hidden. This means that it will still be parsed of supplied,
// and we will still look for it in the environment, but it won't be shown
// in the help
rootCmd.Flags().MarkHidden("honeycomb-api-key")
rootCmd.PersistentFlags().MarkHidden("honeycomb-api-key")

Check failure on line 446 in cmd/root.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `(*github.com/spf13/pflag.FlagSet).MarkHidden` is not checked (errcheck)

// Create groups
rootCmd.AddGroup(&cobra.Group{
Expand Down
2 changes: 2 additions & 0 deletions cmd/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ required.`,
func init() {
rootCmd.AddCommand(snapshotsCmd)

addAPIFlags(snapshotsCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
Expand Down
7 changes: 1 addition & 6 deletions cmd/snapshots_get_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func GetSnapshot(ctx context.Context, ready chan bool) int {
})
if err != nil {
log.WithContext(ctx).WithError(err).WithFields(log.Fields{
"snapshot-url": viper.GetString("snapshot-url"),
"url": viper.GetString("url"),
}).Error("failed to get snapshot")
return 1
}
Expand Down Expand Up @@ -130,10 +130,5 @@ func GetSnapshot(ctx context.Context, ready chan bool) int {
func init() {
snapshotsCmd.AddCommand(getSnapshotCmd)

getSnapshotCmd.PersistentFlags().String("snapshot-url", "", "The snapshot service API endpoint (defaults to --url)")
getSnapshotCmd.PersistentFlags().String("frontend", "https://app.overmind.tech/", "The frontend base URL")

getSnapshotCmd.PersistentFlags().String("uuid", "", "The UUID of the snapshot that should be displayed.")

getSnapshotCmd.PersistentFlags().String("timeout", "5m", "How long to wait for responses")
}
3 changes: 3 additions & 0 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var terraformCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(terraformCmd)

// Hide this flag from the Terraform help as we don't want it to be messy
rootCmd.PersistentFlags().MarkHidden("url")

Check failure on line 22 in cmd/terraform.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `(*github.com/spf13/pflag.FlagSet).MarkHidden` is not checked (errcheck)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
Expand Down

0 comments on commit 6d55832

Please sign in to comment.