github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/recommendation_dismiss.go (about)

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