Skip to content

Commit

Permalink
dev: update some commands and cmd logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 14, 2024
1 parent fae2d34 commit a91fe3a
Show file tree
Hide file tree
Showing 19 changed files with 294 additions and 19 deletions.
4 changes: 4 additions & 0 deletions config/gitx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ git:
default_branch: main
# gitflow git fork mode for develop.
fork_mode: false
disable_https: false
source_remote: main
default_remote: origin
# pull request URL format template
pr_url_format: "http://{host}/{repo_path}/compare/{into_branch}...{from_repo_path}:{from_branch}"

# GitHub config, will extend common info from git.
github:
Expand All @@ -29,6 +32,7 @@ gitlab:
host_url: http://${GITLAB_HOST}
# gitflow git fork mode for develop.
fork_mode: true
disable_https: true
# the source remote name.
source_remote: ${GITLAB_SRC_REMOTE}
# the default upstream remote name. default is origin.
Expand Down
28 changes: 28 additions & 0 deletions data/json-server/demo1/json-server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"port": 13000,
"datafile": "db.json",
"routes": [
{
"type": "static",
"method": "GET",
"path": "/public/assets/*"
},
{
"method": "GET",
"path": "/api/v1/users",
"response": {
"status": 200,
"body": [
{
"id": 1,
"name": "John Doe"
},
{
"id": 2,
"name": "Jane Doe"
}
]
}
}
]
}
4 changes: 2 additions & 2 deletions internal/app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ func Glab() *gitlab.GitLab {
return Get[*gitlab.GitLab](ObjGlab)
}

// Ghub get
// Ghub config get
func Ghub() *github.GitHub {
return Get[*github.GitHub](ObjGhub)
}

// Gitx get
// Gitx config get
func Gitx() *gitx.Config {
return Get[*gitx.Config](ObjGit)
}
6 changes: 5 additions & 1 deletion internal/bootstrap/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bootstrap

import (
"github.com/gookit/gcli/v3"
"github.com/gookit/goutil/strutil/textutil"
"github.com/inhere/kite-go"
"github.com/inhere/kite-go/internal/app"
"github.com/inhere/kite-go/internal/cli"
Expand All @@ -15,7 +16,10 @@ func BootCli(_ *app.KiteApp) error {
a.Version = kite.Version
})
// some info
cliApp.Logo.Text = kite.Banner
cliApp.Logo.Text = textutil.RenderSMap(kite.Banner, map[string]string{
"buildDate": kite.BuildDate,
"goVersion": kite.GoVersion,
}, "{{,}}")
app.Add(app.ObjCli, cliApp)

