From 580c0bf60bb5f52a428e63ea558528cfc7decc96 Mon Sep 17 00:00:00 2001 From: Frederic CORDIER Date: Fri, 17 Oct 2025 14:23:28 +0200 Subject: [PATCH] feat: create pkg package and remove unused code --- apploader/csv-app/csv-app.go | 62 +--------------- apploader/pkg/conversion/conversion.go | 47 +++++++++++++ .../{util/file/base.go => pkg/file/file.go} | 17 ++--- apploader/util/const.go | 7 -- apploader/util/sys/cmd_actuator.go | 70 ------------------- 5 files changed, 54 insertions(+), 149 deletions(-) create mode 100644 apploader/pkg/conversion/conversion.go rename apploader/{util/file/base.go => pkg/file/file.go} (50%) delete mode 100644 apploader/util/const.go delete mode 100644 apploader/util/sys/cmd_actuator.go diff --git a/apploader/csv-app/csv-app.go b/apploader/csv-app/csv-app.go index 4b593f9..54e697e 100644 --- a/apploader/csv-app/csv-app.go +++ b/apploader/csv-app/csv-app.go @@ -7,16 +7,15 @@ package csvapp */ import "C" import ( + "apploader/pkg/conversion" + "apploader/pkg/file" "apploader/secret_server" - "apploader/util/file" - "encoding/json" "fmt" "io/ioutil" "log" "os" "os/exec" "path" - "strconv" "strings" "syscall" "text/template" @@ -297,7 +296,7 @@ func CreateSevers(ca *TaskInfo) error { return fmt.Errorf("user env format error") } for k, v := range userEnv { - value := Interface2string(v) + value := conversion.Interface2String(v) envs = append(envs, fmt.Sprintf("%s=%s", k, value)) } } @@ -386,58 +385,3 @@ func startTask(tasks []*TaskInfo) { } } } - -func Interface2string(value interface{}) string { - var key string - if value == nil { - return key - } - - switch value.(type) { - case float64: - ft := value.(float64) - key = strconv.FormatFloat(ft, 'f', -1, 64) - case float32: - ft := value.(float32) - key = strconv.FormatFloat(float64(ft), 'f', -1, 64) - case int: - it := value.(int) - key = strconv.Itoa(it) - case uint: - it := value.(uint) - key = strconv.Itoa(int(it)) - case int8: - it := value.(int8) - key = strconv.Itoa(int(it)) - case uint8: - it := value.(uint8) - key = strconv.Itoa(int(it)) - case int16: - it := value.(int16) - key = strconv.Itoa(int(it)) - case uint16: - it := value.(uint16) - key = strconv.Itoa(int(it)) - case int32: - it := value.(int32) - key = strconv.Itoa(int(it)) - case uint32: - it := value.(uint32) - key = strconv.Itoa(int(it)) - case int64: - it := value.(int64) - key = strconv.FormatInt(it, 10) - case uint64: - it := value.(uint64) - key = strconv.FormatUint(it, 10) - case string: - key = value.(string) - case []byte: - key = string(value.([]byte)) - default: - newValue, _ := json.Marshal(value) - key = string(newValue) - } - - return key -} diff --git a/apploader/pkg/conversion/conversion.go b/apploader/pkg/conversion/conversion.go new file mode 100644 index 0000000..5124e81 --- /dev/null +++ b/apploader/pkg/conversion/conversion.go @@ -0,0 +1,47 @@ +package conversion + +import ( + "encoding/json" + "strconv" +) + +// Interface2String converts interface{} to string +func Interface2String(value interface{}) string { + if value == nil { + return "" + } + + switch v := value.(type) { + case float64: + return strconv.FormatFloat(v, 'f', -1, 64) + case float32: + return strconv.FormatFloat(float64(v), 'f', -1, 64) + case int: + return strconv.Itoa(v) + case uint: + return strconv.Itoa(int(v)) + case int8: + return strconv.Itoa(int(v)) + case uint8: + return strconv.Itoa(int(v)) + case int16: + return strconv.Itoa(int(v)) + case uint16: + return strconv.Itoa(int(v)) + case int32: + return strconv.Itoa(int(v)) + case uint32: + return strconv.Itoa(int(v)) + case int64: + return strconv.FormatInt(v, 10) + case uint64: + return strconv.FormatUint(v, 10) + case string: + return v + case []byte: + return string(v) + default: + newValue, _ := json.Marshal(value) + return string(newValue) + } +} diff --git a/apploader/util/file/base.go b/apploader/pkg/file/file.go similarity index 50% rename from apploader/util/file/base.go rename to apploader/pkg/file/file.go index d78b6b2..050d011 100644 --- a/apploader/util/file/base.go +++ b/apploader/pkg/file/file.go @@ -2,25 +2,16 @@ package file import "os" +// Exists checks if a file or directory exists func Exists(path string) bool { - _, err := os.Stat(path) //os.Sta + _, err := os.Stat(path) if err != nil { - if os.IsExist(err) { - return true - } - return false + return os.IsExist(err) } return true } -func IsDir(path string) bool { - s, err := os.Stat(path) - if err != nil { - return false - } - return s.IsDir() -} - +// IsFile checks if a path is a file func IsFile(path string) bool { s, err := os.Stat(path) if err != nil { diff --git a/apploader/util/const.go b/apploader/util/const.go deleted file mode 100644 index 0ecb45d..0000000 --- a/apploader/util/const.go +++ /dev/null @@ -1,7 +0,0 @@ -package util - -var ( - AppLabelKey = "" - NamespaceLabelKey = "" - PodAnnotationControllerKindLabelKey = "" -) diff --git a/apploader/util/sys/cmd_actuator.go b/apploader/util/sys/cmd_actuator.go deleted file mode 100644 index 8d03da2..0000000 --- a/apploader/util/sys/cmd_actuator.go +++ /dev/null @@ -1,70 +0,0 @@ -package sys - -import ( - "bufio" - "bytes" - "io" - "os" - "os/exec" - "syscall" -) - -func ActuatorCmdWithResult(name string, arg []string) (stdout string, stderr string, err error) { - cmd := exec.Command(name, arg...) - stdoutBuf := bytes.Buffer{} - stderrBuf := bytes.Buffer{} - cmd.Stdout = &stdoutBuf // - cmd.Stderr = &stderrBuf // - err = cmd.Run() - if err != nil { - return "", "", err - } - - return string(stdoutBuf.Bytes()), string(stderrBuf.Bytes()), nil -} - -func ActuatorCmdWithErrorCode(name string, arg ...string) (code int) { - command := exec.Command(name, arg...) - outinfo := bytes.Buffer{} - command.Stdout = &outinfo - var cmdExitStatus int - err := command.Run() - if err != nil { - if ex, ok := err.(*exec.ExitError); ok { - cmdExitStatus = ex.Sys().(syscall.WaitStatus).ExitStatus() - } - } - return cmdExitStatus -} - -func RunCmdWithLog(logPath string, name string, arg ...string) error { - cmd := exec.Command(name, arg...) - - stdout, _ := cmd.StdoutPipe() - stderr, _ := cmd.StderrPipe() - multiReader := io.MultiReader(stdout, stderr) - readerstd := bufio.NewReader(multiReader) - - // - if err := cmd.Start(); err != nil { - return err - } - - for { - stdout, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) - if err != nil { - return err - } - lineStd, err2 := readerstd.ReadString('\n') - if err2 != nil || io.EOF == err2 { - break - } - stdout.WriteString(lineStd) - stdout.Close() - } - err := cmd.Wait() - if err != nil { - return err - } - return nil -}