A: energy command line, Set energy framework development environment

This commit is contained in:
杨红岩 2023-07-21 13:01:05 +08:00
parent 625b4fec0e
commit 8f05acb860
5 changed files with 140 additions and 73 deletions

View File

@ -22,6 +22,7 @@ var commands = []*internal.Command{
internal.CmdInstall,
internal.CmdPackage,
internal.CmdVersion,
internal.CmdSetenv,
}
func main() {
@ -43,6 +44,8 @@ func main() {
cc.Index = 2
case "version", "v":
cc.Index = 3
case "setenv":
cc.Index = 4
}
command := commands[cc.Index]
if len(extraArgs) < 1 || extraArgs[len(extraArgs)-1] != "." {

View File

@ -16,6 +16,7 @@ type CommandConfig struct {
Install Install `command:"install"`
Package Package `command:"package"`
Version Version `command:"version"`
Setenv Setenv `command:"setenv"`
}
type Install struct {
@ -31,6 +32,10 @@ type Package struct {
Out string `short:"o" long:"out" description:"Output directory" default:"EnergyInstallPkg"`
}
type Setenv struct {
Path string `short:"p" long:"path" description:"Energy framework dir"`
}
type Version struct {
All bool `short:"a" long:"all" description:"show all"`
}

View File

@ -18,15 +18,12 @@ import (
"encoding/json"
"fmt"
progressbar "github.com/energye/energy/v2/cmd/internal/progress-bar"
"github.com/energye/golcl/energy/homedir"
"github.com/energye/golcl/tools/command"
"io"
"io/fs"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
@ -240,76 +237,6 @@ Your current installation environment is Linux and there are two GTK solutions a
return nil
}
func setEnergyHomeEnv(key, value string) {
println("\nSetting environment Variables to ", value)
cmd := command.NewCMD()
cmd.MessageCallback = func(s []byte, e error) {
fmt.Println("CMD", s, " error", e)
}
if isWindows {
var args = []string{"/c", "setx", key, value}
cmd.Command("cmd.exe", args...)
} else {
var envFiles []string
var energyHomeKey = fmt.Sprintf("export %s", key)
var energyHome = fmt.Sprintf("export %s=%s", key, value)
if isLinux {
envFiles = []string{".profile", ".zshrc", ".bashrc"}
} else if isDarwin {
envFiles = []string{".profile", ".zshrc", ".bash_profile"}
}
homeDir, _ := homedir.Dir()
for _, file := range envFiles {
var fp = path.Join(homeDir, file)
cmd.Command("touch", fp)
f, err := os.OpenFile(fp, os.O_RDWR|os.O_APPEND, 0666)
if err == nil {
var oldContent string
if contentBytes, err := ioutil.ReadAll(f); err == nil {
content := string(contentBytes)
oldContent = content
var lines = strings.Split(content, "\n")
var exist = false
for i := 0; i < len(lines); i++ {
line := lines[i]
if strings.Index(line, energyHomeKey) == 0 {
content = strings.ReplaceAll(content, line, energyHome)
exist = true
}
}
if exist {
if err := f.Close(); err == nil {
var oldWrite = func() {
if f, err = os.OpenFile(fp, os.O_RDWR, 0666); err == nil {
f.WriteString(oldContent)
f.Close()
}
}
if newOpenFile, err := os.OpenFile(fp, os.O_RDWR|os.O_TRUNC, 0666); err == nil {
if _, err := newOpenFile.WriteString(content); err == nil {
newOpenFile.Close()
} else {
newOpenFile.Close()
oldWrite()
}
} else {
oldWrite()
}
}
} else {
f.WriteString("\n")
f.WriteString(energyHome)
f.WriteString("\n")
}
} else {
f.Close()
}
}
}
}
cmd.Close()
}
func cefOS() (string, bool) {
if isWindows { // windows arm for 64 bit, windows for 32/64 bit
if runtime.GOARCH == "arm64" {

117
cmd/internal/setenv.go Normal file
View File

@ -0,0 +1,117 @@
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package internal
import (
"errors"
"fmt"
"github.com/energye/golcl/energy/homedir"
"github.com/energye/golcl/tools/command"
"io/ioutil"
"os"
"path"
"strings"
)
var CmdSetenv = &Command{
UsageLine: "setenv -p [path]",
Short: "Set energy framework development environment",
Long: `
-p Set the Framework pointed to by the ENERGY_HOME development environment variable
. Execute default command
`,
}
func init() {
CmdSetenv.Run = runSetenv
}
func runSetenv(c *CommandConfig) error {
if c.Setenv.Path == "" {
return errors.New("ERROR: ENERGY environment variable, command line argument [-p] directory to empty")
}
if !IsExist(c.Setenv.Path) {
return errors.New("Directory [" + c.Setenv.Path + "] does not exist")
}
setEnergyHomeEnv(ENERGY_HOME_KEY, c.Setenv.Path)
println("SUCCESS")
return nil
}
func setEnergyHomeEnv(key, value string) {
println("\nSetting environment Variables to ", value)
cmd := command.NewCMD()
cmd.MessageCallback = func(s []byte, e error) {
fmt.Println("CMD", s, " error", e)
}
if isWindows {
var args = []string{"/c", "setx", key, value}
cmd.Command("cmd.exe", args...)
} else {
var envFiles []string
var energyHomeKey = fmt.Sprintf("export %s", key)
var energyHome = fmt.Sprintf("export %s=%s", key, value)
if isLinux {
envFiles = []string{".profile", ".zshrc", ".bashrc"}
} else if isDarwin {
envFiles = []string{".profile", ".zshrc", ".bash_profile"}
}
homeDir, _ := homedir.Dir()
for _, file := range envFiles {
var fp = path.Join(homeDir, file)
cmd.Command("touch", fp)
f, err := os.OpenFile(fp, os.O_RDWR|os.O_APPEND, 0666)
if err == nil {
var oldContent string
if contentBytes, err := ioutil.ReadAll(f); err == nil {
content := string(contentBytes)
oldContent = content
var lines = strings.Split(content, "\n")
var exist = false
for i := 0; i < len(lines); i++ {
line := lines[i]
if strings.Index(line, energyHomeKey) == 0 {
content = strings.ReplaceAll(content, line, energyHome)
exist = true
}
}
if exist {
if err := f.Close(); err == nil {
var oldWrite = func() {
if f, err = os.OpenFile(fp, os.O_RDWR, 0666); err == nil {
f.WriteString(oldContent)
f.Close()
}
}
if newOpenFile, err := os.OpenFile(fp, os.O_RDWR|os.O_TRUNC, 0666); err == nil {
if _, err := newOpenFile.WriteString(content); err == nil {
newOpenFile.Close()
} else {
newOpenFile.Close()
oldWrite()
}
} else {
oldWrite()
}
}
} else {
f.WriteString("\n")
f.WriteString(energyHome)
f.WriteString("\n")
}
} else {
f.Close()
}
}
}
}
cmd.Close()
}

View File

@ -10,6 +10,8 @@
package internal
import "os"
func ToString(v interface{}) string {
if v == nil {
return ""
@ -23,3 +25,16 @@ func ToRNilString(v interface{}, new string) string {
}
return v.(string)
}
func IsExist(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
} else if os.IsNotExist(err) {
return false
}
return false
}
return true
}