-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from ysicing/b2
feat(release 2.0.3): 支持cloud & code subcmd
- Loading branch information
Showing
44 changed files
with
1,519 additions
and
2,050 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) 2021 ysicing <i@ysicing.me> | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/ergoapi/util/file" | ||
"github.com/gosuri/uitable" | ||
"github.com/manifoldco/promptui" | ||
"github.com/spf13/cobra" | ||
"github.com/ysicing/ergo/common" | ||
"github.com/ysicing/ergo/pkg/ergo/cloud" | ||
"github.com/ysicing/ergo/pkg/ergo/cloud/aliyun" | ||
"github.com/ysicing/ergo/pkg/ergo/cloud/qcloud" | ||
"github.com/ysicing/ergo/pkg/util/factory" | ||
"helm.sh/helm/v3/pkg/cli/output" | ||
"os" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// NewCloudCommand 云服务商支持 | ||
func newCloudCommand(f factory.Factory) *cobra.Command { | ||
cloud := &cobra.Command{ | ||
Use: "cloud [flags]", | ||
Short: "云服务商支持", | ||
} | ||
cloud.AddCommand(newCloudCofig(f)) | ||
cloud.AddCommand(newCloudDns(f)) | ||
return cloud | ||
} | ||
|
||
func newCloudCofig(f factory.Factory) *cobra.Command { | ||
cmd := &cobra.Command{} | ||
return cmd | ||
} | ||
|
||
func newCloudDns(f factory.Factory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "dns [flags]", | ||
Short: "dns", | ||
} | ||
cmd.AddCommand(newDnsList(f)) | ||
return cmd | ||
} | ||
|
||
func newDnsList(f factory.Factory) *cobra.Command { | ||
l := f.GetLog() | ||
cmd := &cobra.Command{ | ||
Use: "domain", | ||
Short: "域名列表", | ||
Run: func(cobraCmd *cobra.Command, args []string) { | ||
templates := &promptui.SelectTemplates{ | ||
Label: "{{ . }}", | ||
Active: "\U0001F449 {{ .Value | cyan }}", | ||
Inactive: " {{ .Value | cyan }}", | ||
Selected: "\U0001F389 {{ .Value | red | cyan }}", | ||
} | ||
cloudprompt := promptui.Select{ | ||
Label: "选择云服务商", | ||
Items: cloud.CloudType, | ||
Size: 4, | ||
Templates: templates, | ||
} | ||
selectid, _, _ := cloudprompt.Run() | ||
ct := cloud.CloudType[selectid] | ||
var domainlist cloud.DomainList | ||
if ct.Key == "all" || ct.Key == cloud.ProviderQcloud.Value() { | ||
l.Debugf("load qcloud domain") | ||
// 腾讯云 | ||
p, err := qcloud.NewDns(qcloud.WithLog(l), qcloud.WithApi(os.Getenv("TX_Key"), os.Getenv("TX_Secret"))) | ||
if err != nil { | ||
l.Errorf("create qcloud api client err: %v", err) | ||
} else { | ||
pd, err := p.DomainList(context.Background()) | ||
if err == nil { | ||
domainlist = append(domainlist, pd...) | ||
} else { | ||
l.Errorf("do qcloud dns api err: %v", err) | ||
} | ||
} | ||
} | ||
if ct.Key == "all" || ct.Key == cloud.ProviderAliyun.Value() { | ||
l.Debugf("load aliyun domain") | ||
// 阿里云 | ||
p, err := aliyun.NewDns(aliyun.WithLog(l), aliyun.WithApi(os.Getenv("Ali_Key"), os.Getenv("Ali_Secret"))) | ||
if err != nil { | ||
l.Errorf("create aliyun api client err: %v", err) | ||
} else { | ||
pd, err := p.DomainList(context.Background()) | ||
if err == nil { | ||
domainlist = append(domainlist, pd...) | ||
} else { | ||
l.Errorf("do aliyun dns api err: %v", err) | ||
} | ||
} | ||
} | ||
table := uitable.New() | ||
table.AddRow("服务商", "域名") | ||
for _, re := range domainlist { | ||
table.AddRow(re.Provider, re.Name) | ||
} | ||
output.EncodeTable(os.Stdout, table) | ||
domainfile := fmt.Sprintf("%v/.domain", common.GetDefaultCfgDir()) | ||
if file.CheckFileExists(domainfile) { | ||
file.RemoveFiles(domainfile) | ||
} | ||
resp, _ := yaml.Marshal(domainlist) | ||
file.Writefile(domainfile, string(resp)) | ||
l.Donef("域名缓存成功: %v", domainfile) | ||
}, | ||
} | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2021 ysicing <i@ysicing.me> | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/manifoldco/promptui" | ||
"github.com/spf13/cobra" | ||
"github.com/ysicing/ergo/pkg/ergo/codegen" | ||
"github.com/ysicing/ergo/pkg/util/factory" | ||
"github.com/ysicing/ergo/pkg/util/log" | ||
"strings" | ||
) | ||
|
||
type CodeOptions struct { | ||
Log log.Logger | ||
} | ||
|
||
func newCodeGenCmd(f factory.Factory) *cobra.Command { | ||
c := &CodeOptions{ | ||
Log: f.GetLog(), | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "code [flags]", | ||
Short: "初始化项目", | ||
Run: func(cobraCmd *cobra.Command, args []string) { | ||
c.Init() | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func (code CodeOptions) Init() { | ||
searcher := func(input string, index int) bool { | ||
p := codegen.CodeType[index] | ||
name := strings.Replace(strings.ToLower(p.Key), " ", "", -1) | ||
input = strings.Replace(strings.ToLower(input), " ", "", -1) | ||
return strings.Contains(name, input) | ||
} | ||
templates := &promptui.SelectTemplates{ | ||
Label: "{{ . }}", | ||
Active: "\U0001F449 {{ .Key | cyan }}", | ||
Inactive: " {{ .Key | cyan }}", | ||
Selected: "\U0001F389 {{ .Key | red | cyan }}", | ||
} | ||
codetype := promptui.Select{ | ||
Label: "选择代码类型", | ||
Items: codegen.CodeType, | ||
Searcher: searcher, | ||
Size: 4, | ||
Templates: templates, | ||
} | ||
codetypeid, _, _ := codetype.Run() | ||
selectcodetypevalue := codegen.CodeType[codetypeid].Key | ||
code.Log.Infof("\U0001F389 选择 %v", selectcodetypevalue) | ||
codefunc := codegen.CodeGen{Log: code.Log} | ||
if selectcodetypevalue == "go" { | ||
code.Log.Infof("Start downloading the template...") | ||
if err := codefunc.GoClone(); err != nil { | ||
code.Log.Fatal(err) | ||
return | ||
} | ||
} else { | ||
code.Log.Infof("Start Gen Crds template...") | ||
if err := codefunc.GenCrds(); err != nil { | ||
code.Log.Fatal(err) | ||
return | ||
} | ||
} | ||
} |
Oops, something went wrong.