提交 00a827c2 编写于 作者: yanghye's avatar yanghye

U: command-line tool, http request api

上级 93612a21
......@@ -24,6 +24,7 @@ var commands = []*internal.Command{
internal.CmdVersion,
internal.CmdSetenv,
internal.CmdEnv,
internal.CmdCreate,
}
func main() {
......@@ -49,6 +50,8 @@ func main() {
cc.Index = 4
case "env":
cc.Index = 5
case "create":
cc.Index = 6
}
command := commands[cc.Index]
if len(extraArgs) < 1 || extraArgs[len(extraArgs)-1] != "." {
......
......@@ -11,3 +11,20 @@
// 创建 energy 项目
package internal
var CmdCreate = &Command{
UsageLine: "create",
Short: "create energy project",
Long: `
Initialize and create an energy golang project
. Execute default command
`,
}
func init() {
CmdCreate.Run = runCreate
}
func runCreate(c *CommandConfig) error {
return nil
}
......@@ -20,7 +20,6 @@ import (
progressbar "github.com/energye/energy/v2/cmd/internal/progress-bar"
"io"
"io/fs"
"io/ioutil"
"net/http"
"net/url"
"os"
......@@ -107,7 +106,7 @@ Your current installation environment is Linux and there are two GTK solutions a
os.MkdirAll(installPathName, fs.ModePerm)
os.MkdirAll(filepath.Join(c.Install.Path, frameworkCache), fs.ModePerm)
println("Start downloading CEF and Energy dependency")
downloadJSON, err := downloadConfig(DownloadVersionURL)
downloadJSON, err := httpRequestGET(DownloadVersionURL)
if err != nil {
fmt.Fprint(os.Stderr, err.Error()+"\n")
os.Exit(1)
......@@ -171,11 +170,13 @@ Your current installation environment is Linux and there are two GTK solutions a
downloadEnergyURL = strings.ReplaceAll(downloadEnergyURL, "{OSARCH}", libEnergyOS)
//提取文件配置
extractData, err := downloadConfig(DownloadExtractURL)
extractData, err := httpRequestGET(DownloadExtractURL)
if err != nil {
fmt.Fprint(os.Stderr, err.Error(), "\n")
os.Exit(1)
}
// 获取安装环境信息
var extractConfig map[string]interface{}
extractData = bytes.TrimPrefix(extractData, []byte("\xef\xbb\xbf"))
if err := json.Unmarshal(extractData, &extractConfig); err != nil {
......@@ -484,21 +485,6 @@ func urlName(downloadUrl string) string {
}
}
// 下载文件配置
func downloadConfig(url string) ([]byte, error) {
client := new(http.Client)
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ret, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return ret, nil
}
func isFileExist(filename string, filesize int64) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
......
......@@ -10,7 +10,11 @@
package internal
import "os"
import (
"io/ioutil"
"net/http"
"os"
)
func ToString(v interface{}) string {
if v == nil {
......@@ -38,3 +42,18 @@ func IsExist(path string) bool {
}
return true
}
// http 请求
func httpRequestGET(url string) ([]byte, error) {
client := new(http.Client)
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ret, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return ret, nil
}
......@@ -32,7 +32,7 @@ func init() {
}
func runVersion(c *CommandConfig) error {
downloadJSON, err := downloadConfig(DownloadVersionURL)
downloadJSON, err := httpRequestGET(DownloadVersionURL)
if err != nil {
fmt.Fprint(os.Stderr, err.Error()+"\n")
os.Exit(1)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册