// load commands
Expand Down
1 change: 1 addition & 0 deletions internal/cli/gitcmd/ghubcmd/ghub_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var GithubCmd = &gcli.Command{
Desc: "useful tools for use github",
Subs: []*gcli.Command{
DownloadAssetCmd,
gitcmd.NewPullRequestCmd(),
gitcmd.BatchCmd,
gitcmd.NewBranchCmd(),
gitcmd.NewCloneCmd(configProvider),
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/gitcmd/gitcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ var GitCommands = &gcli.Command{
RepoInfoCmd,
// StatusInfoCmd,
RemoteInfoCmd,
NewPullRequestCmd(),
NewCloneCmd(configProvider),
NewAddCommitPush(),
NewAddCommitCmd(),
NewUpdateCmd(),
NewUpdatePushCmd(),
NewOpenRemoteCmd(configProvider),
NewInitFlowCmd(),
CreatePRLink,
NewCheckoutCmd(),
ShowLogCmd,
ChangelogCmd,
Expand Down
6 changes: 0 additions & 6 deletions internal/cli/gitcmd/gitflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import (
"github.com/inhere/kite-go/internal/biz/cmdbiz"
)

var CreatePRLink = &gcli.Command{
Name: "pr",
Desc: "create pull request link for current project",
Aliases: []string{"pr-link"},
}

// NewInitFlowCmd instance
func NewInitFlowCmd() *gcli.Command {
var ifOpts = struct {
Expand Down
11 changes: 6 additions & 5 deletions internal/cli/gitcmd/glabcmd/merge_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ func mergeRequestHandle(c *gcli.Command, _ []string) (err error) {
gl := app.Glab()
glp := gitlab.NewGlProject(repoDir, gl)

if gl.HostUrl == "" {
c.Infoln("TIP: config gitlab.host_url is empty, try fetch from default remote")
gl.HostUrl = glp.ForkRmtInfo().HTTPHost()
hostUrl := gl.HostUrl
if glp.IsGitRepo() {
c.Infoln("TIP: in git repository, try fetch host_url from default remote")
hostUrl = glp.ForkRmtInfo().HTTPHost(gl.DisableHTTPS)
}

// http://my.gitlab.com/group/repo/merge_requests/new
Expand All @@ -84,7 +85,7 @@ func mergeRequestHandle(c *gcli.Command, _ []string) (err error) {
repoPath = glp.ForkRmtInfo().Path()
}

link := gl.HostUrl + "/" + repoPath + "/merge_requests/new"
link := hostUrl + "/" + repoPath + "/merge_requests/new"
return sysutil.OpenBrowser(link)
}

Expand Down Expand Up @@ -140,7 +141,7 @@ func mergeRequestHandle(c *gcli.Command, _ []string) (err error) {
show.AList("Merge Request Info", mrInfo)

// link := glp.MargeRequestURL(mrInfo)
link := mrInfo.BuildURL(gl.HostUrl)
link := mrInfo.BuildURL(hostUrl)
c.Warnln("Merge Request Link:")
c.Println(" ", link)

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/gitcmd/glabcmd/resolve_conflict.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var ResolveConflictCmd = &gcli.Command{

curBr := lr.CurBranchName()
br := c.Arg("branch").String()
br = lr.ResolveBranch(br)
br = lr.ResolveAlias(br)

rr := cmdutil.NewRunner(func(rr *cmdutil.Runner) {
rr.Workdir = rcOpts.Workdir
Expand Down
158 changes: 158 additions & 0 deletions internal/cli/gitcmd/pull_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package gitcmd

import (
"fmt"
"strings"

"github.com/gookit/gcli/v3"
"github.com/gookit/gcli/v3/show"
"github.com/gookit/gitw/gitutil"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/sysutil"
"github.com/inhere/kite-go/internal/app"
"github.com/inhere/kite-go/internal/biz/cmdbiz"
)

var (
mrOpts = struct {
cmdbiz.CommonOpts
new bool
// create pr link
direct bool
source string // source branch
target string // target branch
openBr string // same of target + openIt=true
// open link on browser
openIt bool
// auto search .git repo on parent dir
// findRepo bool
}{}
)

// NewPullRequestCmd command
func NewPullRequestCmd() *gcli.Command {
pr := &gcli.Command{
Name: "pull-request",
Aliases: []string{"pr", "mr", "merge-request"},
Desc: "Create new merge requests(PR/MR) by given project information",
Config: func(c *gcli.Command) {
mrOpts.BindCommonFlags(c)

c.BoolOpt(&mrOpts.new, "new", "", false,
"Open new PR page link on browser. eg: http://my.git.com/group/repo/merge_requests/new",
)
c.BoolOpt2(&mrOpts.direct, "direct,d", "The PR is direct from fork to main repository")
// c.BoolOpt2(&mrOpts.findRepo, "find-repo, find", "auto find repo .git dir on parent dirs")

c.StrOpt(&mrOpts.openBr, "open", "o", "Set target branch and open PR link on browser")
c.StrOpt(&mrOpts.source, "source", "s", "The source branch name, default is current `BRANCH`")
c.StrOpt(&mrOpts.target, "target", "t", "The target branch name, default is current `BRANCH`")
c.AddArg("repoPath", "The project name with path in self-host gitlab.\nif empty will fetch from workdir")
},
Help: `
Special:
@, h, HEAD - Current branch name.
@s - Source branch name.
@t - Target branch name.
`,
Examples: `
{$binWithCmd} Will generate PR link for fork 'HEAD_BRANCH' to main 'HEAD_BRANCH'
{$binWithCmd} -o h Will open PR link for fork 'HEAD_BRANCH' to main 'HEAD_BRANCH' on browser
{$binWithCmd} -o qa Will open PR link for main 'HEAD_BRANCH' to main 'qa' on browser
{$binWithCmd} -t qa Will generate PR link for main 'HEAD_BRANCH' to main 'qa'
{$binWithCmd} -t qa --direct Will generate PR link for fork 'HEAD_BRANCH' to main 'qa'
# Will generate PR link for 'group/repo', from 'dev' to 'qa' branch
{$binWithCmd} -o dev -t qa group/repo
`,
Func: mergeRequestHandle,
}

return pr
}

func mergeRequestHandle(c *gcli.Command, _ []string) (err error) {
repoDir := mrOpts.Workdir

gx := app.Gitx()
gp := gx.LoadRepo(repoDir)

hostUrl := gx.HostUrl
if gp.IsGitRepo() {
c.Infoln("TIP: in git repository, try fetch host_url from default remote")
hostUrl = gp.DefaultRemoteInfo().HTTPHost(gx.DisableHTTPS)
}

// http://my.gitlab.com/group/repo/merge_requests/new
repoPath := c.Arg("repoPath").String()
if mrOpts.new {
if repoPath == "" {
repoPath = gp.DefaultRemoteInfo().Path()
}

link := hostUrl + "/" + repoPath + "/pulls"
return sysutil.OpenBrowser(link)
}

mrOpts.source, _ = gp.ResolveBranch(mrOpts.source)
if mrOpts.openBr != "" {
openBr, _ := gp.ResolveBranch(mrOpts.openBr)
mrOpts.openIt = true
mrOpts.target = openBr
} else {
mrOpts.target, _ = gp.ResolveBranch(mrOpts.target)
}

var srcPid, dstPid string
// var group, name string
if strutil.IsNotBlank(repoPath) {
repoPath = strings.TrimSpace(repoPath)
_, _, err = gitutil.SplitPath(repoPath)
if err != nil {
return err
}

srcPid, dstPid = repoPath, repoPath
} else {
if err := gp.CheckRemote(); err != nil {
return err
}

if mrOpts.target == "@s" {
mrOpts.target = mrOpts.source
}

dstPid = gp.SrcRemoteInfo().Path()
srcPid = gp.DefaultRemoteInfo().Path()
if mrOpts.direct {
dstPid = gp.DefaultRemoteInfo().Path()
}

if mrOpts.direct || mrOpts.target == mrOpts.source {
repoPath = gp.DefaultRemoteInfo().Path()
} else {
repoPath = dstPid
}
}

show.AList("Current Information", maputil.Data{
"Direct from fork": mrOpts.direct,
"Open browser link": mrOpts.openIt,
"From group/name": srcPid,
"Into group/name": dstPid,
"Into branch name": mrOpts.target,
})

// http://git.your.com/GROUP/NAME/compare/TARGET_BRANCH...GROUP1/NAME:SOURCE_BRANCH
link := fmt.Sprintf(
"%s/%s/compare/%s...%s:%s",
hostUrl, dstPid, mrOpts.target, srcPid, mrOpts.source,
)
c.Warnln("Pull Request Link:")
c.Println(" ", link)

if mrOpts.openIt {
err = sysutil.OpenBrowser(link)
}
return
}
1 change: 1 addition & 0 deletions internal/cli/httpcmd/httpcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var HttpCmd = &gcli.Command{
NewFileServerCmd(),
NewHookServerCmd(),
NewOAPIServeCmd(),
NewJSONServerCmd(),
// TODO convert to CURL command. refer: https://github.com/moul/http2curl/blob/master/http2curl.go
},
Config: func(c *gcli.Command) {
Expand Down
19 changes: 19 additions & 0 deletions internal/cli/httpcmd/json_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package httpcmd

import "github.com/gookit/gcli/v3"

// NewJSONServerCmd instance
func NewJSONServerCmd() *gcli.Command {

return &gcli.Command{
Name: "json-server",
Desc: "start an simple json http server",
Aliases: []string{"json-serve", "json-srv", "jss"},
Config: func(c *gcli.Command) {

},
Func: func(c *gcli.Command, args []string) error {
return c.NewErr("TODO: not implement")
},
}
}
2 changes: 2 additions & 0 deletions kite.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var EmbedFs embed.FS
// from http://patorjk.com/software/taag/#p=testall&f=Graffiti&t=Kite
// font: Doom,Graffiti,Isometric1 - Isometric3, Ogre, Slant
var Banner = `
GoVersion: {{goVersion}}
BuildDate: {{buildDate}}
__ __ __ ______ ______
/\ \/ / /\ \ /\__ _\ /\ ___\
\ \ _"-. \ \ \ \/_/\ \/ \ \ __\
Expand Down
2 changes: 2 additions & 0 deletions kitefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
echo hello
4 changes: 4 additions & 0 deletions pkg/gitx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Config struct {
// ForkMode enable git fork mode for develop.
// If is False, use branch mode, will ignore SourceRemote setting.
ForkMode bool `json:"fork_mode"`
// DisableHTTPS disable https(eg: on PR/MR link). if is true, will use HTTP url.
DisableHTTPS bool `json:"disable_https"`
// SourceRemote the source remote name, it is center repo.
SourceRemote string `json:"source_remote"`
// DefaultRemote the default upstream remote name, use for develop. default: origin.
Expand All @@ -36,6 +38,8 @@ type Config struct {
DefaultBranch string `json:"default_branch"`
// BranchAliases branch aliases
BranchAliases maputil.Aliases `json:"branch_aliases"`
// PrUrlFormat pull request URL format template. can use var like {host}
PrUrlFormat string `json:"pr_url_format"`
}

// NewConfig instance
Expand Down
5 changes: 5 additions & 0 deletions pkg/gitx/gitlab/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func (p *GlProject) GitLoc() *gitx.GitLoc {
return p.lp
}

// IsGitRepo check the project dir is git repo
func (p *GlProject) IsGitRepo() bool {
return p.lp.IsGitRepo()
}

func (p *GlProject) CheckRemote() error {
if err := p.CheckDefaultRemote(); err != nil {
return err
Expand Down
Loading

0 comments on commit a91fe3a

Please sign in to comment.