Skip to content

Commit

Permalink
Remove Readdition and refactor args part in install/uninstall/update
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Feb 23, 2022
1 parent 36ee515 commit 44e5e48
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 64 deletions.
33 changes: 9 additions & 24 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,22 @@ func (m metaCmd) newInstallCmd() *cobra.Command {
Args: cobra.MinimumNArgs(0),
ValidArgs: state.Keys(m.state.Additions),
RunE: func(cmd *cobra.Command, args []string) error {
resources := append(m.state.Additions, m.state.Readditions...)
pkgs := m.GetPackages(resources)
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, state.Keys(resources))
Expand All @@ -77,6 +73,7 @@ func (m metaCmd) newInstallCmd() *cobra.Command {
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 @@ -157,15 +154,3 @@ func (c *installCmd) run(pkgs []config.Package) error {

return exit.ErrorOrNil()
}

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

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

return nil, errors.New("not found")
}
11 changes: 3 additions & 8 deletions cmd/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,9 @@ func (m *metaCmd) init() error {
}
m.state = s

log.Printf("[INFO] state additions: (%d) %#v",
len(s.Additions), state.Keys(s.Additions))
log.Printf("[INFO] state readditions: (%d) %#v",
len(s.Readditions), state.Keys(s.Readditions))
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 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
2 changes: 1 addition & 1 deletion cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *showCmd) run(args []string) error {
}

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.Name,
Type: pkg.Type,
Expand Down
15 changes: 6 additions & 9 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,16 @@ func (m metaCmd) newUninstallCmd() *cobra.Command {
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, state.Keys(resources))
Expand Down
21 changes: 9 additions & 12 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,24 @@ func (m metaCmd) newUpdateCmd() *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MinimumNArgs(0),
ValidArgs: state.Keys(m.state.Additions),
ValidArgs: state.Keys(m.state.Changes),
RunE: func(cmd *cobra.Command, args []string) error {
resources := m.state.Changes
pkgs := m.GetPackages(resources)
if len(pkgs) == 0 {
if len(resources) == 0 {
fmt.Println("No packages to update")
return nil
}

// not update all packages. Instead just only update
// given packages when not updated yet.
var given []config.Package
var tmp []state.Resource
for _, arg := range args {
pkg, err := c.getFromChanges(arg)
if err != nil {
// no hit in changes
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, state.Keys(resources))
Expand All @@ -77,6 +73,7 @@ func (m metaCmd) newUpdateCmd() *cobra.Command {
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
23 changes: 13 additions & 10 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ type State struct {

// No record in state file
Additions []Resource
// Exists but resource paths has something problem
// so it's likely to have had problem when installing before
Readditions []Resource

// Exists in state file but no in config file
// so maybe users had deleted the package from config file
Deletions []Resource

// Something changes happened between config file and state file
// Currently only version (github.release.tag) is detected as changes
Changes []Resource

// All items recorded in state file. It means no changes between state file
// and config file
NoChanges []Resource
Expand Down Expand Up @@ -140,10 +140,7 @@ func (s *State) listChanges() []Resource {
func (s *State) listNoChanges() []Resource {
var resources []Resource
for _, resource := range s.packages {
if contains(s.listAdditions(), resource.Name) {
continue
}
if contains(s.listReadditions(), resource.Name) {
if contains(append(s.listAdditions(), s.listReadditions()...), resource.Name) {
continue
}
if contains(s.listChanges(), resource.Name) {
Expand Down Expand Up @@ -226,8 +223,7 @@ func Open(path string, resourcers []Resourcer) (*State, error) {
}
}

s.Additions = s.listAdditions()
s.Readditions = s.listReadditions()
s.Additions = append(s.listAdditions(), s.listReadditions()...)
s.Deletions = s.listDeletions()
s.Changes = s.listChanges()
s.NoChanges = s.listNoChanges()
Expand Down Expand Up @@ -303,7 +299,6 @@ func (s *State) Refresh() error {
defer s.mu.Unlock()

someChanges := len(s.Additions) > 0 ||
len(s.Readditions) > 0 ||
len(s.Changes) > 0 ||
len(s.Deletions) > 0

Expand All @@ -328,3 +323,11 @@ func (s *State) Refresh() error {

return nil
}

func Map(resources []Resource) map[string]Resource {
m := map[string]Resource{}
for _, resource := range resources {
m[resource.Name] = resource
}
return m
}

0 comments on commit 44e5e48

Please sign in to comment.