Skip to content

Commit

Permalink
feat(update): update
Browse files Browse the repository at this point in the history
update

Signed-off-by: ysicing <i@ysicing.me>
  • Loading branch information
ysicing committed May 1, 2022
1 parent ace9e17 commit 9df9ec0
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 23 deletions.
22 changes: 15 additions & 7 deletions cmd/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ func Install(f factory.Factory) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
iargs := strings.Split(args[0], "/")
if len(iargs) != 2 {
if len(iargs) == 2 {
o.Repo = iargs[0]
o.Name = iargs[1]
} else if len(iargs) == 1 && iargs[0] != "ysicing" {
o.Repo = "ysicing"
o.Name = iargs[0]
} else {
return fmt.Errorf("ergo addons install [repo/name] or [repo] [name]")
}
o.Repo = iargs[0]
o.Name = iargs[1]
} else {
o.Repo = args[0]
o.Name = args[1]
Expand All @@ -46,11 +50,15 @@ func UnInstall(f factory.Factory) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
iargs := strings.Split(args[0], "/")
if len(iargs) != 2 {
return fmt.Errorf("ergo addons uninstall [repo/name] or [repo] [name]")
if len(iargs) == 2 {
o.Repo = iargs[0]
o.Name = iargs[1]
} else if len(iargs) == 1 && iargs[0] != "ysicing" {
o.Repo = "ysicing"
o.Name = iargs[0]
} else {
return fmt.Errorf("ergo addons install [repo/name] or [repo] [name]")
}
o.Repo = iargs[0]
o.Name = iargs[1]
} else {
o.Repo = args[0]
o.Name = args[1]
Expand Down
1 change: 1 addition & 0 deletions cmd/kube/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ysicing/ergo/pkg/util/log"

// default provider
_ "github.com/ysicing/ergo/internal/pkg/providers/incluster"
_ "github.com/ysicing/ergo/internal/pkg/providers/native"
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/go-ping/ping v0.0.0-20211130115550-779d1e919534
github.com/go-sql-driver/mysql v1.6.0
github.com/gofrs/flock v0.8.1
github.com/google/go-github/v39 v39.2.1-0.20211020014439-17a925b6f848
github.com/google/go-github/v44 v44.0.0
github.com/gopasspw/gopass v1.14.0
github.com/gosuri/uitable v0.0.4
github.com/hashicorp/go-version v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/go-containerregistry v0.5.1/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0=
github.com/google/go-github/v30 v30.1.0 h1:VLDx+UolQICEOKu2m4uAoMti1SxuEBAl7RSEG16L+Oo=
github.com/google/go-github/v30 v30.1.0/go.mod h1:n8jBpHl45a/rlBUtRJMOG4GhNADUQFEufcolZ95JfU8=
github.com/google/go-github/v39 v39.2.1-0.20211020014439-17a925b6f848 h1:5nQBvcXwZb9k+tNMO4wppyR+fQvcHpytOYUj1sIIdiQ=
github.com/google/go-github/v39 v39.2.1-0.20211020014439-17a925b6f848/go.mod h1:C1s8C5aCC9L+JXIYpJM5GYytdX52vC1bLvHEF1IhBrE=
github.com/google/go-github/v44 v44.0.0 h1:1Lfk2mhM7pTWqwGC6Ft16S3c2LBw8DLcw9TOhYoQ9zE=
github.com/google/go-github/v44 v44.0.0/go.mod h1:CqZYQRxOcb81M+ufZB7duWNS0lFfas/r7cEAKpLBYww=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
Expand Down
66 changes: 66 additions & 0 deletions internal/pkg/providers/incluster/incluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package incluster

import (
"github.com/ysicing/ergo/internal/pkg/cluster"
"github.com/ysicing/ergo/internal/pkg/providers"
"github.com/ysicing/ergo/internal/pkg/types"
)

// providerName is the name of this provider.
const providerName = "incluster"

const createUsageExample = `
create default cluster:
ergo kube k3s init
`

type Native struct {
*cluster.Cluster
}

func init() {
providers.RegisterProvider(providerName, func() (providers.Provider, error) {
return newProvider(), nil
})
}

func newProvider() *Native {
c := cluster.NewCluster()
c.Provider = providerName
return &Native{
Cluster: c,
}
}

// GetUsageExample returns native usage example prompt.
func (p *Native) GetUsageExample(action string) string {
switch action {
case "create":
return createUsageExample
default:
return "not support"
}
}

// GetCreateFlags returns native create flags.
func (p *Native) GetCreateFlags() []types.Flag {
return nil
}

func (p *Native) GetProviderName() string {
return p.Provider
}

// InitCluster init cluster.
func (p *Native) InitCluster() (err error) {
return nil
}

// JoinCluster join cluster.
func (p *Native) JoinCluster() (err error) {
return nil
}

func (p *Native) InitSystem() error {
return nil
}
23 changes: 10 additions & 13 deletions pkg/ergo/git/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,11 @@ import (
"strings"

"github.com/ergoapi/util/ptr"
"github.com/google/go-github/v39/github"
"github.com/google/go-github/v44/github"
"github.com/ysicing/ergo/pkg/util/log"
"golang.org/x/oauth2"
)

func getNameFromURL(name, url string) string {
if strings.Contains(name, "/") {
s := strings.Split(url, "/")
return s[len(s)-1]
}
return name
}

func CleanPackage(user, token string) {
ctx := context.TODO()
ts := oauth2.StaticTokenSource(
Expand All @@ -39,21 +31,26 @@ func CleanPackage(user, token string) {
}
for _, p := range packages {
log.Flog.Debugf("package %v", p.GetName())
packagesversions, _, err := client.Users.PackageGetAllVersions(ctx, user, "container", getNameFromURL(p.GetName(), p.GetHTMLURL()))
packagesversions, _, err := client.Users.PackageGetAllVersions(ctx, user, "container", p.GetName(), &github.PackageListOptions{
ListOptions: github.ListOptions{
PerPage: 300,
},
PackageType: ptr.StringPtr("container"),
})
if err != nil {
log.Flog.Debugf("list package version err: %v, skip", err)
continue
}
for _, pv := range packagesversions {
clean(ctx, client, pv, p.GetOwner().GetName(), p.GetName(), getNameFromURL(p.GetName(), p.GetHTMLURL()))
clean(ctx, client, pv, p.GetOwner().GetName(), p.GetName())
}
}
}

func clean(ctx context.Context, client *github.Client, p *github.PackageVersion, user, name, urlname string) {
func clean(ctx context.Context, client *github.Client, p *github.PackageVersion, user, name string) {
for _, v := range p.Metadata.Container.Tags {
if strings.Contains(v, "-") {
if resp, err := client.Users.PackageDeleteVersion(ctx, user, "container", urlname, p.GetID()); err != nil {
if resp, err := client.Users.PackageDeleteVersion(ctx, user, "container", name, p.GetID()); err != nil {
if resp.StatusCode == 400 {
log.Flog.Warnf("%v cannot delete the last tagged version [ %v ] of %v.", user, v, name)
} else if resp.StatusCode == 404 {
Expand Down

0 comments on commit 9df9ec0

Please sign in to comment.