Skip to content

Commit

Permalink
integrate github with tran (#9)
Browse files Browse the repository at this point in the history
* get the tran gh api, add `auth` and `gh-config` commands

* bump `abdfnx/gh` version, add `gh-repo` command to `tran`

* finally, add `tran sync` command to tran, add new go modules

* update `tran` root command examples

Co-authored-by: @abdfnx
  • Loading branch information
david-tomson committed Feb 11, 2022
1 parent 5675c59 commit d3d7d17
Show file tree
Hide file tree
Showing 10 changed files with 748 additions and 127 deletions.
4 changes: 3 additions & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tasks:
- init: go get -d ./...
command: curl -fsSL https://cutt.ly/tran-cli | bash
command: |
brew install go-task/tap/go-task
curl -fsSL https://cutt.ly/tran-cli | bash
5 changes: 5 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/abdfnx/tran/models"
"github.com/abdfnx/tran/constants"
"github.com/abdfnx/tran/internal/tui"
"github.com/abdfnx/gh/pkg/cmd/factory"
)

var NewSendCmd = &cobra.Command{
Expand Down Expand Up @@ -51,3 +52,7 @@ var NewReceiveCmd = &cobra.Command{
return nil
},
}

var NewAuthCmd = Auth(factory.New())
var NewGHConfigCmd = GHConfig(factory.New())
var NewGHRepoCmd = Repo(factory.New())
59 changes: 59 additions & 0 deletions app/gh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package app

import (
"github.com/spf13/cobra"
"github.com/abdfnx/gh/pkg/cmdutil"
aCmd "github.com/abdfnx/gh/pkg/cmd/auth"
cCmd "github.com/abdfnx/gh/pkg/cmd/gh-config"
rCmd "github.com/abdfnx/gh/pkg/cmd/gh-repo"
"github.com/abdfnx/gh/context"
"github.com/abdfnx/gh/api"
"github.com/abdfnx/gh/core/ghrepo"
)

func Auth(f *cmdutil.Factory) *cobra.Command {
cmd := aCmd.NewCmdAuth(f)
return cmd
}

func GHConfig(f *cmdutil.Factory) *cobra.Command {
cmd := cCmd.NewCmdConfig(f)
return cmd
}

func Repo(f *cmdutil.Factory) *cobra.Command {
repoResolvingCmdFactory := *f
repoResolvingCmdFactory.BaseRepo = resolvedBaseRepo(f)

cmd := rCmd.NewCmdRepo(&repoResolvingCmdFactory)

return cmd
}

func resolvedBaseRepo(f *cmdutil.Factory) func() (ghrepo.Interface, error) {
return func() (ghrepo.Interface, error) {
httpClient, err := f.HttpClient()
if err != nil {
return nil, err
}

apiClient := api.NewClientFromHTTP(httpClient)

remotes, err := f.Remotes()
if err != nil {
return nil, err
}

repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, "")
if err != nil {
return nil, err
}

baseRepo, err := repoContext.BaseRepo(f.IOStreams)
if err != nil {
return nil, err
}

return baseRepo, nil
}
}
182 changes: 182 additions & 0 deletions app/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package app

import (
"fmt"
"log"
"time"
"runtime"

"github.com/abdfnx/gosh"
"github.com/spf13/cobra"
"github.com/abdfnx/gh/utils"
"github.com/briandowns/spinner"
"github.com/MakeNowJust/heredoc"
"github.com/abdfnx/tran/constants"
git_config "github.com/david-tomson/tran-git"
)

var username = git_config.GitConfig()

var (
NewCmdStart = &cobra.Command{
Use: "start",
Aliases: []string{"."},
Example: "tran sync start",
Short: "Start sync your tran config.",
Run: func(cmd *cobra.Command, args []string) {
if username != ":username" {
exCmd := constants.StartEX()

gosh.RunMulti(constants.Start_ml(), constants.Start_w())
gosh.Run(exCmd)
} else {
utils.AuthMessage()
}
},
}

NewCmdClone = &cobra.Command{
Use: "clone",
Aliases: []string{"cn"},
Short: CloneHelp(),
Run: func(cmd *cobra.Command, args []string) {
if username != ":username" {
cloneCmd := constants.Clone()

gosh.Run(cloneCmd)
gosh.RunMulti(constants.Clone_check_ml(), constants.Clone_check_w())
} else {
utils.AuthMessage()
}
},
}

NewCmdPush = &cobra.Command{
Use: "push",
Aliases: []string{"ph"},
Short: "Push the new changes in tran config file.",
Run: func(cmd *cobra.Command, args []string) {
if username != ":username" {
gosh.RunMulti(constants.Push_ml(), constants.Push_w())
} else {
utils.AuthMessage()
}
},
}

NewCmdPull = &cobra.Command{
Use: "pull",
Aliases: []string{"pl"},
Short: PullHelp(),
Run: func(cmd *cobra.Command, args []string) {
if username != ":username" {
gosh.RunMulti(constants.Pull_ml(), constants.Pull_w())
} else {
utils.AuthMessage()
}
},
}

FetchClone = &cobra.Command{
Use: "fetchx",
Short: "Special command for windows",
Run: func(cmd *cobra.Command, args []string) {
if username != ":username" {
if runtime.GOOS == "windows" {
gosh.PowershellCommand(constants.Clone())
} else {
fmt.Println("This command isn't avaliable for this platform")
}
} else {
utils.AuthMessage()
}
},
}
)

func Sync() *cobra.Command {
cmd := &cobra.Command{
Use: "sync <command>",
Short: "Sync your tran config file.",
Long: SyncHelp(),
Example: heredoc.Doc(`
tran sync start
tran sync clone
`),
}

cmd.AddCommand(NewCmdStart)
cmd.AddCommand(NewCmdClone)
cmd.AddCommand(NewCmdPush)
cmd.AddCommand(NewCmdPull)
cmd.AddCommand(FetchClone)

return cmd
}

const tranConfigPath string = "/.tran"

func PullHelp() string {
return git_config.GitConfigWithMsg("Pull the new changes from ", tranConfigPath)
}

func SyncHelp() string {
return git_config.GitConfigWithMsg("Sync your config file, by create a private repo at ", tranConfigPath)
}

func CloneHelp() string {
return git_config.GitConfigWithMsg("Clone your .tran from your private repo at https://github.com/", tranConfigPath)
}

func PushSync() {
const Syncing string = " 📮 Syncing..."

if runtime.GOOS == "windows" {
err, out, errout := gosh.PowershellOutput(
`
$directoyPath = "~/.config/tran/.git"
if (Test-Path -path $directoyPath) {
Write-Host "Reading from .tran folder..."
}
`)

fmt.Print(out)

if err != nil {
log.Printf("error: %v\n", err)
fmt.Print(errout)
} else if out != "" {
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = Syncing
s.Start()

gosh.PowershellCommand(constants.Push_w())

s.Stop()
}
} else {
err, out, errout := gosh.ShellOutput(
`
if [ -d ~/.config/tran/.git ]; then
echo "📖 Reading from .tran folder..."
fi
`)

fmt.Print(out)

if err != nil {
log.Printf("error: %v\n", err)
fmt.Print(errout)
} else if out != "" {
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = Syncing
s.Start()

gosh.ShellCommand(constants.Push_ml())

s.Stop()
}
}
}

Loading

0 comments on commit d3d7d17

Please sign in to comment.