Skip to content

Commit

Permalink
Merge pull request #1 from mvazquezc/main
Browse files Browse the repository at this point in the history
Migrating cli from Red Hat owned repo to Dddosify
  • Loading branch information
fatihbaltaci authored Mar 28, 2023
2 parents a539ad2 + 1b9e1e2 commit 1b2ead2
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 1 deletion.
71 changes: 71 additions & 0 deletions .github/workflows/go-binary-build-and-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Go App Build
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.19

- name: Get Dependencies
working-directory: ./
run: go mod tidy

- name: Run Build
working-directory: ./
run: |
echo "Create output dir"
mkdir -p ./out/
echo "Building Linux amd64 binary"
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./out/ddosify-latencies-linux-amd64
echo "Building Linux arm64 binary"
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o ./out/ddosify-latencies-linux-arm64
echo "Building Darwin amd64 binary"
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o ./out/ddosify-latencies-darwin-amd64
echo "Building Darwin arm64 binary"
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o ./out/ddosify-latencies-darwin-arm64
echo "Building Windows amd64 binary"
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o ./out/ddosify-latencies-windows-amd64.exe
- name: Save CLI Binaries Artifacts
uses: actions/upload-artifact@v3
with:
name: ddosify-latencies-binaries
path: ./out/

release:
name: Creates a new release with the resulting binaries
needs: [build]
runs-on: ubuntu-20.04
strategy:
matrix:
include:
- asset_name: ddosify-latencies-linux-amd64
- asset_name: ddosify-latencies-linux-arm64
- asset_name: ddosify-latencies-darwin-amd64
- asset_name: ddosify-latencies-darwin-arm64
- asset_name: ddosify-latencies-windows-amd64.exe
steps:
- name: Pull the cli binaries from Artifacts
uses: actions/download-artifact@v3
with:
path: /tmp/

- name: Upload binary to release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: /tmp/ddosify-latencies-binaries/${{ matrix.asset_name }}
asset_name: ${{ matrix.asset_name }}
tag: ${{ github.ref }}-release
overwrite: true
body: "Automatic release created by a GitHub Action"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

out/

# Dependency directories (remove the comment below to include it)
# vendor/
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
# ddosify-latency-cli
# Ddosify latencies CLI tool

