Skip to content

Commit

Permalink
initializing
Browse files Browse the repository at this point in the history
  • Loading branch information
fastgh committed Sep 8, 2020
1 parent b8fbf28 commit 8c2810c
Show file tree
Hide file tree
Showing 11 changed files with 564 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

/target/
/fgit/
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# fgit
GITHUB client wrapper, for Chinese developers, with fast access via transparent proxy

## Usage:
Download the executable binary to $PATH. For ex., for Mac OSX,

```shell
curl -L https://github.com/fastgh/fgit/releases/download/v0.9.0/fgit.darwin -o /usr/local/bin/fgit
chmod +x /usr/local/bin/fgit
```
2 changes: 2 additions & 0 deletions RELEASE_HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# v0.9.0
First release
22 changes: 22 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

set -e

PROJECT_DIR=$(cd "$(dirname $0)";pwd)
TARGET_DIR=${PROJECT_DIR}/target

rm -rf ${TARGET_DIR}
cd ${PROJECT_DIR}

go_build() {
local _OS=$1
local _PREFIX=$2
local _OS_TARGET_DIR=${TARGET_DIR}/${_OS}

mkdir -p ${_OS_TARGET_DIR}
GOOS=${_OS} GOARCH=amd64 go build -o ${_OS_TARGET_DIR}/fgit${_PREFIX}
}

go_build linux .linux
go_build darwin .darwin
go_build windows .exe
139 changes: 139 additions & 0 deletions cmdline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package main

import (
"encoding/json"
"os"

"net/url"

"github.com/gookit/color"
"github.com/pkg/errors"
)

const (
// AppVersion ...
AppVersion = "0.9.0"
)

// CommandLineT ...
type CommandLineT struct {
GitCommand string
GitRemoteName string
IsGitClone bool
GitURLText string
ArgIndexOfGitURLText int
PerhapsNeedProxyOrMirror bool
IsPrivate *bool
Args []string
}

// CommandLine ...
type CommandLine = *CommandLineT

// PrintHelp ...
func PrintHelp(errorMode bool) {
var c color.Color
if errorMode {
c = color.Red
} else {
c = color.Blue
}
c.Printf("fgit: a fastgithub git client, version %s\n", AppVersion)
}

func (me CommandLine) String() string {
json, err := json.MarshalIndent(me, "", " ")
if err != nil {
panic(errors.Wrapf(err, "failed to json mashal"))
}
return string(json)
}

// ParseCommandLine ...
func ParseCommandLine() CommandLine {
valueTrue := true
valueFalse := false

r := &CommandLineT{
GitCommand: "",
GitRemoteName: "",
IsGitClone: false,
GitURLText: "",
ArgIndexOfGitURLText: -1,
PerhapsNeedProxyOrMirror: false,
IsPrivate: &valueFalse,
Args: []string{},
}

hasCmd := false

for i := 1; i < len(os.Args); i++ {
arg := os.Args[i]

if arg == "--private" {
r.IsPrivate = &valueTrue
continue
}
if arg == "--debug" {
Debug = true
continue
}
if arg == "--mock" {
Mock = true
continue
}

r.Args = append(r.Args, arg)

if arg[0:1] == "-" {
continue
}

if hasCmd {
continue
}
hasCmd = true
r.GitCommand = arg

if r.GitCommand == "clone" {
r.IsGitClone = true
r.PerhapsNeedProxyOrMirror = true

if i < len(os.Args)-1 {
r.ArgIndexOfGitURLText = i + 1
r.GitURLText = os.Args[r.ArgIndexOfGitURLText]
}
} else if r.GitCommand == "pull" || r.GitCommand == "push" || r.GitCommand == "fetch" {
r.PerhapsNeedProxyOrMirror = true
if i < len(os.Args)-1 {
r.GitRemoteName = os.Args[i+1]
}
if r.GitCommand == "push" {
r.IsPrivate = &valueTrue
}
}
}

return r
}

// InstrumentURLwithMirror ...
func InstrumentURLwithMirror(gitURLText string, mirrorURLText string) string {
var err error

var mirrorURL *url.URL
if mirrorURL, err = url.Parse(mirrorURLText); err != nil {
panic(errors.Wrapf(err, "failed to parse url: %s", mirrorURLText))
}

var gitURL *url.URL
if gitURL, err = url.Parse(gitURLText); err != nil {
panic(errors.Wrapf(err, "failed to parse url: %s", gitURLText))
}

gitURL.Scheme = mirrorURL.Scheme
gitURL.Host = mirrorURL.Host
gitURL.User = mirrorURL.User

return gitURL.String()
}
62 changes: 62 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"encoding/json"
"path/filepath"

"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
)

// ConfigT ...
type ConfigT struct {
AccountID string `json:"account-id"`
PublicMirror string `json:"public-mirror"`
PrivateProxy string `json:"private-proxy"`
}

// Config ...
type Config = *ConfigT

