88 "path/filepath"
99
1010 tea "github.com/charmbracelet/bubbletea"
11- "github.com/mritd/bubbles/progressbar"
12-
1311 "github.com/mitchellh/go-homedir"
1412)
1513
@@ -21,100 +19,104 @@ func install(dir string) error {
2119
2220 toolKitHome := filepath .Join (home , ".gitflow-toolkit" )
2321 toolKitPath := filepath .Join (dir , "gitflow-toolkit" )
24- //toolKitHooks := filepath.Join(toolKitHome, "hooks")
22+ toolKitHooks := filepath .Join (toolKitHome , "hooks" )
23+ links := linkPath (dir )
2524
26- m := & progressbar.Model {
27- Width : 40 ,
28- InitMessage : "Initializing, please wait..." ,
29- Stages : []progressbar.ProgressFunc {
30- func () (string , error ) {
31- err := os .RemoveAll (toolKitHome )
32- if err != nil {
33- return "" , fmt .Errorf ("💔 failed to remove dir: %s: %s" , toolKitHome , err )
34- }
35- return "✔ Clean install dir..." , nil
25+ m := stageModel {
26+ stages : []stage {
27+ {
28+ title : "Clean install dir..." ,
29+ f : func () error { return os .RemoveAll (toolKitHome ) },
3630 },
37- func () (string , error ) {
38- for _ , link := range linkPath (dir ) {
39- if _ , err := os .Lstat (link ); err == nil {
40- err := os .RemoveAll (link )
41- if err != nil {
42- return "" , fmt .Errorf ("💔 failed to remove symlink: %s: %s" , link , err )
31+ {
32+ title : "Clean symlinks..." ,
33+ f : func () error {
34+ for _ , link := range links {
35+ if _ , err := os .Lstat (link ); err == nil {
36+ err := os .RemoveAll (link )
37+ if err != nil {
38+ return fmt .Errorf ("💔 failed to remove symlink: %s: %s" , link , err )
39+ }
40+ } else if ! os .IsNotExist (err ) {
41+ return fmt .Errorf ("💔 failed to get symlink info: %s: %s" , link , err )
4342 }
44- } else if ! os .IsNotExist (err ) {
45- return "" , fmt .Errorf ("💔 failed to get symlink info: %s: %s" , link , err )
4643 }
47- }
48- return "✔ Clean symlinks..." , nil
44+ return nil
45+ },
4946 },
50- func () (string , error ) {
51- // ignore unset failed error
52- _ , _ = git ("config" , "--global" , "--unset" , "core.hooksPath" )
53- return "✔ Unset commit hooks..." , nil
47+ {
48+ title : "Unset commit hooks..." ,
49+ f : func () error {
50+ _ , _ = git ("config" , "--global" , "--unset" , "core.hooksPath" )
51+ return nil
52+ },
5453 },
55- func () (string , error ) {
56- err := os .MkdirAll (toolKitHome , 0755 )
57- if err != nil {
58- return "" , fmt .Errorf ("💔 failed to create toolkit home: %s" , err )
59- }
60- return "✔ Create toolkit home..." , nil
54+ {
55+ title : "Create toolkit home..." ,
56+ f : func () error {
57+ return os .MkdirAll (toolKitHome , 0755 )
58+ },
6159 },
62- func () (string , error ) {
63- binPath , err := exec .LookPath (os .Args [0 ])
64- if err != nil {
65- return "" , fmt .Errorf ("💔 failed to get bin file info: %s: %s" , os .Args [0 ], err )
66- }
60+ {
61+ title : "Install executable file..." ,
62+ f : func () error {
63+ binPath , err := exec .LookPath (os .Args [0 ])
64+ if err != nil {
65+ return fmt .Errorf ("💔 failed to get bin file info: %s: %s" , os .Args [0 ], err )
66+ }
6767
68- currentFile , err := os .Open (binPath )
69- if err != nil {
70- return "" , fmt .Errorf ("💔 failed to get bin file info: %s: %s" , binPath , err )
71- }
72- defer func () { _ = currentFile .Close () }()
68+ currentFile , err := os .Open (binPath )
69+ if err != nil {
70+ return fmt .Errorf ("💔 failed to get bin file info: %s: %s" , binPath , err )
71+ }
72+ defer func () { _ = currentFile .Close () }()
7373
74- installFile , err := os .OpenFile (filepath .Join (dir , "gitflow-toolkit" ), os .O_CREATE | os .O_RDWR | os .O_TRUNC , 0755 )
75- if err != nil {
76- return "" , fmt .Errorf ("💔 failed to create bin file: %s: %s" , filepath .Join (toolKitHome , "gitflow-toolkit" ), err )
77- }
78- defer func () { _ = installFile .Close () }()
74+ installFile , err := os .OpenFile (filepath .Join (dir , "gitflow-toolkit" ), os .O_CREATE | os .O_RDWR | os .O_TRUNC , 0755 )
75+ if err != nil {
76+ return fmt .Errorf ("💔 failed to create bin file: %s: %s" , filepath .Join (toolKitHome , "gitflow-toolkit" ), err )
77+ }
78+ defer func () { _ = installFile .Close () }()
7979
80- _ , err = io .Copy (installFile , currentFile )
81- if err != nil {
82- return "" , fmt .Errorf ("💔 failed to copy file: %s: %s" , filepath .Join (toolKitHome , "gitflow-toolkit" ), err )
83- }
84- return "✔ Install executable file..." , nil
80+ _ , err = io .Copy (installFile , currentFile )
81+ if err != nil {
82+ return fmt .Errorf ("💔 failed to copy file: %s: %s" , filepath .Join (toolKitHome , "gitflow-toolkit" ), err )
83+ }
84+ return nil
85+ },
86+ },
87+ {
88+ title : "Create symlink..." ,
89+ f : func () error {
90+ for _ , link := range links {
91+ err := os .Symlink (toolKitPath , link )
92+ if err != nil {
93+ return fmt .Errorf ("💔 failed to create symlink: %s: %s" , link , err )
94+ }
95+ }
96+ return nil
97+ },
8598 },
86- func () (string , error ) {
87- for _ , link := range linkPath (dir ) {
88- err := os .Symlink (toolKitPath , link )
99+ {
100+ title : "Set commit hooks..." ,
101+ f : func () error {
102+ err := os .MkdirAll (toolKitHooks , 0755 )
89103 if err != nil {
90- return "" , fmt .Errorf ("💔 failed to create symlink : %s: %s" , link , err )
104+ return fmt .Errorf ("💔 failed to create hooks dir : %s: %s" , toolKitHooks , err )
91105 }
92- }
93- return "✔ Create symlink..." , nil
106+ err = os .Symlink (toolKitPath , filepath .Join (toolKitHooks , "commit-msg" ))
107+ if err != nil {
108+ return fmt .Errorf ("💔 failed to create commit hook synlink: %s: %s" , filepath .Join (toolKitHooks , "commit-msg" ), err )
109+ }
110+ _ , _ = git ("config" , "--global" , "core.hooksPath" , toolKitHooks )
111+ return nil
112+ },
94113 },
95- //func() (string, error) {
96- // err := os.MkdirAll(toolKitHooks, 0755)
97- // if err != nil {
98- // return "", fmt.Errorf("💔 failed to create hooks dir: %s: %s", toolKitHooks, err)
99- // }
100- // err = os.Symlink(toolKitPath, filepath.Join(toolKitHooks, "commit-msg"))
101- // if err != nil {
102- // return "", fmt.Errorf("💔 failed to create commit hook synlink: %s: %s", filepath.Join(toolKitHooks, "commit-msg"), err)
103- // }
104- // err = gitCommand(ioutil.Discard, []string{"config", "--global", "core.hooksPath", toolKitHooks})
105- // if err != nil {
106- // return "", fmt.Errorf("💔 failed to set commit hooks: %s", err)
107- // }
108- // return "✔ Set commit hooks...", nil
109- //},
110- func () (string , error ) {
111- //_, err := git("test")
112- //if err != nil {
113- // return "", fmt.Errorf("💔 install failed: %s", err)
114- //}
115- return "✔ Install success..." , nil
114+ {
115+ title : "Install success..." ,
116+ f : func () error { return nil },
116117 },
117118 },
119+ spinner : stageSpinner ,
118120 }
119121
120122 return tea .NewProgram (m ).Start ()
@@ -128,53 +130,52 @@ func uninstall(dir string) error {
128130
129131 toolKitHome := filepath .Join (home , ".gitflow-toolkit" )
130132 toolKitPath := filepath .Join (dir , "gitflow-toolkit" )
133+ links := linkPath (dir )
131134
132- m := & progressbar.Model {
133- Width : 40 ,
134- InitMessage : "Initializing, please wait..." ,
135- Stages : []progressbar.ProgressFunc {
136- func () (string , error ) {
137- err := os .RemoveAll (toolKitHome )
138- if err != nil {
139- return "" , fmt .Errorf ("💔 failed to remove dir: %s: %s" , toolKitHome , err )
140- }
141- return "✔ Clean install dir..." , nil
135+ m := stageModel {
136+ spinner : stageSpinner ,
137+ stages : []stage {
138+ {
139+ title : "Clean install dir..." ,
140+ f : func () error { return os .RemoveAll (toolKitHome ) },
142141 },
143- func () (string , error ) {
144- for _ , link := range linkPath (dir ) {
145- if _ , err := os .Lstat (link ); err == nil {
146- err := os .RemoveAll (link )
147- if err != nil {
148- return "" , fmt .Errorf ("💔 failed to remove symlink: %s: %s" , link , err )
142+ {
143+ title : "Clean symlinks..." ,
144+ f : func () error {
145+ for _ , link := range links {
146+ if _ , err := os .Lstat (link ); err == nil {
147+ err := os .RemoveAll (link )
148+ if err != nil {
149+ return fmt .Errorf ("💔 failed to remove symlink: %s: %s" , link , err )
150+ }
151+ } else if ! os .IsNotExist (err ) {
152+ return fmt .Errorf ("💔 failed to get symlink info: %s: %s" , link , err )
149153 }
150- } else if ! os .IsNotExist (err ) {
151- return "" , fmt .Errorf ("💔 failed to get symlink info: %s: %s" , link , err )
152154 }
153- }
154- return "✔ Clean symlinks..." , nil
155+ return nil
156+ },
155157 },
156- func () (string , error ) {
157- err := os .RemoveAll (toolKitPath )
158- if err != nil {
159- return "" , fmt .Errorf ("💔 failed to remove bin file: %s: %s" , toolKitPath , err )
160- }
161- return "✔ Clean bin file..." , nil
158+ {
159+ title : "Clean bin file..." ,
160+ f : func () error {
161+ return os .Remove (toolKitPath )
162+ },
162163 },
163- func () (string , error ) {
164- // ignore unset failed error
165- _ , _ = git ("config" , "--global" , "--unset" , "core.hooksPath" )
166- return "✔ Unset commit hooks..." , nil
164+ {
165+ title : "Unset commit hooks..." ,
166+ f : func () error {
167+ _ , _ = git ("config" , "--global" , "--unset" , "core.hooksPath" )
168+ return nil
169+ },
167170 },
168- func () (string , error ) {
169- _ , err := git ("test" )
170- if err == nil {
171- return "" , fmt .Errorf ("💔 uninstall failed: %s" , err )
172- }
173- return "✔ UnInstall success..." , nil
171+ {
172+ title : "UnInstall success..." ,
173+ f : func () error {
174+ return nil
175+ },
174176 },
175177 },
176178 }
177-
178179 return tea .NewProgram (m ).Start ()
179180}
180181
0 commit comments