Skip to content

Commit ff6ebda

Browse files
torsmcopybara-github
authored andcommitted
Add storefile and deletefile commands to admin cli tool
PiperOrigin-RevId: 792570340
1 parent 745f2ab commit ff6ebda

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

  • fleetspeak/src/admin/cli

fleetspeak/src/admin/cli/cli.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ func Usage() {
5050
" %s listcontacts <client_id> [limit]\n"+
5151
" %s analysehistory <client_id>\n"+
5252
" %s blacklistclients [<client_id>...]\n"+
53-
"\n", n, n, n, n)
53+
" %s storefile <service_name> <file_name> <file_path>\n"+
54+
" %s deletefile <service_name> <file_name>\n"+
55+
"\n", n, n, n, n, n, n)
5456
}
5557

5658
// Execute examines command line flags and executes one of the standard command line
@@ -74,6 +76,10 @@ func Execute(conn *grpc.ClientConn, args ...string) {
7476
AnalyseHistory(admin, args[1:]...)
7577
case "blacklistclients":
7678
BlacklistClients(admin, args[1:]...)
79+
case "storefile":
80+
StoreFile(admin, args[1:]...)
81+
case "deletefile":
82+
DeleteFile(admin, args[1:]...)
7783
default:
7884
fmt.Fprintf(os.Stderr, "Unknown command: %v\n", args[0])
7985
Usage()
@@ -250,3 +256,55 @@ func BlacklistClients(c sgrpc.AdminClient, args ...string) {
250256
}
251257
}
252258
}
259+
260+
// StoreFile reads a file from disk and sends it to the server's file store.
261+
// args[0] must be a service name, args[1] the file name to store as, and
262+
// args[2] the path to the file on disk.
263+
func StoreFile(c sgrpc.AdminClient, args ...string) {
264+
if len(args) != 3 {
265+
Usage()
266+
os.Exit(1)
267+
}
268+
serviceName := args[0]
269+
fileName := args[1]
270+
filePath := args[2]
271+
272+
data, err := os.ReadFile(filePath)
273+
if err != nil {
274+
log.Exitf("Failed to read file [%s]: %v", filePath, err)
275+
}
276+
277+
req := &spb.StoreFileRequest{
278+
ServiceName: serviceName,
279+
FileName: fileName,
280+
Data: data,
281+
}
282+
283+
ctx := context.Background()
284+
if _, err := c.StoreFile(ctx, req); err != nil {
285+
log.Exitf("StoreFile RPC failed: %v", err)
286+
}
287+
fmt.Printf("Stored file %s/%s to server.\n", serviceName, fileName)
288+
}
289+
290+
// DeleteFile tells the server to delete a file from its file store.
291+
// args[0] must be a service name, args[1] the file name to delete.
292+
func DeleteFile(c sgrpc.AdminClient, args ...string) {
293+
if len(args) != 2 {
294+
Usage()
295+
os.Exit(1)
296+
}
297+
serviceName := args[0]
298+
fileName := args[1]
299+
300+
req := &spb.DeleteFileRequest{
301+
ServiceName: serviceName,
302+
FileName: fileName,
303+
}
304+
305+
ctx := context.Background()
306+
if _, err := c.DeleteFile(ctx, req); err != nil {
307+
log.Exitf("DeleteFile RPC failed: %v", err)
308+
}
309+
fmt.Printf("Deleted file %s/%s from server.\n", serviceName, fileName)
310+
}

0 commit comments

Comments
 (0)