1
1
package flightpkg
2
2
3
3
import (
4
- "bufio"
5
4
"encoding/json"
6
5
"fmt"
7
6
"io/ioutil"
8
7
"net/http"
9
8
"os"
9
+ "runtime"
10
+ "strconv"
11
+ "strings"
10
12
11
13
grab "github.com/cavaliergopher/grab/v3"
12
14
targz "github.com/walle/targz"
13
15
)
14
16
17
+ // var version string = "2.0.3"
18
+ var version string = "0.0.0"
19
+
15
20
func Install (args []string ) {
16
21
registry := "https://registry.yarnpkg.com/"
17
22
// registry2 := "https://registry.npmmirror.com/"
@@ -59,20 +64,99 @@ func Uninstall(args []string) {
59
64
fmt .Println ("flight uninstall <pkg>" )
60
65
} else {
61
66
os .RemoveAll (fmt .Sprintf ("./node_modules/%v" , args [0 ]))
67
+ fmt .Println ("Uninstalled " + args [0 ])
68
+
69
+ files , _ := ioutil .ReadDir ("./node_modules" )
70
+ if len (files ) == 0 {
71
+ os .Remove ("./node_modules" )
72
+ }
73
+ }
74
+ }
75
+
76
+ func Status () {
77
+ version_dotless , _ := strconv .ParseInt (strings .ReplaceAll (version , "." , "" ), 10 , 64 )
78
+ latest := "https://api.github.com/repos/flightpkg/flight-v2/releases/latest"
79
+ fmt .Printf ("Version: %v\n " , version )
80
+ resp , err := http .Get (latest )
81
+ if err != nil {
82
+ fmt .Println (err )
83
+ }
84
+
85
+ defer resp .Body .Close ()
86
+
87
+ body , err := ioutil .ReadAll (resp .Body )
88
+
89
+ if err != nil {
90
+ fmt .Println (err )
91
+ }
92
+
93
+ var data map [string ]interface {}
94
+
95
+ /* Getting the version of the package that is being installed. */
96
+ json .Unmarshal (body , & data )
97
+
98
+ latest_tag := data ["tag_name" ].(string )
99
+ latest_tag_dotless , _ := strconv .ParseInt (strings .Replace (latest_tag , "." , "" , - 1 ), 10 , 64 )
100
+
101
+ if int (version_dotless ) < int (latest_tag_dotless ) {
102
+ fmt .Printf ("Update Available:\n %v -> %v" , version , latest_tag )
103
+ } else if int (version_dotless ) > int (latest_tag_dotless ) {
104
+ fmt .Println ("You're a time traveler, aren't you?" )
105
+ } else {
106
+ fmt .Println ("Up to date!" )
62
107
}
63
108
}
64
109
65
- func Figlet () {
66
- // Open the file.
67
- f , _ := os .Open ("..\\ misc\\ flight.txt" )
68
- // Create a new Scanner for the file.
69
- scanner := bufio .NewScanner (f )
70
- // Loop over all lines in the file and print them.
71
- for scanner .Scan () {
72
- line := scanner .Text ()
73
- fmt .Println (line )
110
+ func Update () {
111
+ latest := "https://api.github.com/repos/flightpkg/flight-v2/releases/latest"
112
+ resp , err := http .Get (latest )
113
+ if err != nil {
114
+ fmt .Println (err )
115
+ }
116
+
117
+ defer resp .Body .Close ()
118
+
119
+ body , err := ioutil .ReadAll (resp .Body )
120
+
121
+ if err != nil {
122
+ fmt .Println (err )
123
+ }
124
+
125
+ var data map [string ]interface {}
126
+
127
+ /* Getting the version of the package that is being installed. */
128
+ json .Unmarshal (body , & data )
129
+ latest_tag := data ["tag_name" ].(string )
130
+ latest_tag_dotless , _ := strconv .ParseInt (strings .Replace (latest_tag , "." , "" , - 1 ), 10 , 64 )
131
+ version_dotless , _ := strconv .ParseInt (strings .ReplaceAll (version , "." , "" ), 10 , 64 )
132
+
133
+ fmt .Printf ("%v -> %v\n " , version , latest_tag )
134
+
135
+ op := runtime .GOOS
136
+ switch op {
137
+ case "windows" :
138
+ if int (version_dotless ) < int (latest_tag_dotless ) {
139
+ apd , _ := os .UserConfigDir ()
140
+ url := data ["assets" ].([]interface {})[0 ].(map [string ]interface {})["browser_download_url" ].(string )
141
+ os .Mkdir (apd + "/.flightpkg/bin" , 0777 )
142
+ os .Remove (fmt .Sprintf ("%v/.flightpkg/bin/flight.exe" , apd ))
143
+ _ , err := grab .Get (fmt .Sprintf ("%v/.flightpkg/bin/flight.exe" , apd ), url )
144
+ if err != nil {
145
+ fmt .Println (err )
146
+ }
147
+
148
+ fmt .Println ("Updated to " + latest_tag )
149
+ } else {
150
+ fmt .Println ("Up to date!" )
151
+ }
152
+ case "darwin" :
153
+ //TODO: Add MacOS support
154
+ case "linux" :
155
+ //TODO: Add Linux support
156
+ default :
157
+ fmt .Printf ("%s.\n " , op )
74
158
}
75
- fmt . Println ()
159
+
76
160
}
77
161
78
162
func Help () {
@@ -82,5 +166,6 @@ flight help
82
166
flight version
83
167
flight install <pkg>
84
168
flight uninstall <pkg>
85
- flight update` )
169
+ flight update
170
+ flight status` )
86
171
}
0 commit comments