Skip to content

Commit

Permalink
Merge pull request #7 from blacknon/develop
Browse files Browse the repository at this point in the history
v0.1.4
  • Loading branch information
blacknon authored Apr 14, 2024
2 parents 233e95b + d8ead25 commit 9c777f2
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 110 deletions.
160 changes: 112 additions & 48 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package client
import (
"fmt"
"os"
"sync"

"github.com/blacknon/snipt/config"
"github.com/google/go-github/github"
Expand All @@ -31,30 +32,55 @@ func (c *Client) Init(conf config.Config) {

// Gitlab.Init
for _, gitlabConf := range conf.GitLab {
g := GitlabClient{}
g := GitlabClient{
proxy: gitlabConf.Proxy,
proxyUser: gitlabConf.ProxyUser,
proxyPass: gitlabConf.ProxyPass,
}
g.Init(gitlabConf.Url, gitlabConf.AccessToken)

c.lists = append(c.lists, &g)
}
}

// List
func (c *Client) List(isFile, isSecret bool) (snippetList SnippetList) {
//
func (c *Client) List(isFile, isSecret bool) SnippetList {
var snippetList SnippetList
var wg sync.WaitGroup // goroutineの完了を待機するためのWaitGroup
resultChannel := make(chan SnippetList, len(c.lists)) // 処理結果を収集するチャネル

// 各クライアントに対してgoroutineを起動
for _, gc := range c.lists {
list, err := gc.List(isFile, isSecret)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
continue
}
wg.Add(1)
go func(gc GitClient) {
defer wg.Done()
// クライアントのListメソッドを実行
list, err := gc.List(isFile, isSecret)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
return
}

// 処理結果をチャネルに送信
resultChannel <- list
}(gc)
}

// 全てのgoroutineの完了を待機してチャネルを閉じる
go func() {
wg.Wait()
close(resultChannel)
}()

// チャネルから受け取ったリストをまとめる
for list := range resultChannel {
snippetList = append(snippetList, list...)
}

//
// filterListsDataに結果を保存
c.filterListsData = snippetList

return
return snippetList
}

// Get
Expand Down Expand Up @@ -136,55 +162,93 @@ func (c *Client) Delete(url string) (err error) {
}

