-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0001d33
commit e53c265
Showing
14 changed files
with
268 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package license | ||
|
||
import ( | ||
"debricked/pkg/client" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
var debClient *client.DebClient | ||
|
||
var email string | ||
var commitHash string | ||
|
||
func NewLicenseCmd(debrickedClient *client.DebClient) *cobra.Command { | ||
debClient = debrickedClient | ||
cmd := &cobra.Command{ | ||
Use: "license", | ||
Short: "Generate license report", | ||
Long: `Generate license report from a commit hash. | ||
This is a premium feature. Please visit https://debricked.com/pricing/ for more info. | ||
The finished report will be sent to the specified email address.`, | ||
RunE: run, | ||
} | ||
|
||
cmd.Flags().StringVarP(&email, "email", "e", "", "The email address that the report will be sent to") | ||
_ = cmd.MarkFlagRequired("email") | ||
|
||
cmd.Flags().StringVarP(&commitHash, "commit", "c", "", "commit hash") | ||
_ = cmd.MarkFlagRequired("commit") | ||
|
||
return cmd | ||
} | ||
|
||
func run(_ *cobra.Command, _ []string) error { | ||
if err := report(); err != nil { | ||
return errors.New(fmt.Sprintf("%s %s\n", color.RedString("⨯"), err.Error())) | ||
} | ||
|
||
fmt.Printf("%s Successfully ordered license report\n", color.GreenString("✔")) | ||
|
||
return nil | ||
} | ||
|
||
func report() error { | ||
commitId, err := getCommitId(commitHash) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return orderReport(commitId) | ||
} | ||
|
||
func orderReport(commitId int) error { | ||
uri := fmt.Sprintf("/api/1.0/open/licenses/get-licenses?order=asc&sortColumn=name&generateExcel=1&commitId=%d&email=%s", commitId, email) | ||
res, err := debClient.Get(uri, "application/json") | ||
if err != nil { | ||
return err | ||
} | ||
if res.StatusCode == http.StatusForbidden { | ||
return errors.New("premium feature. Please visit https://debricked.com/pricing/ for more info") | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return errors.New(fmt.Sprintf("failed to order report. Status code: %d", res.StatusCode)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type commit struct { | ||
FileIds []int `json:"uploaded_programs_file_ids"` | ||
Id int `json:"id"` | ||
Name string `json:"name"` | ||
ReleaseData string `json:"release_date"` | ||
} | ||
|
||
func getCommitId(hash string) (int, error) { | ||
uri := fmt.Sprintf("/api/1.0/releases/by/name?name=%s", hash) | ||
res, err := debClient.Get(uri, "application/json") | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if res.StatusCode == http.StatusForbidden { | ||
return 0, errors.New("premium feature. Please visit https://debricked.com/pricing/ for more info") | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return 0, errors.New(fmt.Sprintf("No commit was found with the name %s", hash)) | ||
} | ||
|
||
body, err := io.ReadAll(res.Body) | ||
var commits []commit | ||
err = json.Unmarshal(body, &commits) | ||
if len(commits) == 0 { | ||
return 0, errors.New(fmt.Sprintf("No commit was found with the name %s", hash)) | ||
} | ||
|
||
return commits[0].Id, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package license | ||
|
||
import ( | ||
"debricked/pkg/client" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const validCommit = "84cac1be9931f8bcc8ef59c5544aaac8c5c97c8b" | ||
|
||
func TestNewLicenseCmd(t *testing.T) { | ||
cmd := NewLicenseCmd(client.NewDebClient(nil)) | ||
commands := cmd.Commands() | ||
nbrOfCommands := 0 | ||
if len(commands) != nbrOfCommands { | ||
t.Error(fmt.Sprintf("failed to assert that there were %d sub commands connected", nbrOfCommands)) | ||
} | ||
|
||
flags := cmd.Flags() | ||
flagAssertions := map[string]string{ | ||
"commit": "c", | ||
"email": "e", | ||
} | ||
for name, shorthand := range flagAssertions { | ||
flag := flags.Lookup(name) | ||
if flag == nil { | ||
t.Error(fmt.Sprintf("failed to assert that %s flag was set", name)) | ||
} | ||
if flag.Shorthand != shorthand { | ||
t.Error(fmt.Sprintf("failed to assert that %s flag shorthand %s was set correctly", name, shorthand)) | ||
} | ||
} | ||
} | ||
|
||
func TestRunUnAuthorized(t *testing.T) { | ||
email = "[email protected]" | ||
commitHash = validCommit | ||
accessToken := "invalid" | ||
debClient = client.NewDebClient(&accessToken) | ||
err := run(nil, nil) | ||
if err == nil { | ||
t.Fatal("failed to assert that an error occurred") | ||
} | ||
if !strings.Contains(err.Error(), "⨯ Unauthorized. Specify access token") { | ||
t.Error("failed to assert error message") | ||
} | ||
} | ||
|
||
func TestRun(t *testing.T) { | ||
email = "[email protected]" | ||
commitHash = validCommit | ||
debClient = client.NewDebClient(nil) | ||
err := run(nil, nil) | ||
if err != nil { | ||
t.Fatal("failed to assert that no error occurred") | ||
} | ||
} | ||
|
||
func TestReportInvalidCommitHash(t *testing.T) { | ||
email = "[email protected]" | ||
commitHash = "invalid" | ||
debClient = client.NewDebClient(nil) | ||
err := report() | ||
if err == nil { | ||
t.Fatal("failed to assert that error occurred") | ||
} | ||
if !strings.Contains(err.Error(), "No commit was found with the name invalid") { | ||
t.Error("failed to assert error message") | ||
} | ||
} | ||
|
||
func TestGetCommitId(t *testing.T) { | ||
debClient = client.NewDebClient(nil) | ||
id, err := getCommitId(validCommit) | ||
if err != nil { | ||
t.Fatal("failed to assert that no error occurred") | ||
} | ||
if id < 1 { | ||
t.Error("failed to assert that the commit ID was a positive integer") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
package report | ||
|
||
import ( | ||
"debricked/pkg/client" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestNewReportCmd(t *testing.T) { | ||
cmd := NewReportCmd(client.NewDebClient(nil)) | ||
commands := cmd.Commands() | ||
nbrOfCommands := 1 | ||
if len(commands) != nbrOfCommands { | ||
t.Error(fmt.Sprintf("failed to assert that there were %d sub commands connected", nbrOfCommands)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.