Skip to content

Commit

Permalink
Add a verbose flag which prints more updates while executing. (#26)
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
grundleborg authored Oct 22, 2021
1 parent 0d97ec9 commit 03ea2a5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/fetch_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func fetchAttachments(cmd *cobra.Command, args []string) error {

// Run through all the files in the input archive.
for _, file := range r.File {
verbosePrintln(fmt.Sprintf("Processing file: %s\n", file.Name))

// Open the file from the input archive.
inReader, err := file.Open()
Expand Down Expand Up @@ -98,6 +99,7 @@ func fetchAttachments(cmd *cobra.Command, args []string) error {
}

func processChannelFile(w *zip.Writer, file *zip.File, inBuf []byte, token string) error {
verbosePrintln("This is a 'channels' file. Examining it's contents for attachments.")

// Parse the JSON of the file.
var posts []SlackPost
Expand Down Expand Up @@ -152,6 +154,8 @@ func processChannelFile(w *zip.Writer, file *zip.File, inBuf []byte, token strin
continue
}

verbosePrintln(fmt.Sprintf("Downloading file %s (%s)", file.Id, file.Name))

// Fetch the file.
req, err := http.NewRequest("GET", downloadUrl, nil)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions cmd/fetch_emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func fetchEmails(cmd *cobra.Command, args []string) error {

// Run through all the files in the input archive.
for _, file := range r.File {
verbosePrintln(fmt.Sprintf("Processing file: %s\n", file.Name))

// Open the file from the input archive.
inReader, err := file.Open()
if err != nil {
Expand Down Expand Up @@ -91,6 +93,8 @@ func fetchEmails(cmd *cobra.Command, args []string) error {
}

func processUsersJson(output io.Writer, input io.Reader, slackApiToken string) error {
verbosePrintln("Found users.json file.")

// We want to preserve all existing fields in JSON.
// By using interface{} (instead of struct), we can avoid describing all
// the fields (new ones might be added by Slack devs in the future!) at the cost of
Expand All @@ -110,6 +114,8 @@ func processUsersJson(output io.Writer, input io.Reader, slackApiToken string) e
return errors.New("Failed to find any users in users.json. Looks like something went wrong.")
}

verbosePrintln("Updating users.json contents with fetched emails.")

for _, user := range data {
// These 'ok's only check for type assertion success.
// Map access would return untyped nil,
Expand All @@ -136,6 +142,8 @@ func processUsersJson(output io.Writer, input io.Reader, slackApiToken string) e
}

func fetchUserEmails(token string) (map[string]string, error) {
verbosePrintln("Fetching emails from Slack API")

client := &http.Client{}
req, err := http.NewRequest("GET", "https://slack.com/api/users.list", nil)
if err != nil {
Expand Down Expand Up @@ -171,6 +179,8 @@ func fetchUserEmails(token string) (map[string]string, error) {
return nil, errors.New("Unexpected lack of ok=true in Slack API response. Is access token correct?")
}

verbosePrintln("Fetched emails from Slack API. Now building a map of them to process.")

res := make(map[string]string)
for _, user := range data.Members {
if user.Id != "" && user.Profile.Email != "" {
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
var (
inputArchive string
outputArchive string
verbose bool
)

var rootCmd = &cobra.Command{
Expand All @@ -21,6 +22,7 @@ Version: 0.4.0`,
func init() {
rootCmd.PersistentFlags().StringVarP(&inputArchive, "input-archive", "i", "", "the path to the Slack export archive which you wish to augment")
rootCmd.PersistentFlags().StringVarP(&outputArchive, "output-archive", "o", "", "the path to which you would like the output archive to be written")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "print detailed information about what is happening while the command is executing")
rootCmd.MarkPersistentFlagRequired("input-archive")
rootCmd.MarkPersistentFlagRequired("output-archive")
rootCmd.AddCommand(fetchAttachmentsCmd)
Expand Down
7 changes: 7 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cmd

func verbosePrintln(line string) {
if verbose {
println(line)
}
}

0 comments on commit 03ea2a5

Please sign in to comment.