Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve dependency on config package of state package #26

Merged
merged 7 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/b4b4r07/afx/pkg/config"
"github.com/b4b4r07/afx/pkg/errors"
"github.com/b4b4r07/afx/pkg/helpers/templates"
"github.com/b4b4r07/afx/pkg/state"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -46,35 +47,33 @@ func (m metaCmd) newInstallCmd() *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MinimumNArgs(0),
ValidArgs: getNameInPackages(m.state.Additions),
ValidArgs: state.Keys(m.state.Additions),
RunE: func(cmd *cobra.Command, args []string) error {
pkgs := append(m.state.Additions, m.state.Readditions...)
if len(pkgs) == 0 {
resources := m.state.Additions
if len(resources) == 0 {
fmt.Println("No packages to install")
return nil
}

// not install all new packages. Instead just only install
// given packages when not installed yet.
var given []config.Package
var tmp []state.Resource
for _, arg := range args {
pkg, err := c.getFromAdditions(arg)
if err != nil {
// no hit in additions
resource, ok := state.Map(resources)[arg]
if !ok {
continue
}
given = append(given, pkg)
tmp = append(tmp, resource)
}
if len(given) > 0 {
pkgs = given
if len(tmp) > 0 {
resources = tmp
}

yes, _ := m.askRunCommand(*c, getNameInPackages(pkgs))
yes, _ := m.askRunCommand(*c, state.Keys(resources))
if !yes {
fmt.Println("Cancelled")
return nil
}

pkgs := m.GetPackages(resources)
m.env.AskWhen(map[string]bool{
"GITHUB_TOKEN": config.HasGitHubReleaseBlock(pkgs),
"AFX_SUDO_PASSWORD": config.HasSudoInCommandBuildSteps(pkgs),
Expand Down Expand Up @@ -155,15 +154,3 @@ func (c *installCmd) run(pkgs []config.Package) error {

return exit.ErrorOrNil()
}

func (c *installCmd) getFromAdditions(name string) (config.Package, error) {
pkgs := append(c.state.Additions, c.state.Readditions...)

for _, pkg := range pkgs {
if pkg.GetName() == name {
return pkg, nil
}
}

return nil, errors.New("not found")
}
51 changes: 26 additions & 25 deletions cmd/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ func (m *metaCmd) init() error {
log.Printf("[DEBUG] mkdir %s\n", os.Getenv("AFX_COMMAND_PATH"))
os.MkdirAll(os.Getenv("AFX_COMMAND_PATH"), os.ModePerm)

s, err := state.Open(filepath.Join(root, "state.json"), m.packages)
resourcers := make([]state.Resourcer, len(m.packages))
for i, pkg := range m.packages {
resourcers[i] = pkg
}

s, err := state.Open(filepath.Join(root, "state.json"), resourcers)
if err != nil {
return errors.Wrap(err, "faield to open state file")
}
m.state = s

log.Printf("[INFO] state additions: (%d) %#v",
len(s.Additions), getNameInPackages(s.Additions))
log.Printf("[INFO] state readditions: (%d) %#v",
len(s.Readditions), getNameInPackages(s.Readditions))
log.Printf("[INFO] state deletions: (%d) %#v",
len(s.Deletions), getNameInResources(s.Deletions))
log.Printf("[INFO] state changes: (%d) %#v",
len(s.Changes), getNameInPackages(s.Changes))
log.Printf("[INFO] state additions: (%d) %#v", len(s.Additions), state.Keys(s.Additions))
log.Printf("[INFO] state deletions: (%d) %#v", len(s.Deletions), state.Keys(s.Deletions))
log.Printf("[INFO] state changes: (%d) %#v", len(s.Changes), state.Keys(s.Changes))
log.Printf("[INFO] state unchanges: (%d) []string{...skip...}", len(s.NoChanges))

return nil
Expand Down Expand Up @@ -232,22 +232,6 @@ func (m *metaCmd) askRunCommand(op interface{}, pkgs []string) (bool, error) {
return yes, nil
}

func getNameInPackages(pkgs []config.Package) []string {
var keys []string
for _, pkg := range pkgs {
keys = append(keys, pkg.GetName())
}
return keys
}

func getNameInResources(resources []state.Resource) []string {
var keys []string
for _, resource := range resources {
keys = append(keys, resource.Name)
}
return keys
}

func shouldCheckForUpdate() bool {
if os.Getenv("AFX_NO_UPDATE_NOTIFIER") != "" {
return false
Expand All @@ -269,3 +253,20 @@ func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) {
stateFilePath := filepath.Join(os.Getenv("HOME"), ".afx", "version.json")
return update.CheckForUpdate(stateFilePath, Repository, Version)
}

func (m metaCmd) GetPackage(resource state.Resource) config.Package {
for _, pkg := range m.packages {
if pkg.GetName() == resource.Name {
return pkg
}
}
return nil
}

func (m metaCmd) GetPackages(resources []state.Resource) []config.Package {
var pkgs []config.Package
for _, resource := range resources {
pkgs = append(pkgs, m.GetPackage(resource))
}
return pkgs
}
41 changes: 10 additions & 31 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sort"
"strings"

"github.com/b4b4r07/afx/pkg/config"
"github.com/b4b4r07/afx/pkg/helpers/templates"
"github.com/b4b4r07/afx/pkg/printers"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -51,58 +50,38 @@ func (c *showCmd) run(args []string) error {
w := printers.GetNewTabWriter(os.Stdout)
headers := []string{"NAME", "TYPE", "STATUS"}

getType := func(pkg config.Package) string {
var ty string
switch pkg := pkg.(type) {
case *config.GitHub:
ty = "GitHub"
if pkg.HasReleaseBlock() {
ty = "GitHub Release"
}
case *config.Gist:
ty = "Gist"
case *config.Local:
ty = "Local"
case *config.HTTP:
ty = "HTTP"
default:
ty = "Unknown"
}
return ty
}

type Item struct {
Name string
Type string
Status string
}

var items []Item
for _, pkg := range append(c.state.Additions, c.state.Readditions...) {
for _, pkg := range c.state.Additions {
items = append(items, Item{
Name: pkg.GetName(),
Type: getType(pkg),
Name: pkg.Name,
Type: pkg.Type,
Status: "WaitingInstall",
})
}
for _, pkg := range c.state.Changes {
items = append(items, Item{
Name: pkg.GetName(),
Type: getType(pkg),
Name: pkg.Name,
Type: pkg.Type,
Status: "WaitingUpdate",
})
}
for _, resource := range c.state.Deletions {
for _, pkg := range c.state.Deletions {
items = append(items, Item{
Name: resource.Name,
Type: resource.Type,
Name: pkg.Name,
Type: pkg.Type,
Status: "WaitingUninstall",
})
}
for _, pkg := range c.state.NoChanges {
items = append(items, Item{
Name: pkg.GetName(),
Type: getType(pkg),
Name: pkg.Name,
Type: pkg.Type,
Status: "Installed",
})
}
Expand Down
37 changes: 25 additions & 12 deletions cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/b4b4r07/afx/pkg/errors"
"github.com/b4b4r07/afx/pkg/helpers/templates"
"github.com/b4b4r07/afx/pkg/state"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -62,12 +63,12 @@ func (c stateCmd) newStateListCmd() *cobra.Command {
SilenceErrors: true,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
items, err := c.state.List()
resources, err := c.state.List()
if err != nil {
return err
}
for _, item := range items {
fmt.Println(item)
for _, resource := range resources {
fmt.Println(resource.Name)
}
return nil
},
Expand Down Expand Up @@ -106,30 +107,42 @@ func (c stateCmd) newStateRemoveCmd() *cobra.Command {
SilenceErrors: true,
Aliases: []string{"rm"},
Args: cobra.MinimumNArgs(0),
ValidArgs: getNameInPackages(c.state.NoChanges),
ValidArgs: state.Keys(c.state.NoChanges),
RunE: func(cmd *cobra.Command, args []string) error {
var resources []string
var resources []state.Resource
switch len(cmd.Flags().Args()) {
case 0:
list, err := c.state.List()
rs, err := c.state.List()
if err != nil {
return errors.Wrap(err, "failed to list state items")
}
var items []string
for _, r := range rs {
items = append(items, r.Name)
}
var selected string
if err := survey.AskOne(&survey.Select{
Message: "Choose a package:",
Options: list,
Options: items,
}, &selected); err != nil {
return errors.Wrap(err, "failed to get input from console")
}
resources = append(resources, selected)
resource, err := c.state.Get(selected)
if err != nil {
return errors.Wrapf(err, "%s: failed to get state file", selected)
}
resources = append(resources, resource)
default:
// TODO: check valid or invalid
resources = cmd.Flags().Args()
for _, arg := range cmd.Flags().Args() {
resource, err := c.state.Get(arg)
if err != nil {
return errors.Wrapf(err, "%s: failed to get state file", arg)
}
resources = append(resources, resource)
}
}
for _, resource := range resources {
id := c.state.ToID(resource)
c.state.Remove(id)
c.state.Remove(resource)
}
return nil
},
Expand Down
49 changes: 17 additions & 32 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,27 @@ func (m metaCmd) newUninstallCmd() *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MinimumNArgs(0),
ValidArgs: getNameInResources(m.state.Deletions),
ValidArgs: state.Keys(m.state.Deletions),
RunE: func(cmd *cobra.Command, args []string) error {
resources := m.state.Deletions
if len(resources) == 0 {
fmt.Println("No packages to uninstall")
return nil
}

// not uninstall all old packages. Instead just only uninstall
// given packages when not uninstalled yet.
var given []state.Resource
var tmp []state.Resource
for _, arg := range args {
resource, err := c.getFromDeletions(arg)
if err != nil {
// no hit in deletions
resource, ok := state.Map(resources)[arg]
if !ok {
continue
}
given = append(given, resource)
tmp = append(tmp, resource)
}
if len(given) > 0 {
resources = given
if len(tmp) > 0 {
resources = tmp
}

yes, _ := m.askRunCommand(*c, getNameInResources(resources))
yes, _ := m.askRunCommand(*c, state.Keys(resources))
if !yes {
fmt.Println("Cancelled")
return nil
Expand All @@ -85,35 +82,23 @@ func (m metaCmd) newUninstallCmd() *cobra.Command {
func (c *uninstallCmd) run(resources []state.Resource) error {
var errs errors.Errors

delete := func(paths ...string) error {
var errs errors.Errors
for _, path := range paths {
errs.Append(os.RemoveAll(path))
}
return errs.ErrorOrNil()
}

for _, resource := range resources {
err := delete(append(resource.Paths, resource.Home)...)
if err != nil {
errs.Append(err)
continue
}
c.state.Remove(resource.ID)
c.state.Remove(resource)
fmt.Printf("deleted %s\n", resource.Home)
}

return errs.ErrorOrNil()
}

func delete(paths ...string) error {
var errs errors.Errors
for _, path := range paths {
errs.Append(os.RemoveAll(path))
}
return errs.ErrorOrNil()
}

func (c *uninstallCmd) getFromDeletions(name string) (state.Resource, error) {
resources := c.state.Deletions

for _, resource := range resources {
if resource.Name == name {
return resource, nil
}
}

return state.Resource{}, errors.New("not found")
}
Loading