// PlatformList
func (c *Client) PlatformList(enableProject bool) (platformList []string, err error) {
func (c *Client) PlatformList(enableProject bool) ([]string, error) {
var platformList []string
var err error
var wg sync.WaitGroup // 同期用のWaitGroupを用意

// clear
c.filterListsData = []*SnippetListData{}

for _, gc := range c.lists {
platformName := gc.GetPlatformName()
gc.SetFilterKey(platformName)

// append platform to platformList
platformList = append(platformList, platformName)
// チャネルを用意して、処理結果を収集
resultChannel := make(chan struct {
platform string
data *SnippetListData
err error
}, len(c.lists))

// append paltform to c.filterListsData
data := &SnippetListData{
Client: gc,
Platform: platformName,
}
// 各リストに対して並行処理を実行
for _, gc := range c.lists {
wg.Add(1)
go func(gc GitClient) {
defer wg.Done()

c.filterListsData = append(c.filterListsData, data)
platformName := gc.GetPlatformName()
gc.SetFilterKey(platformName)

// Get gitlab project list
glsnippet, ok := gc.(*GitlabClient)
if enableProject && ok {
projects, err := glsnippet.GetProjectList()
if err != nil {
return []string{}, err
data := &SnippetListData{
Client: gc,
Platform: platformName,
}

for _, p := range projects {
// set platformName
pn := fmt.Sprintf("%s /%s", platformName, p.PathWithNamespace)

// set pd
pd := &SnippetListData{}
*pd = *data
pd.Platform = platformName

// set pd.client
gls := &GitlabClient{}
*gls = *glsnippet
gls.Project = p
gls.SetFilterKey(pn)
pd.Client = gls

platformList = append(platformList, pn)
c.filterListsData = append(c.filterListsData, pd)
// Get gitlab project list
glsnippet, ok := gc.(*GitlabClient)
if enableProject && ok {
projects, err := glsnippet.GetProjectList()
if err != nil {
resultChannel <- struct {
platform string
data *SnippetListData
err error
}{platform: platformName, data: nil, err: err}
return
}

for _, p := range projects {
pn := fmt.Sprintf("%s /%s", platformName, p.PathWithNamespace)

pd := &SnippetListData{
Client: &GitlabClient{Project: p /* 他のフィールドを設定 */},
Platform: pn,
}
// 結果をチャネルに送信
resultChannel <- struct {
platform string
data *SnippetListData
err error
}{platform: pn, data: pd, err: nil}
}
} else {
// 結果をチャネルに送信
resultChannel <- struct {
platform string
data *SnippetListData
err error
}{platform: platformName, data: data, err: nil}
}
}(gc)
}

// 全てのgoroutineが終了するのを待機
go func() {
wg.Wait()
close(resultChannel) // チャネルを閉じる
}()

// 結果を受け取ってリストに追加
for result := range resultChannel {
if result.err != nil {
err = result.err
} else {
platformList = append(platformList, result.platform)
c.filterListsData = append(c.filterListsData, result.data)
}
}
return

if err != nil {
return nil, err
}
return platformList, nil
}

func (c *Client) VisibilityListFromPlatform(platform string) (visibilityList []Visibility) {
Expand Down
6 changes: 6 additions & 0 deletions client/gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"
"net/url"
"sort"

"github.com/google/go-github/github"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -98,6 +99,11 @@ func (g *GistClient) List(isFile, isSecret bool) (snippetList SnippetList, err e
}
}

sort.Slice(snippetList, func(i, j int) bool {
// i番目とj番目の要素のAgeを比較
return snippetList[i].URL < snippetList[j].URL
})

return snippetList, err
}

Expand Down
133 changes: 78 additions & 55 deletions client/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package client

import (
"context"
"encoding/base64"
"fmt"
"net/http"
"net/url"
Expand All @@ -22,6 +23,11 @@ type GitlabClient struct {
PlatformName string
FilterKey string
Project *gitlab.Project

// proxy
proxy string
proxyUser string
proxyPass string
}

var (
Expand All @@ -35,27 +41,22 @@ func (g *GitlabClient) Init(u, token string) (err error) {
// create ctx
g.ctx = context.Background()

// TODO: proxyを設定できるように修正する(↓はテスト時のコードなので少し残しておく)
// Create http client
// h := &http.Client{
// Transport: &http.Transport{
// DialContext: (&net.Dialer{
// Timeout: 1000 * time.Millisecond,
// KeepAlive: 1000 * time.Millisecond,
// }).DialContext,
// TLSHandshakeTimeout: 300 * time.Millisecond,
// ResponseHeaderTimeout: 300 * time.Millisecond,
// ExpectContinueTimeout: 100 * time.Millisecond,
// },
// Timeout: 3000 * time.Millisecond,
// }
// proxyUrl, err := url.Parse("http://127.0.0.1:8080")
// h := &http.Client{
// Transport: &http.Transport{
// Proxy: http.ProxyURL(proxyUrl),
// },
// }
h := &http.Client{}
transport := &http.Transport{}
if g.proxy != "" {
proxyUrl, err := url.Parse(g.proxy)
if err != nil {
return err
}

hdr := make(http.Header)
hdr.Add("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(g.proxyUser+":"+g.proxyPass)))

transport = &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
}
}

h := &http.Client{Transport: transport}

// Create Gitlab Client
g.client, err = gitlab.NewClient(token, gitlab.WithBaseURL(u), gitlab.WithHTTPClient(h))
Expand All @@ -81,50 +82,72 @@ func (g *GitlabClient) Init(u, token string) (err error) {

// List
func (g *GitlabClient) List(isFile, isSecret bool) (snippetList SnippetList, err error) {
// get snippetList
snippetDataList, _, err := g.client.Snippets.ListSnippets(&gitlab.ListSnippetsOptions{})

// gitlab.Snippet to Interface
for _, snippet := range snippetDataList {
if !isSecret {
//
v := getGitlabVisibilityFromString(snippet.Visibility)
switch v {
case GitlabIsPrivate, GitlabIsInternal:
continue
}
}
// set ListProjectsOptions pageSize
const pageSize = 50

// get Description
title := replaceNewline(snippet.Title, "\\n")
opt := gitlab.ListSnippetsOptions(
gitlab.ListOptions{
Page: 0,
PerPage: pageSize,
},
)

for {
// get snippetList
snippetDataList, resp, ferr := g.client.Snippets.ListSnippets(&opt)

data := SnippetListData{
Client: g,
Platform: g.PlatformName,
Id: strconv.Itoa(snippet.ID),
Title: title,
URL: snippet.WebURL,
Visibility: snippet.Visibility,
// check error
if ferr != nil {
return snippetList, ferr
}

// check file flag
if isFile {
if len(snippet.Files) > 1 {
for _, f := range snippet.Files {
fd := data
fd.URL, _ = url.JoinPath(fd.URL, f.Path)
fd.RawURL = f.RawURL
for _, snippet := range snippetDataList {
if !isSecret {
//
v := getGitlabVisibilityFromString(snippet.Visibility)
switch v {
case GitlabIsPrivate, GitlabIsInternal:
continue
}
}

// get Description
title := replaceNewline(snippet.Title, "\\n")

data := SnippetListData{
Client: g,
Platform: g.PlatformName,
Id: strconv.Itoa(snippet.ID),
Title: title,
URL: snippet.WebURL,
Visibility: snippet.Visibility,
}

snippetList = append(snippetList, &fd)
// check file flag
if isFile {
if len(snippet.Files) > 1 {
for _, f := range snippet.Files {
fd := data
fd.URL, _ = url.JoinPath(fd.URL, f.Path)
fd.RawURL = f.RawURL

snippetList = append(snippetList, &fd)
}
} else {
data.URL, _ = url.JoinPath(data.URL, snippet.FileName)
data.RawURL = snippet.RawURL
snippetList = append(snippetList, &data)
}
} else {
data.URL, _ = url.JoinPath(data.URL, snippet.FileName)
data.RawURL = snippet.RawURL
snippetList = append(snippetList, &data)
}
} else {
snippetList = append(snippetList, &data)
}

if resp.NextPage == 0 {
break
}

opt.Page = resp.NextPage
}

return
Expand Down
Loading

0 comments on commit 9c777f2

Please sign in to comment.