Skip to content

Commit

Permalink
Improve follow command output (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
felicianotech committed Sep 15, 2022
1 parent ab79ee4 commit e5194c1
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions arc/cmd/follow.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -32,14 +34,47 @@ var followCmd = &cobra.Command{

reqURL := fmt.Sprintf("https://circleci.com/api/v1.1/project/gh/%s/%s/follow?circle-token=%s", org, repo, os.Getenv("CIRCLECI_TOKEN"))

resp, err := http.Post(reqURL, "application/json", nil)
req, err := http.NewRequest("POST", reqURL, nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
fmt.Printf("%s", body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

var followResp map[string]interface{}

err = json.Unmarshal(body, &followResp)
if err != nil {
return errors.New("Failed to unmarshal CircleCI's API reponse.")
}

if followResp["message"] != nil {

msg := followResp["message"].(string)

if strings.Contains(msg, "Not Found") {
fmt.Println("Error: Failed to follow project as it wasn't found.")
} else if strings.Contains(msg, "For security purposes") {
fmt.Println("Error: You don't have permission to follow project.")
} else {
fmt.Printf("Error: Failed to follow project. Here's what CircleCI's API returns: %s", msg)
}

return nil

}

fmt.Println("The project has been followed successfully.")

return nil
},
Expand Down

0 comments on commit e5194c1

Please sign in to comment.