Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
// Copyright © 2023 Ory Corp
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package flagx
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/ory/x/cmdx"
|
|
)
|
|
|
|
func NewFlagSet(name string) *pflag.FlagSet {
|
|
return pflag.NewFlagSet(name, pflag.ContinueOnError)
|
|
}
|
|
|
|
// MustGetBool returns a bool flag or fatals if an error occurs.
|
|
// Deprecated: just handle the error properly, this breaks command testing
|
|
func MustGetBool(cmd *cobra.Command, name string) bool {
|
|
ok, err := cmd.Flags().GetBool(name)
|
|
if err != nil {
|
|
cmdx.Fatalf(err.Error())
|
|
}
|
|
return ok
|
|
}
|
|
|
|
// MustGetString returns a string flag or fatals if an error occurs.
|
|
// Deprecated: just handle the error properly, this breaks command testing
|
|
func MustGetString(cmd *cobra.Command, name string) string {
|
|
s, err := cmd.Flags().GetString(name)
|
|
if err != nil {
|
|
cmdx.Fatalf(err.Error())
|
|
}
|
|
return s
|
|
}
|
|
|
|
// MustGetDuration returns a time.Duration flag or fatals if an error occurs.
|
|
// Deprecated: just handle the error properly, this breaks command testing
|
|
func MustGetDuration(cmd *cobra.Command, name string) time.Duration {
|
|
d, err := cmd.Flags().GetDuration(name)
|
|
if err != nil {
|
|
cmdx.Fatalf(err.Error())
|
|
}
|
|
return d
|
|
}
|
|
|
|
// MustGetStringSlice returns a []string flag or fatals if an error occurs.
|
|
// Deprecated: just handle the error properly, this breaks command testing
|
|
func MustGetStringSlice(cmd *cobra.Command, name string) []string {
|
|
ss, err := cmd.Flags().GetStringSlice(name)
|
|
if err != nil {
|
|
cmdx.Fatalf(err.Error())
|
|
}
|
|
return ss
|
|
}
|
|
|
|
// MustGetStringArray returns a []string flag or fatals if an error occurs.
|
|
// Deprecated: just handle the error properly, this breaks command testing
|
|
func MustGetStringArray(cmd *cobra.Command, name string) []string {
|
|
ss, err := cmd.Flags().GetStringArray(name)
|
|
if err != nil {
|
|
cmdx.Fatalf(err.Error())
|
|
}
|
|
return ss
|
|
}
|
|
|
|
// MustGetStringToStringMap returns a map[string]string flag or fatals if an error occurs.
|
|
// Deprecated: just handle the error properly, this breaks command testing
|
|
func MustGetStringToStringMap(cmd *cobra.Command, name string) map[string]string {
|
|
ss, err := cmd.Flags().GetStringToString(name)
|
|
if err != nil {
|
|
cmdx.Fatalf(err.Error())
|
|
}
|
|
return ss
|
|
}
|
|
|
|
// MustGetInt returns a int flag or fatals if an error occurs.
|
|
// Deprecated: just handle the error properly, this breaks command testing
|
|
func MustGetInt(cmd *cobra.Command, name string) int {
|
|
ss, err := cmd.Flags().GetInt(name)
|
|
if err != nil {
|
|
cmdx.Fatalf(err.Error())
|
|
}
|
|
return ss
|
|
}
|
|
|
|
// MustGetUint8 returns a uint8 flag or fatals if an error occurs.
|
|
// Deprecated: just handle the error properly, this breaks command testing
|
|
func MustGetUint8(cmd *cobra.Command, name string) uint8 {
|
|
v, err := cmd.Flags().GetUint8(name)
|
|
if err != nil {
|
|
cmdx.Fatalf(err.Error())
|
|
}
|
|
return v
|
|
}
|
|
|
|
// MustGetUint32 returns a uint32 flag or fatals if an error occurs.
|
|
// Deprecated: just handle the error properly, this breaks command testing
|
|
func MustGetUint32(cmd *cobra.Command, name string) uint32 {
|
|
v, err := cmd.Flags().GetUint32(name)
|
|
if err != nil {
|
|
cmdx.Fatalf(err.Error())
|
|
}
|
|
return v
|
|
}
|