github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/authtoken/delete.go (about) 1 package authtoken 2 3 import ( 4 "bufio" 5 "fmt" 6 "io" 7 "os" 8 "path/filepath" 9 10 "github.com/fastly/go-fastly/v9/fastly" 11 12 "github.com/fastly/cli/pkg/argparser" 13 "github.com/fastly/cli/pkg/global" 14 "github.com/fastly/cli/pkg/text" 15 ) 16 17 // NewDeleteCommand returns a usable command registered under the parent. 18 func NewDeleteCommand(parent argparser.Registerer, g *global.Data) *DeleteCommand { 19 c := DeleteCommand{ 20 Base: argparser.Base{ 21 Globals: g, 22 }, 23 } 24 c.CmdClause = parent.Command("delete", "Revoke an API token").Alias("remove") 25 26 c.CmdClause.Flag("current", "Revoke the token used to authenticate the request").BoolVar(&c.current) 27 c.CmdClause.Flag("file", "Revoke tokens in bulk from a newline delimited list of tokens").StringVar(&c.file) 28 c.CmdClause.Flag("id", "Alphanumeric string identifying a token").StringVar(&c.id) 29 return &c 30 } 31 32 // DeleteCommand calls the Fastly API to delete an appropriate resource. 33 type DeleteCommand struct { 34 argparser.Base 35 36 current bool 37 file string 38 id string 39 } 40 41 // Exec invokes the application logic for the command. 42 func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error { 43 if !c.current && c.file == "" && c.id == "" { 44 return fmt.Errorf("error parsing arguments: must provide either the --current, --file or --id flag") 45 } 46 47 if c.current { 48 err := c.Globals.APIClient.DeleteTokenSelf() 49 if err != nil { 50 c.Globals.ErrLog.Add(err) 51 return err 52 } 53 54 text.Success(out, "Deleted current token") 55 return nil 56 } 57 58 if c.file != "" { 59 input, err := c.constructInputBatch() 60 if err != nil { 61 c.Globals.ErrLog.Add(err) 62 return err 63 } 64 65 err = c.Globals.APIClient.BatchDeleteTokens(input) 66 if err != nil { 67 c.Globals.ErrLog.Add(err) 68 return err 69 } 70 71 text.Success(out, "Deleted tokens") 72 if c.Globals.Verbose() { 73 text.Break(out) 74 c.printTokens(out, input.Tokens) 75 } 76 return nil 77 } 78 79 if c.id != "" { 80 input := c.constructInput() 81 82 err := c.Globals.APIClient.DeleteToken(input) 83 if err != nil { 84 c.Globals.ErrLog.Add(err) 85 return err 86 } 87 88 text.Success(out, "Deleted token '%s'", c.id) 89 return nil 90 } 91 92 return nil 93 } 94 95 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 96 func (c *DeleteCommand) constructInput() *fastly.DeleteTokenInput { 97 var input fastly.DeleteTokenInput 98 99 input.TokenID = c.id 100 101 return &input 102 } 103 104 // constructInputBatch transforms values parsed from CLI flags into an object to be used by the API client library. 105 func (c *DeleteCommand) constructInputBatch() (*fastly.BatchDeleteTokensInput, error) { 106 var ( 107 err error 108 file io.Reader 109 input fastly.BatchDeleteTokensInput 110 path string 111 tokens []*fastly.BatchToken 112 ) 113 114 if path, err = filepath.Abs(c.file); err == nil { 115 if _, err = os.Stat(path); err == nil { 116 if file, err = os.Open(path); err == nil /* #nosec */ { 117 scanner := bufio.NewScanner(file) 118 for scanner.Scan() { 119 tokens = append(tokens, &fastly.BatchToken{ID: scanner.Text()}) 120 } 121 err = scanner.Err() 122 } 123 } 124 } 125 126 input.Tokens = tokens 127 128 if err != nil { 129 return nil, err 130 } 131 132 return &input, nil 133 } 134 135 // printTokens displays the tokens provided by a user. 136 func (c *DeleteCommand) printTokens(out io.Writer, rs []*fastly.BatchToken) { 137 t := text.NewTable(out) 138 t.AddHeader("TOKEN ID") 139 for _, r := range rs { 140 t.AddLine(r.ID) 141 } 142 t.Print() 143 }