The CLI tool interacts with the [ddosify latency API](https://docs.ddosify.com/cloud/api/latency-testing-api). The CLI usage can be found below:

~~~sh
Usage:
ddosify-latencies run [flags]

Flags:
-h, --help help for run
-i, --interval string The amount of waiting time between runs. (default "1m")
-l, --locations stringArray The array of locations to be requested. e.g: NA.US.*,NA.EU.* (default [EU.ES.*])
-o, --output-format string Output in an specific format. Usage: '-o [ table | yaml | json ]' (default "table")
--output-locations int The number of best locations to output. (default 1)
-r, --runs int The number of executions. (default 1)
-t, --target-url string The target url. e.g: https://google.com
~~~

An example output using `table` output:

~~~sh
┌─────────────┬─────────────────┐
│ LOCATION │ AVERAGE LATENCY │
├─────────────┼─────────────────┤
│ NA.US.TX.DA │ 9.000000 │
│ NA.US.TX.SA │ 11.000000 │
│ NA.US.NV.LV │ 13.000000 │
└─────────────┴─────────────────┘
~~~

An example output using `json` output:

~~~sh
{
"result": [
{
"location": "NA.US.TX.HO",
"avgLatency": 3
},
{
"location": "NA.US.VA.AS",
"avgLatency": 4
},
{
"location": "NA.US.TX.DA",
"avgLatency": 4
}
]
}
~~~

An example output using `yaml` output:

~~~sh
result:
- location: NA.US.VA.AS
avglatency: 4
- location: NA.US.TX.DA
avglatency: 4
- location: NA.US.TX.HO
avglatency: 4
~~~
73 changes: 73 additions & 0 deletions cli/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cli

import (
"errors"

"github.com/ddosify/ddosify-api-lib/pkg/ddosify"
"github.com/spf13/cobra"
)

var (
targetURL string
numberOfRuns int
waitInterval string
locations []string
outputLocationsNumber int
outputFormat string
)

func addExecFlags(cmd *cobra.Command) {

flags := cmd.Flags()
flags.StringVarP(&targetURL, "target-url", "t", "", "The target url. e.g: https://google.com")
flags.IntVarP(&numberOfRuns, "runs", "r", 1, "The number of executions.")
flags.StringVarP(&waitInterval, "interval", "i", "1m", "The amount of waiting time between runs.")
flags.StringArrayVarP(&locations, "locations", "l", []string{"EU.ES.*"}, "The array of locations to be requested. e.g: NA.US.*,NA.EU.*")
flags.IntVar(&outputLocationsNumber, "output-locations", 1, "The number of best locations to output.")
flags.StringVarP(&outputFormat, "output-format", "o", "table", "Output in an specific format. Usage: '-o [ table | yaml | json ]'")
cmd.MarkFlagRequired("target-url")
}

func NewExecCommand() *cobra.Command {

cmd := &cobra.Command{
Use: "run",
Short: "Exec the run command",
RunE: func(cmd *cobra.Command, args []string) error {
// Validate command Args
err := validateCommandArgs()
if err != nil {
return err
}
// Get waitIntervalInSeconds
waitIntervalSeconds := ddosify.IntervalTimeToSeconds(waitInterval)
lc := ddosify.NewLatencyChecker(ddosify.GetEnv("DDOSIFY_X_API_KEY", "NOT_SET"), targetURL, numberOfRuns, waitIntervalSeconds, locations, outputLocationsNumber)
res, err := lc.RunCommandExec()
switch {
case outputFormat == "yaml":
writeOutputYaml(res)
case outputFormat == "json":
writeOutputJson(res)
default:
writeOutputTable(res)
}
return err
},
}
addExecFlags(cmd)
return cmd
}

// validateCommandArgs validates that arguments passed by the user are valid
func validateCommandArgs() error {
validInterval := ddosify.ValidateIntervalTime(waitInterval)
if !validInterval {
return errors.New(" not valid interval")
}
// Validate URL is valid
validURL := ddosify.ValidateURL(targetURL)
if !validURL {
return errors.New(" not valid url")
}
return nil
}
38 changes: 38 additions & 0 deletions cli/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cli

import (
"encoding/json"
"fmt"
color "github.com/TwiN/go-color"
"github.com/ddosify/ddosify-api-lib/pkg/ddosify"
"github.com/jedib0t/go-pretty/v6/table"
"gopkg.in/yaml.v3"
"os"
)

func writeOutputTable(lcol ddosify.LatencyCheckerOutputList) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{color.InWhite("Location"), color.InWhite("Average Latency")})
if len(lcol.Result) > 0 {
for i := range lcol.Result {
if i == 0 {
t.AppendRow([]interface{}{color.InGreen(lcol.Result[i].Location), color.InGreen(fmt.Sprintf("%f", lcol.Result[i].AvgLatency))})
continue
}
t.AppendRow([]interface{}{color.InWhite(lcol.Result[i].Location), color.InWhite(fmt.Sprintf("%f", lcol.Result[i].AvgLatency))})
}
}
t.SetStyle(table.StyleLight)
t.Render()
}

func writeOutputJson(lcol ddosify.LatencyCheckerOutputList) {
o, _ := json.MarshalIndent(lcol, "", " ")
fmt.Println(string(o))
}

func writeOutputYaml(lcol ddosify.LatencyCheckerOutputList) {
o, _ := yaml.Marshal(lcol)
fmt.Println(string(o))
}
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module github.com/ddosify/ddosify-latency-cli

go 1.19

require (
github.com/TwiN/go-color v1.4.0
github.com/ddosify/ddosify-api-lib v0.0.0-20230328153039-4edc32ab0c9a
github.com/jedib0t/go-pretty/v6 v6.4.6
github.com/spf13/cobra v1.6.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/adhocore/gronx v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.1.0 // indirect
)
33 changes: 33 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"github.com/ddosify/ddosify-latency-cli/cli"
color "github.com/TwiN/go-color"
"log"
"os"

"github.com/spf13/cobra"
)

func main() {
command := newCommand()
if err := command.Execute(); err != nil {
log.Fatalf(color.InRed("[ERROR]")+"%s", err.Error())
}

}

func newCommand() *cobra.Command {
c := &cobra.Command{
Use: "ddosify-latencies",
Short: "ddosify-latencies is the command line interface to work with the ddosify latencies API",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
os.Exit(1)
},
}

c.AddCommand(cli.NewExecCommand())

return c
}

0 comments on commit 1b2ead2

Please sign in to comment.