// LoadConfig ...
func LoadConfig() Config {
path := GetConfigJSONFilePath()
if !FileExists(path) {
SaveConfigJSONFile(path, &ConfigT{})
}
return ConfigWithJSONFile(path)
}

// ConfigWithJSONFile ...
func ConfigWithJSONFile(path string) Config {
jsonText := string(ReadFile(path))
return ConfigWithJSON(jsonText)
}

// SaveConfigJSONFile ...
func SaveConfigJSONFile(path string, config Config) {
jsonText, err := json.Marshal(config)
if err != nil {
panic(errors.Wrapf(err, "failed to marshal json: %v\n", jsonText))
}
WriteFile(path, []byte(jsonText))
}

// ConfigWithJSON ...
func ConfigWithJSON(jsonText string) Config {
r := &ConfigT{}
if err := json.Unmarshal([]byte(jsonText), &r); err != nil {
panic(errors.Wrapf(err, "failed to unmarshal json: %s\n"+jsonText))
}
return r
}

// GetConfigJSONFilePath return (file path)
func GetConfigJSONFilePath() string {
dir, err := homedir.Dir()
if err != nil {
panic(errors.Wrapf(err, "failed to get home dir"))
}

return filepath.Join(dir, ".fgit.json")
}
118 changes: 118 additions & 0 deletions git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"fmt"
"net/url"
"os"
"os/exec"
"strings"

"github.com/pkg/errors"
)

// ResolveGitURLText ...
func ResolveGitURLText(gitURLText string, remoteName string, isGitClone bool) string {
if len(gitURLText) == 0 {
if !isGitClone {
gitURLText = GetGitRemoteURL(remoteName)
}
}

if len(gitURLText) == 0 {
panic(errors.New("cannot resolve git url"))
}

return gitURLText
}

// ResolveGitURL ...
func ResolveGitURL(gitURLText string) *url.URL {
var err error
var r *url.URL

if r, err = url.Parse(gitURLText); err != nil {
panic(errors.Wrapf(err, "failed to parse url: %s", gitURLText))
}

if strings.ToLower(r.Scheme) != "https" {
panic(fmt.Errorf("only https is supported but got: %s", r.Scheme))
}

return r

}

// ConfigGitHTTPProxy ...
func ConfigGitHTTPProxy(global bool, newHTTPProxy, newHTTPSProxy string) (oldHTTPProxy, oldHTTPSProxy string) {
var scope string
if global {
scope = "--global"
} else {
scope = "--local"
}

ExecGit([]string{"config", scope, "--set", "http.proxy", newHTTPProxy})
ExecGit([]string{"config", scope, "--set", "https.proxy", newHTTPSProxy})

oldHTTPProxy = ExecGit([]string{"config", scope, "--get", "http.proxy"})
oldHTTPSProxy = ExecGit([]string{"config", scope, "--get", "https.proxy"})

return
}

// ResetGitHTTPProxy ...
func ResetGitHTTPProxy(global bool, oldHTTPProxy, oldHTTPSProxy string) {
var scope string
if global {
scope = "--global"
} else {
scope = "--local"
}

if len(oldHTTPProxy) > 0 {
ExecGit([]string{"config", scope, "--set", "http.proxy", oldHTTPProxy})
} else {
ExecGit([]string{"config", scope, "--unset-all", "http.proxy"})
}

if len(oldHTTPSProxy) > 0 {
ExecGit([]string{"config", scope, "--set", "https.proxy", oldHTTPSProxy})
} else {
ExecGit([]string{"config", scope, "--unset-all", "https.proxy"})
}
}

// GetGitRemoteURL ...
func GetGitRemoteURL(remoteName string) string {
return ExecGit([]string{"remote", "get-url", "origin"})
}

// SetGitRemoteURL ...
func SetGitRemoteURL(remoteName string, remoteURL string) {
ExecGit([]string{"remote", "set-url", "origin", remoteURL})
}

// ExecGit ...
func ExecGit(args []string) string {
if Debug {
fmt.Println("git " + strings.Join(args, " "))
}

if Mock {
fmt.Println("mocking run: git " + strings.Join(args, " "))
return ""
}

var command = exec.Command("git", args...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
var err = command.Start()
if err != nil {
return err.Error()
}
err = command.Wait()
if err != nil {
return err.Error()
}
return ""
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/fastgh/fgit

go 1.13

require (
github.com/gookit/color v1.2.7
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gookit/color v1.2.6/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg=
github.com/gookit/color v1.2.7 h1:4qePMNWZhrmbfYJDix+J4V2l0iVW+6jQGjicELlN14E=
github.com/gookit/color v1.2.7/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg=
github.com/gookit/goutil v0.2.12/go.mod h1:3rjyvZP1PJiyZgC4eN0xu0bdLqeBbF5+pM4XlrimmtA=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Loading

0 comments on commit 8c2810c

Please sign in to comment.