Skip to content

Commit 165face

Browse files
authored
feat/updating
* add version * feat/updating easter egg * add update logic for Windows (not finished) * figlet fix-ish * remove figlet entirely * add dists * remove figlet again * hope this works
1 parent fc802b9 commit 165face

File tree

4 files changed

+102
-21
lines changed

4 files changed

+102
-21
lines changed

dist/flight.exe

2 KB
Binary file not shown.

misc/flight.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/func/func.go

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package flightpkg
22

33
import (
4-
"bufio"
54
"encoding/json"
65
"fmt"
76
"io/ioutil"
87
"net/http"
98
"os"
9+
"runtime"
10+
"strconv"
11+
"strings"
1012

1113
grab "github.com/cavaliergopher/grab/v3"
1214
targz "github.com/walle/targz"
1315
)
1416

17+
// var version string = "2.0.3"
18+
var version string = "0.0.0"
19+
1520
func Install(args []string) {
1621
registry := "https://registry.yarnpkg.com/"
1722
// registry2 := "https://registry.npmmirror.com/"
@@ -59,20 +64,99 @@ func Uninstall(args []string) {
5964
fmt.Println("flight uninstall <pkg>")
6065
} else {
6166
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!")
62107
}
63108
}
64109

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)
74158
}
75-
fmt.Println()
159+
76160
}
77161

78162
func Help() {
@@ -82,5 +166,6 @@ flight help
82166
flight version
83167
flight install <pkg>
84168
flight uninstall <pkg>
85-
flight update`)
169+
flight update
170+
flight status`)
86171
}

src/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ import (
88

99
func main() {
1010
cli := os.Args[1:]
11-
funcs.Figlet()
1211
if len(cli) == 0 {
1312
fmt.Println("flight <command> [arguments]")
1413
} else {
15-
if cli[0] == "-h" {
14+
if cli[0] == "-h" || cli[0] == "--help" {
1615
funcs.Help()
1716
} else if cli[0] == "install" || cli[0] == "i" || cli[0] == "add" || cli[0] == "a" || cli[0] == "get" || cli[0] == "g" {
1817
funcs.Install(cli[1:])
1918
} else if cli[0] == "uninstall" || cli[0] == "u" || cli[0] == "remove" || cli[0] == "r" || cli[0] == "ui" {
2019
funcs.Uninstall(cli[1:])
20+
} else if cli[0] == "status" || cli[0] == "s" || cli[0] == "st" {
21+
funcs.Status()
22+
} else if cli[0] == "update" || cli[0] == "up" || cli[0] == "u" {
23+
funcs.Update()
2124
} else {
2225
fmt.Println("flight <command> [arguments]")
2326
}

0 commit comments

Comments
 (0)