Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
[PR] Add debug function (#3)
Browse files Browse the repository at this point in the history
* [debug] add debugConnection(url)

* [flag] add debug

* [debug] add debug logic
  • Loading branch information
KevinZonda authored May 23, 2020
1 parent 2c81d11 commit 9978cdf
Showing 1 changed file with 91 additions and 4 deletions.
95 changes: 91 additions & 4 deletions fgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,77 @@ import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"path"
"strings"
)

func debugConnection(url string) bool {
fmt.Print("Test connection...")
response, err := http.Head(url)
if err != nil {
fmt.Println("Failed")
fmt.Println("Response create failed\n", err)
return false
}
if response.StatusCode != http.StatusOK {
fmt.Println("Failed")
return false
} else {
fmt.Println("Success")
return true
}
}

func debug(url string) bool {
if url != "--help" {
fmt.Println("" +
"FastGit Debug Tool\n" +
"==================\n" +
"Remote Address:", url)
fmt.Print("IP Address: ")
addr, err := net.LookupIP(strings.Replace(strings.Replace(url, "https://", "", -1), "http://", "", -1))
if err != nil {
fmt.Println("Unknown")
} else {
fmt.Println(addr)
}

fmt.Print("Local Address: ")
resp, err := http.Get("https://api.ip.sb/ip")
defer resp.Body.Close()
if err != nil {
fmt.Println("Unknown -> ", err)
} else {
s, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Unknown -> ", err)
} else {
fmt.Printf("[%s]\n", strings.Replace(string(s), "\n", "", -1))
}
}

return debugConnection(url)
} else {
fmt.Println("" +
"FastGit Debug Command Line Tool\n" +
"===============================\n" +
"SYNTAX\n" +
" fgit debug [URL<string>] [--help]\n" +
"REMARKS\n" +
" URL is an optional parameter\n" +
" We debug https://hub.fastgit.org by default\n" +
" If you want to debug another URL, enter URL param\n" +
"EXAMPLE\n" +
" fgit debug\n" +
" fgit debug https://fastgit.org")
return true
}
}

func convertToFastGit() bool {
return convertHelper("https://github.com", "https://hub.fastgit.org")
}
Expand Down Expand Up @@ -48,18 +113,40 @@ func checkErr(err error, msg string, exitCode int) {
}

func main() {
if len(os.Args) == 1 {
if len(os.Args) == 1 || (len(os.Args) == 2 && os.Args[1] == "--help") {
fmt.Println("" +
"FastGit Command Line Tool\n" +
"=========================\n" +
"We will convert GitHub to FastGit automatically\n" +
"Do everything like git\n" +
"Build by KevinZonda with GoLang")
"REMARKS\n" +
" We will convert GitHub to FastGit automatically\n" +
" Do everything like git\n" +
" Build by KevinZonda with GoLang\n" +
"EXTRA-SYNTAX\n" +
" fgit debug [URL<string>] [--help]\n" +
" If you wan to known more about extra-syntax, try to use --help")
os.Exit(0)
}

isConvertToFastGit := false
isPush := false

if os.Args[1] == "debug" {
var isConnectOk bool
switch len(os.Args) {
case 2:
isConnectOk = debug("https://hub.fastgit.org")
case 3:
isConnectOk = debug(os.Args[2])
default:
fmt.Println("Invalid args for debug. If help wanted, use --help arg.")
}
if isConnectOk {
os.Exit(0)
} else {
os.Exit(1)
}
}

for i := range os.Args {
if os.Args[i] == "push" {
isPush = true
Expand Down

0 comments on commit 9978cdf

Please sign in to comment.