|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/nullify/slack-cli/internal/output" |
| 9 | + "github.com/nullify/slack-cli/internal/slack" |
| 10 | + "github.com/nullify/slack-cli/internal/urlparse" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var fileCmd = &cobra.Command{ |
| 15 | + Use: "file", |
| 16 | + Short: "Inspect, upload, and download Slack file attachments", |
| 17 | +} |
| 18 | + |
| 19 | +var fileInfoCmd = &cobra.Command{ |
| 20 | + Use: "info <file-id>", |
| 21 | + Short: "Fetch metadata for a Slack file", |
| 22 | + Args: cobra.ExactArgs(1), |
| 23 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 24 | + client, err := getClient() |
| 25 | + if err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + info, err := slack.GetFileInfo(cmd.Context(), client, args[0]) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + return output.PrintJSON(info) |
| 33 | + }, |
| 34 | +} |
| 35 | + |
| 36 | +var fileDownloadCmd = &cobra.Command{ |
| 37 | + Use: "download <file-id>", |
| 38 | + Short: "Download a Slack file to stdout or --output <path>", |
| 39 | + Long: `Download a Slack file attachment using its file ID. |
| 40 | +
|
| 41 | +The file content is written to stdout by default, or to a file with --output. |
| 42 | +File metadata (name, mimetype, size) is printed to stderr so stdout stays clean for piping. |
| 43 | +
|
| 44 | +Examples: |
| 45 | + slack-cli file download F12345678 > image.png |
| 46 | + slack-cli file download F12345678 --output image.png`, |
| 47 | + Args: cobra.ExactArgs(1), |
| 48 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 49 | + outputPath, _ := cmd.Flags().GetString("output") |
| 50 | + |
| 51 | + client, err := getClient() |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + info, err := slack.GetFileInfo(cmd.Context(), client, args[0]) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + if info.URLPrivate == "" { |
| 61 | + return fmt.Errorf("file %s has no downloadable URL (it may be a snippet or external file)", args[0]) |
| 62 | + } |
| 63 | + |
| 64 | + fmt.Fprintf(os.Stderr, "downloading: %s (%s, %d bytes)\n", info.Name, info.Mimetype, info.Size) |
| 65 | + |
| 66 | + body, _, err := client.Download(cmd.Context(), info.URLPrivate) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + defer body.Close() |
| 71 | + |
| 72 | + var dst io.Writer = os.Stdout |
| 73 | + if outputPath != "" { |
| 74 | + f, err := os.Create(outputPath) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("creating output file: %w", err) |
| 77 | + } |
| 78 | + defer f.Close() |
| 79 | + dst = f |
| 80 | + } |
| 81 | + |
| 82 | + if _, err := io.Copy(dst, body); err != nil { |
| 83 | + return fmt.Errorf("writing file content: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + if outputPath != "" { |
| 87 | + fmt.Fprintf(os.Stderr, "saved to: %s\n", outputPath) |
| 88 | + } |
| 89 | + return nil |
| 90 | + }, |
| 91 | +} |
| 92 | + |
| 93 | +var fileUploadCmd = &cobra.Command{ |
| 94 | + Use: "upload <channel> <file-path>", |
| 95 | + Short: "Upload a file to a Slack channel or thread", |
| 96 | + Long: `Upload a local file to a Slack channel using the files.getUploadURLExternal API. |
| 97 | +
|
| 98 | +The channel can be a channel ID (C...), channel name (#general), or user ID for DMs. |
| 99 | +
|
| 100 | +Examples: |
| 101 | + slack-cli file upload C01234ABC ./report.pdf --message "Here is the report" |
| 102 | + slack-cli file upload "#general" ./image.png --thread-ts 1234567890.123456 |
| 103 | + slack-cli file upload C01234ABC ./data.csv --title "Q1 Data" --message "See attached"`, |
| 104 | + Args: cobra.ExactArgs(2), |
| 105 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 106 | + message, _ := cmd.Flags().GetString("message") |
| 107 | + threadTS, _ := cmd.Flags().GetString("thread-ts") |
| 108 | + title, _ := cmd.Flags().GetString("title") |
| 109 | + filename, _ := cmd.Flags().GetString("filename") |
| 110 | + |
| 111 | + client, err := getClient() |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + target := urlparse.ParseMsgTarget(args[0]) |
| 117 | + channelID, err := resolveTargetToChannel(cmd, client, target) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + fmt.Fprintf(os.Stderr, "uploading: %s\n", args[1]) |
| 123 | + |
| 124 | + info, err := slack.UploadFile(cmd.Context(), client, slack.UploadOpts{ |
| 125 | + FilePath: args[1], |
| 126 | + ChannelID: channelID, |
| 127 | + Filename: filename, |
| 128 | + Title: title, |
| 129 | + Message: message, |
| 130 | + ThreadTS: threadTS, |
| 131 | + }) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + return output.PrintJSON(info) |
| 137 | + }, |
| 138 | +} |
| 139 | + |
| 140 | +func init() { |
| 141 | + rootCmd.AddCommand(fileCmd) |
| 142 | + fileCmd.AddCommand(fileInfoCmd, fileDownloadCmd, fileUploadCmd) |
| 143 | + |
| 144 | + fileDownloadCmd.Flags().String("output", "", "Write file content to this path instead of stdout") |
| 145 | + |
| 146 | + fileUploadCmd.Flags().String("message", "", "Optional message to post alongside the file") |
| 147 | + fileUploadCmd.Flags().String("thread-ts", "", "Thread root ts to upload into a thread") |
| 148 | + fileUploadCmd.Flags().String("title", "", "Display title for the file (defaults to filename)") |
| 149 | + fileUploadCmd.Flags().String("filename", "", "Override the filename sent to Slack (defaults to basename of file-path)") |
| 150 | +} |
0 commit comments