Skip to content

Commit

Permalink
Use struct for as.gh-extension
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Mar 18, 2023
1 parent 67861a8 commit 3c1b2a5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
5 changes: 2 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/b4b4r07/afx/pkg/dependency"
"github.com/b4b4r07/afx/pkg/errors"
"github.com/b4b4r07/afx/pkg/state"
"github.com/go-playground/validator/v10"
"github.com/goccy/go-yaml"
Expand Down Expand Up @@ -98,7 +97,7 @@ func (c Config) Parse() ([]Package, error) {
func visitYAML(files *[]string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrapf(err, "%s: failed to visit", path)
return fmt.Errorf("%w: %s: failed to visit", err, path)
}
switch filepath.Ext(path) {
case ".yaml", ".yml":
Expand Down Expand Up @@ -178,7 +177,7 @@ func Sort(given []Package) ([]Package, error) {

resolved, err := dependency.Resolve(graph)
if err != nil {
return pkgs, errors.Wrap(err, "failed to resolve dependency graph")
return pkgs, fmt.Errorf("%w: failed to resolve dependency graph", err)
}

for _, node := range resolved {
Expand Down
42 changes: 32 additions & 10 deletions pkg/config/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"github.com/fatih/color"
)

const GHExtension = "gh-extension"

// GitHub represents GitHub repository
type GitHub struct {
Name string `yaml:"name" validate:"required"`
Expand All @@ -35,16 +33,25 @@ type GitHub struct {

Branch string `yaml:"branch"`
Option *GitHubOption `yaml:"with"`
As string `yaml:"as" validate:"excluded_with=Release"`

Release *GitHubRelease `yaml:"release"`

Plugin *Plugin `yaml:"plugin"`
Command *Command `yaml:"command" validate:"required_with=Release"` // TODO: (not required Release)
Plugin *Plugin `yaml:"plugin"`
Command *Command `yaml:"command" validate:"required_with=Release"` // TODO: (not required Release)
As *GitHubAs `yaml:"as"`

DependsOn []string `yaml:"depends-on"`
}

type GitHubAs struct {
GHExtension *GHExtension `yaml:"gh-extension"`
}

type GHExtension struct {
Name string `yaml:"name" validate:"required"`
RenameTo string `yaml:"rename-to" validate:"startswith=gh-,excludesall=/"`
}

type GitHubOption struct {
Depth int `yaml:"depth"`
}
Expand Down Expand Up @@ -171,8 +178,8 @@ func (c GitHub) Install(ctx context.Context, status chan<- Status) error {
}
}

switch c.As {
case GHExtension:
var errs errors.Errors
if c.IsGHExtension() {
// https://github.com/cli/cli/tree/trunk/pkg/cmd/extension
ok, _ := github.HasRelease(http.DefaultClient, c.Owner, c.Repo)
if ok {
Expand All @@ -183,9 +190,21 @@ func (c GitHub) Install(ctx context.Context, status chan<- Status) error {
return err
}
}
if gh := c.As.GHExtension; gh.RenameTo != "" {
dir := filepath.Join(filepath.Dir(c.GetHome()), gh.RenameTo)
errs.Append(
os.Symlink(
c.GetHome(),
dir,
))
errs.Append(
os.Symlink(
filepath.Join(dir, gh.Name),
filepath.Join(dir, gh.RenameTo),
))
}
}

var errs errors.Errors
if c.HasPluginBlock() {
errs.Append(c.Plugin.Install(c))
}
Expand Down Expand Up @@ -368,8 +387,7 @@ func (c GitHub) GetName() string {

// GetHome returns a path
func (c GitHub) GetHome() string {
switch c.As {
case GHExtension:
if c.IsGHExtension() {
return filepath.Join(os.Getenv("HOME"), ".local", "share", "gh", "extensions", c.Repo)
}
return filepath.Join(os.Getenv("HOME"), ".afx", "github.com", c.Owner, c.Repo)
Expand Down Expand Up @@ -466,3 +484,7 @@ func (c GitHub) checkUpdates(ctx context.Context) (report, error) {
return report{}, errors.New("invalid version comparison")
}
}

func (c GitHub) IsGHExtension() bool {
return c.As != nil && c.As.GHExtension != nil
}

0 comments on commit 3c1b2a5

Please sign in to comment.