github.com/hernad/nomad@v1.6.112/command/recommendation_dismiss.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/mitchellh/cli" 11 "github.com/posener/complete" 12 13 "github.com/hernad/nomad/api/contexts" 14 ) 15 16 // Ensure RecommendationDismissCommand satisfies the cli.Command interface. 17 var _ cli.Command = &RecommendationDismissCommand{} 18 19 // RecommendationAutocompleteCommand provides AutocompleteArgs for all 20 // recommendation commands that support prefix-search autocompletion 21 type RecommendationAutocompleteCommand struct { 22 Meta 23 } 24 25 func (r *RecommendationAutocompleteCommand) AutocompleteArgs() complete.Predictor { 26 return complete.PredictFunc(func(a complete.Args) []string { 27 client, err := r.Meta.Client() 28 if err != nil { 29 return nil 30 } 31 32 resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Recommendations, nil) 33 if err != nil { 34 return []string{} 35 } 36 return resp.Matches[contexts.Recommendations] 37 }) 38 } 39 40 // RecommendationDismissCommand implements cli.Command. 41 type RecommendationDismissCommand struct { 42 RecommendationAutocompleteCommand 43 } 44 45 // Help satisfies the cli.Command Help function. 46 func (r *RecommendationDismissCommand) Help() string { 47 helpText := ` 48 Usage: nomad recommendation dismiss [options] <recommendation_ids> 49 50 Dismiss one or more Nomad recommendations. 51 52 When ACLs are enabled, this command requires a token with the 'submit-job', 53 'read-job', and 'submit-recommendation' capabilities for the 54 recommendation's namespace. 55 56 General Options: 57 58 ` + generalOptionsUsage(usageOptsDefault) 59 return strings.TrimSpace(helpText) 60 } 61 62 // Synopsis satisfies the cli.Command Synopsis function. 63 func (r *RecommendationDismissCommand) Synopsis() string { 64 return "Dismiss one or more Nomad recommendations" 65 } 66 67 func (r *RecommendationDismissCommand) AutocompleteFlags() complete.Flags { 68 return mergeAutocompleteFlags(r.Meta.AutocompleteFlags(FlagSetClient), 69 complete.Flags{}) 70 } 71 72 // Name returns the name of this command. 73 func (r *RecommendationDismissCommand) Name() string { return "recommendation dismiss" } 74 75 // Run satisfies the cli.Command Run function. 76 func (r *RecommendationDismissCommand) Run(args []string) int { 77 78 flags := r.Meta.FlagSet(r.Name(), FlagSetClient) 79 flags.Usage = func() { r.Ui.Output(r.Help()) } 80 if err := flags.Parse(args); err != nil { 81 return 1 82 } 83 84 if args = flags.Args(); len(args) < 1 { 85 r.Ui.Error("This command takes at least one argument: <recommendation_id>") 86 r.Ui.Error(commandErrorText(r)) 87 return 1 88 } 89 90 // Get the HTTP client. 91 client, err := r.Meta.Client() 92 if err != nil { 93 r.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 94 return 1 95 } 96 97 // Create a list of recommendations to dismiss. 98 ids := make([]string, len(args)) 99 copy(ids, args) 100 101 _, err = client.Recommendations().Delete(ids, nil) 102 if err != nil { 103 r.Ui.Error(fmt.Sprintf("Error dismissing recommendations: %v", err)) 104 return 1 105 } 106 107 verb := "recommendation" 108 if len(ids) > 1 { 109 verb += "s" 110 } 111 r.Ui.Output(fmt.Sprintf("Successfully dismissed %s", verb)) 112 return 0 113 }