英文:
`no such file or directory` with `os.Remove` inside go routine
问题
我使用Cobra框架为我的CLI应用程序添加了一个新的命令。该命令用于启动一个接受socket连接的TCP服务器。它接收一个有效路径(absolute
path)指向一个文件/目录,并尝试删除它。以下是该命令的代码:
package cmd
import (
"bufio"
"fmt"
"net"
"os"
"github.com/spf13/cobra"
"wpgenius.io/util"
)
var cachePurgerCmd = &cobra.Command{
Use: "cache-purger",
Short: "Listen for request to purge NGINX page cache",
Run: func(cmd *cobra.Command, args []string) {
dstream, err := net.Listen("tcp", ":9876")
if err != nil {
util.HandleError(err, "Can not start listener..")
return
}
fmt.Println("Listening for purge requests...")
defer dstream.Close()
for {
con, err := dstream.Accept()
if err != nil {
util.HandleError(err, "Can not accept connection")
os.Exit(1)
}
go handleRequest(con)
}
},
}
func handleRequest(con net.Conn) {
path, err := bufio.NewReader(con).ReadString('\n')
if err != nil {
return
}
defer con.Close()
err = os.Remove(path)
if err != nil {
con.Write([]byte("ERROR"))
fmt.Println(err)
util.HandleError(err, "Can not delete cache file")
return
}
con.Write([]byte("SUCCESS"))
}
func init() {
rootCmd.AddCommand(cachePurgerCmd)
}
尽管文件/目录存在,但仍然出现“no such file or directory”错误。我通过将os.Remove
添加到main
函数中进行了一次简单的检查,以确保问题不是与路径相关,并且我可以看到它成功删除了文件/目录。
我不确定这是否与go routing
或tcp server
有关!
非常感谢您的帮助!
英文:
I added a new command to my CLI application using the Cobra framework. This command is supposed to start a TCP server that accepts socket connections. It receives a payload which is an absolute
path to a file/directory and tries to delete it. Here is the code for the command
package cmd
import (
"bufio"
"fmt"
"net"
"os"
"github.com/spf13/cobra"
"wpgenius.io/util"
)
var cachePurgerCmd = &cobra.Command{
Use: "cache-purger",
Short: "Listen for request to purge NGINX page cache",
Run: func(cmd *cobra.Command, args []string) {
dstream, err := net.Listen("tcp", ":9876")
if err != nil {
util.HandleError(err, "Can not start listener..")
return
}
fmt.Println("Listening for purge requests...")
defer dstream.Close()
for {
con, err := dstream.Accept()
if err != nil {
util.HandleError(err, "Can not accept connection")
os.Exit(1)
}
go handleRequest(con)
}
},
}
func handleRequest(con net.Conn) {
path, err := bufio.NewReader(con).ReadString('\n')
if err != nil {
return
}
defer con.Close()
err = os.Remove(path)
if err != nil {
con.Write([]byte("ERROR"))
fmt.Println(err)
util.HandleError(err, "Can not delete cache file")
return
}
con.Write([]byte("SUCCESS"))
}
func init() {
rootCmd.AddCommand(cachePurgerCmd)
}
Although the file/directory exists, I still get no such file or directory
error.
I did a sanity check by simply adding the os.Remove
to the main
function to make sure it's not related to the path and I can see it successfully delete the file/directory.
I'm not sure if it has something to do with go routing
or with the tcp server
!
Any help will be highly appreciated!
答案1
得分: 0
我猜测你的意思是,在你输入的路径中,关键点在于...
英文:
I guess the point is \n in the path you input.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论