Skip to content

Commit efe7664

Browse files
authored
Merge pull request #23 from b4b4r07/feature/state
2 parents a327101 + 2a25823 commit efe7664

File tree

4 files changed

+196
-51
lines changed

4 files changed

+196
-51
lines changed

cmd/self-update.go

+43-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import (
44
"context"
55
"fmt"
66
"io/ioutil"
7+
"log"
78
"net/http"
89
"os"
910
"path/filepath"
1011
"runtime"
12+
"sort"
13+
"strings"
1114

1215
"github.com/AlecAivazis/survey/v2"
16+
"github.com/Masterminds/semver/v3"
1317
"github.com/b4b4r07/afx/pkg/config"
1418
"github.com/b4b4r07/afx/pkg/errors"
1519
"github.com/b4b4r07/afx/pkg/helpers/templates"
@@ -24,6 +28,8 @@ type selfUpdateCmd struct {
2428
meta
2529

2630
opt selfUpdateOpt
31+
32+
annotation map[string]string
2733
}
2834

2935
type selfUpdateOpt struct {
@@ -48,7 +54,12 @@ var (
4854

4955
// newSelfUpdateCmd creates a new selfUpdate command
5056
func newSelfUpdateCmd() *cobra.Command {
51-
c := &selfUpdateCmd{}
57+
info := color.New(color.FgGreen).SprintFunc()
58+
c := &selfUpdateCmd{
59+
annotation: map[string]string{
60+
"0.1.11": info(`Run "afx state refresh --force" at first!`),
61+
},
62+
}
5263

5364
selfUpdateCmd := &cobra.Command{
5465
Use: "self-update",
@@ -196,6 +207,36 @@ func (c *selfUpdateCmd) run(args []string) error {
196207
return errors.Wrap(err, "error occurred while updating binary")
197208
}
198209

199-
color.New(color.Bold).Printf("Successfully updated to version %s", latest.Version())
210+
color.New(color.Bold).Printf("Successfully updated to version %s\n", latest.Version())
211+
212+
var vs []*semver.Version
213+
for v := range c.annotation {
214+
vs = append(vs, semver.MustParse(v))
215+
}
216+
sort.Sort(semver.Collection(vs))
217+
218+
var messages []string
219+
for _, v := range vs {
220+
start := semver.MustParse(Version)
221+
stop := semver.MustParse(latest.Version())
222+
223+
log.Printf("[DEBUG] (self-update) Current version: %s", start)
224+
log.Printf("[DEBUG] (self-update) Next version: %s", v)
225+
226+
if stop.LessThan(v) {
227+
break
228+
}
229+
230+
if v.GreaterThan(start) {
231+
messages = append(messages, "- "+c.annotation[v.String()])
232+
}
233+
}
234+
235+
if len(messages) > 0 {
236+
fmt.Printf("\nTo use %q version:\n%s\n",
237+
latest.Version(),
238+
strings.Join(messages, "\n"))
239+
}
240+
200241
return nil
201242
}

cmd/state.go

+72-25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55

6+
"github.com/AlecAivazis/survey/v2"
67
"github.com/b4b4r07/afx/pkg/errors"
78
"github.com/b4b4r07/afx/pkg/helpers/templates"
89
"github.com/fatih/color"
@@ -11,6 +12,12 @@ import (
1112

1213
type stateCmd struct {
1314
meta
15+
16+
opt stateOpt
17+
}
18+
19+
type stateOpt struct {
20+
force bool
1421
}
1522

1623
var (
@@ -23,8 +30,10 @@ var (
2330

2431
// newStateCmd creates a new state command
2532
func newStateCmd() *cobra.Command {
33+
c := &stateCmd{}
34+
2635
stateCmd := &cobra.Command{
27-
Use: "state [list|refresh]",
36+
Use: "state [list|refresh|remove]",
2837
Short: "Advanced state management",
2938
Long: stateLong,
3039
Example: stateExample,
@@ -33,68 +42,106 @@ func newStateCmd() *cobra.Command {
3342
SilenceErrors: true,
3443
Args: cobra.MaximumNArgs(1),
3544
Hidden: true,
45+
PostRunE: func(cmd *cobra.Command, args []string) error {
46+
return c.meta.init(args)
47+
},
3648
}
3749

38-
stateListCmd := newStateListCmd()
39-
stateRefreshCmd := newStateRefreshCmd()
40-
stateRefreshCmd.Flags().BoolP("force", "", false, "Force update")
41-
4250
stateCmd.AddCommand(
43-
stateListCmd,
44-
stateRefreshCmd,
51+
c.newStateListCmd(),
52+
c.newStateRefreshCmd(),
53+
c.newStateRemoveCmd(),
4554
)
4655

4756
return stateCmd
4857
}
4958

50-
func newStateListCmd() *cobra.Command {
51-
c := &stateCmd{}
52-
59+
func (c stateCmd) newStateListCmd() *cobra.Command {
5360
return &cobra.Command{
5461
Use: "list",
5562
Short: "List your state items",
5663
DisableFlagsInUseLine: true,
5764
SilenceUsage: true,
5865
SilenceErrors: true,
59-
Args: cobra.MaximumNArgs(0),
66+
Args: cobra.ExactArgs(0),
6067
PreRunE: func(cmd *cobra.Command, args []string) error {
6168
return c.meta.init(args)
6269
},
63-
Run: func(cmd *cobra.Command, args []string) {
64-
for _, pkg := range c.State.NoChanges {
65-
fmt.Println(pkg.GetName())
70+
RunE: func(cmd *cobra.Command, args []string) error {
71+
items, err := c.State.List()
72+
if err != nil {
73+
return err
6674
}
75+
for _, item := range items {
76+
fmt.Println(item)
77+
}
78+
return nil
6779
},
6880
}
6981
}
7082

71-
func newStateRefreshCmd() *cobra.Command {
72-
c := &stateCmd{}
73-
74-
return &cobra.Command{
83+
func (c stateCmd) newStateRefreshCmd() *cobra.Command {
84+
cmd := &cobra.Command{
7585
Use: "refresh",
7686
Short: "Refresh your state file",
7787
DisableFlagsInUseLine: true,
7888
SilenceUsage: true,
7989
SilenceErrors: true,
80-
Args: cobra.MaximumNArgs(0),
90+
Args: cobra.ExactArgs(0),
8191
PreRunE: func(cmd *cobra.Command, args []string) error {
8292
return c.meta.init(args)
8393
},
8494
RunE: func(cmd *cobra.Command, args []string) error {
85-
force, err := cmd.Flags().GetBool("force")
86-
if err != nil {
87-
return err
88-
}
89-
if force {
95+
if c.opt.force {
9096
return c.State.New()
9197
}
92-
9398
if err := c.State.Refresh(); err != nil {
9499
return errors.Wrap(err, "failed to refresh state")
95100
}
96101
fmt.Println(color.WhiteString("Successfully refreshed"))
97102
return nil
98103
},
99104
}
105+
cmd.Flags().BoolVarP(&c.opt.force, "force", "", false, "force update")
106+
return cmd
107+
}
108+
109+
func (c stateCmd) newStateRemoveCmd() *cobra.Command {
110+
return &cobra.Command{
111+
Use: "remove",
112+
Short: "Remove selected packages from state file",
113+
DisableFlagsInUseLine: true,
114+
SilenceUsage: true,
115+
SilenceErrors: true,
116+
Aliases: []string{"rm"},
117+
Args: cobra.MinimumNArgs(0),
118+
PreRunE: func(cmd *cobra.Command, args []string) error {
119+
return c.meta.init(args)
120+
},
121+
RunE: func(cmd *cobra.Command, args []string) error {
122+
var resources []string
123+
switch len(cmd.Flags().Args()) {
124+
case 0:
125+
list, err := c.State.List()
126+
if err != nil {
127+
return errors.Wrap(err, "failed to list state items")
128+
}
129+
var selected string
130+
if err := survey.AskOne(&survey.Select{
131+
Message: "Choose a package:",
132+
Options: list,
133+
}, &selected); err != nil {
134+
return errors.Wrap(err, "failed to get input from console")
135+
}
136+
resources = append(resources, selected)
137+
default:
138+
// TODO: check valid or invalid
139+
resources = cmd.Flags().Args()
140+
}
141+
for _, resource := range resources {
142+
c.State.Remove(resource)
143+
}
144+
return nil
145+
},
146+
}
100147
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.0.2
77
github.com/MakeNowJust/heredoc v1.0.0
88
github.com/MakeNowJust/heredoc/v2 v2.0.1
9+
github.com/Masterminds/semver/v3 v3.1.0
910
github.com/Netflix/go-expect v0.0.0-20190729225929-0e00d9168667 // indirect
1011
github.com/agext/levenshtein v1.2.2 // indirect
1112
github.com/creativeprojects/go-selfupdate v0.6.1

0 commit comments

Comments
 (0)