Skip to content

Commit

Permalink
Improve error message if product set more than once
Browse files Browse the repository at this point in the history
On registration and deregistration the product flag (-p/--product)
is only supposed to be used once. But this is not enforced and the
error message from the server is not clear about what the user did
wrong. This patch enforces the one product rule at flag parse time
and prints a better message.

SUSE#171
  • Loading branch information
djoreilly committed May 10, 2023
1 parent e3c41e6 commit 31ef269
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions suseconnect/suseconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ func main() {
}
}

// singleStringFlag cannot be set more than once.
// e.g. `cmd -p abc -p def` will give a parse error.
type singleStringFlag struct {
value string
isSet bool
}

func (p *singleStringFlag) String() string {
return p.value
}

func (p *singleStringFlag) Set(value string) error {
if p.isSet {
return fmt.Errorf("this flag can only be specified once\n")
}
p.value = value
p.isSet = true
return nil
}

func connectMain() {
var (
status bool
Expand All @@ -46,7 +66,7 @@ func connectMain() {
fsRoot string
namespace string
token string
product string
product singleStringFlag
instanceDataFile string
listExtensions bool
email string
Expand Down Expand Up @@ -78,11 +98,11 @@ func connectMain() {
flag.StringVar(&namespace, "namespace", "", "")
flag.StringVar(&token, "regcode", "", "")
flag.StringVar(&token, "r", "", "")
flag.StringVar(&product, "product", "", "")
flag.StringVar(&product, "p", "", "")
flag.StringVar(&instanceDataFile, "instance-data", "", "")
flag.StringVar(&email, "email", "", "")
flag.StringVar(&email, "e", "", "")
flag.Var(&product, "product", "")
flag.Var(&product, "p", "")

flag.Parse()
if version {
Expand Down Expand Up @@ -121,8 +141,8 @@ func connectMain() {
if token != "" {
connect.CFG.Token = token
}
if product != "" {
if p, err := connect.SplitTriplet(product); err != nil {
if product.isSet {
if p, err := connect.SplitTriplet(product.value); err != nil {
fmt.Print("Please provide the product identifier in this format: ")
fmt.Print("<internal name>/<version>/<architecture>. You can find ")
fmt.Print("these values by calling: 'SUSEConnect --list-extensions'\n")
Expand Down Expand Up @@ -174,7 +194,7 @@ func connectMain() {
fmt.Print("Please use --instance-data only in combination ")
fmt.Print("with --url pointing to your RMT or SMT server\n")
os.Exit(1)
} else if connect.URLDefault() && token == "" && product == "" {
} else if connect.URLDefault() && token == "" && product.value == "" {
flag.Usage()
os.Exit(1)
} else if isSumaManaged() {
Expand Down

0 comments on commit 31ef269

Please sign in to comment.