for i in "$@"; do
if [ "${1}"x != "${i}"x ]; then
$cmd upload "$i" "${cos_base_path}${dir}/"
if [ $? -eq 0 ]; then
if [[ ${2} =~ /([^/]+).([^/]+)$ ]]; then
filenames+=("${base_url}${cos_base_path}${dir}/${BASH_REMATCH[1]}.${BASH_REMATCH[2]}")
fi
fi
fi
done
echo "Upload Success:"
for name in "${filenames[@]}"; do
echo $name
done
把这个shell改成golang语言
for i in "$@"; do
if [ "${1}"x != "${i}"x ]; then
$cmd upload "$i" "${cos_base_path}${dir}/"
if [ $? -eq 0 ]; then
if [[ ${2} =~ /([^/]+).([^/]+)$ ]]; then
filenames+=("${base_url}${cos_base_path}${dir}/${BASH_REMATCH[1]}.${BASH_REMATCH[2]}")
fi
fi
fi
done
echo "Upload Success:"
for name in "${filenames[@]}"; do
echo $name
done
把这个shell改成golang语言
func main() {
if len(os.Args) <= 1 {
fmt.Println("Please provide the text to translate.")
return
}
text := strings.TrimSpace(os.Args[1])
text = strings.ReplaceAll(text, " ", "")
salt := fmt.Sprintf("%d", time.Now().Unix())
sign := md5.Sum([]byte(appID + text + salt + apiKey))
query := fmt.Sprintf("%s?q=%s&from=zh&to=en&appid=%s&salt=%s&sign=%s",
baseURL, text, appID, salt, hex.EncodeToString(sign[:]))
resp, err := http.Get(query)
if err != nil {
fmt.Printf("Failed to get translation for %s: %v", text, err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response body for %s: %v", text, err)
return
}
var dst string
fmt.Sscanf(string(body), `{"error_code":0,"error_msg":"SUCCESS","from":"zh","to":"en","trans_result":[{"src":"%s","dst":"%s"}]}`,
&text, &dst)
dir := strings.ToLower(strings.ReplaceAll(dst, " ", "-"))
var filenames []stringfor _, arg := range os.Args[1:] {
if arg == os.Args[1] {
continue
}
absPath, err := filepath.Abs(arg)
if err != nil {
fmt.Printf("Failed to get absolute path for %s: %v", arg, err)
continue
}
if err := uploadToCos(absPath, filepath.Join(cosBasePath, dir)); err != nil {
fmt.Printf("Failed to upload %s: %v\n", arg, err)
continue
}
ext := filepath.Ext(arg)
filename := fmt.Sprintf("%s%s%s", baseURLPrefix, cosBasePath, filepath.Join(dir, strings.TrimSuffix(filepath.Base(absPath), ext)+ext))
filenames = append(filenames, filename)
}
fmt.Println("Upload Success:")
for _, name := range filenames {
fmt.Println(name)
}