Go: strange results when using strings with exec.Command - linux

I have a Go function that processes Linux CLI commands and their arguments:
func cmd(cmd string, args ...string) ([]byte, error) {
path, err := exec.Command("/usr/bin/which", cmd).Output()
if err != nil {
return []byte(""), err
}
response, err := exec.Command(string(path), args...).Output()
if err != nil {
response = []byte("Unknown")
}
return response, err
}
Which is called by the following:
func main() {
uname, err := cmd("uname", "-a")
fmt.Println(string(uname))
}
The "which" command returns the correct path to the binary but when it tries to run the second exec command with a dynamic path the return is always:
fork/exec /usr/bin/uname
: no such file or directory
exit status 1
Yet if the second exec command is hardcoded, everything works as expected and prints the uname:
response, err := exec.Command("/usr/bin/uname", args...).Output()
Am I missing something about how exec and strings behave?
Thanks

The which command prints a newline following the name of the executable. The path variable is set to "/usr/bin/uname\n". There is no executable with this path. The extra newline is visible in the error message (the newline just before the ":").
Trim the newline suffix to get the correct name of the executable:
response, err := exec.Command(strings.TrimSuffix(string(path), "\n"), args...).Output()

Related

Golang chmod files and directories recursively [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I'm trying to use WalkDir with Chmod to recursively change the permission of a directory and its files and subdirectories. Somehow it does just change the first file or directory of the given path and stops afterwards. Can someone spot the mistake? getFileMode just converts the string "755" in os.FileMode(0755) and returns it.
func ChmodRec(path string, di fs.DirEntry, err error) error {
fileMode, err2 := getFileMode(os.Getenv("CHMOD_MODE"))
if err2 != nil {
log.Fatal("Could not set file mode for chmodding", path)
panic(err)
}
err2 = os.Chmod(path, fileMode)
if err2 != nil {
fmt.Println("Could not chmod", path)
panic(err)
}
fmt.Println("Changing mode of", path)
return nil
}
func ChmodRecursive(path string, mode string) {
os.Setenv("CHMOD_MODE", mode)
err := filepath.WalkDir(path, ChmodRec)
if err != nil {
log.Fatal("Could not chmod recursively ", path)
panic(err)
}
}
func main() {
path := "bla/test/"
mode := "755"
ChmodRecursive(path, mode)
}
Your code does not check err argument in ChmodRec. This is an extract from official documentation:
WalkDir calls the function with a non-nil err argument in two cases.
First, if the initial fs.Stat on the root directory fails, WalkDir
calls the function with path set to root, d set to nil, and err set to
the error from fs.Stat.
Second, if a directory's ReadDir method fails, WalkDir calls the
function with path set to the directory's path, d set to an
fs.DirEntry describing the directory, and err set to the error from
ReadDir. In this second case, the function is called twice with the
path of the directory: the first call is before the directory read is
attempted and has err set to nil, giving the function a chance to
return SkipDir and avoid the ReadDir entirely. The second call is
after a failed ReadDir and reports the error from ReadDir. (If ReadDir
succeeds, there is no second call.)
Add this code to the beginning of the function. It can give you a hint:
func ChmodRec(path string, di fs.DirEntry, err error) error {
if err != nil {
log.Fatal(err)
}

How to restart itself in Go daemon process?

I use go-daemon library to fork process and run it in background. And I need to restart the daemon process after update performed from within http handler.
The handler code is
func httpUpdate(w http.ResponseWriter, req *http.Request) {
if !isPost(req.Method) {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
if checkAuth(req) != 200 {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
log.Println("INFO: Update request, checking for update...")
var err = doUpdate(UPDATE_URL, nil, false)
if !isError(err) {
log.Println("INFO: Update successful, exit")
var system = RealSystem{}
system.Run(fmt.Sprintf("(sleep 0.3s && %s start &> /test/extra.log)&disown", appFilename()))
system.Exit(0)
return
}
w.WriteHeader(http.StatusNoContent)
}
doUpdate() returns nil if successfully replaced the executable file. RealSystem is just wrapper for exec.Command and os.Exit(). appFilename() is the executable file name. The command to start app is /path/to/app start.
I see that new process starts, but executing Context::Reborn() fails with EOF error. Looks like some intrinsic pipes used as implementation details fail with EOF (may be...).
What would be the reason? Or may be there is a better way of doing that?
For now everything happens inside docker container in the "context" of e2e test if it matters. I spent hours trying to make it work but with no success.
I assume you mean restarting the currently running Go binary. You can use a syscall for unix-based systems, and use an exec.Command for Windows.
func RestartSelf() error {
self, err := osext.Executable()
if err != nil {
return err
}
args := os.Args
env := os.Environ()
// Windows does not support exec syscall.
if runtime.GOOS == "windows" {
cmd := exec.Command(self, args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = env
err := cmd.Run()
if err == nil {
os.Exit(0)
}
return err
}
return syscall.Exec(self, args, env)
}
The issue is specific to the library. Spawn new self instance from within child process is not a problem for the system, but for that library.
To achieve this it's necessary to execute something like that.
Note the _GO_DAEMON=0 variable set to zero. This makes library follow parent control flow.
var cmd = exec.Command("bash", "-c", fmt.Sprintf("sleep 0.5s; _GO_DAEMON=0 %s start", appFilename()))
var err = cmd.Start()
Also it was necessary to make small changes to the original library. Here is the fork.

using curl with commands in go

I'm using Go with command to execute curl which works as expected
curl := exec.Command("curl", "https://services.odata.org/V3/northwind/northwind.svc/")
out, err := curl.Output()
if err != nil {
fmt.Println("erorr" , err)
return
}
fmt.Println(out)
Now I want to use some placeholders like
curl -O http://quiet-waters-1228.herokuapp.com/assets/image.jpg
but now I need to get the url for command
e.g. if I run in bash mytool url I got the url value
`curl -O $(mytool url)`
The problem is that we need to execute the command in the code and I'm not sure how to pass it
curl := exec.Command("curl", "curl -O $(url)")
out, err := curl.Output()
if err != nil {
fmt.Println("erorr" , err)
return
}
fmt.Println(out)
In os package you have slice of strings which contains all arguments passed by shell to your program.
os.Args 0th value, i.e., first element is going to be name of the command itself.
If your tool command is mytool, os.Args[0] contains mytool.
Rest are going to be the arguments, which are passed by shell.
package main
import (
"log"
"os"
"os/exec"
)
func main() {
if len(os.Args) < 2 {
// If argument is not provided quit
log.Fatalln("url not provided")
}
url := os.Args[1] // URL
cmd := exec.Command("curl", "-O", url)
cmd.Run()
}
You can also download multiple URLs concurrently,
var wg *sync.WaitGroup
func main() {
urls := os.Args[1:]
wg = new(sync.WaitGroup)
wg.Add(len(urls))
for _, url := range urls {
go download(url)
}
wg.Wait()
}
func download(url string) {
defer wg.Done()
cmd := exec.Command("curl", "-O", url)
cmd.Run()
}

Answer to password shell prompt in golang

I am searching for a way to answer to a shell password prompt in golang.
like :
bussiere#kus:~/Workspace/rteest$ ./passwordtest.sh
Password :
I would like to enter the password automatically with my token in golang after launching a shell command / script ...
I've made some script that get a one time token with mfa if everything is ok (in golang). So i need to enter the tempory token to a linux password prompt.
I know that there is the expect command but i would like to compile my program to embed it and have minimal depency.
Thanks and regards
edit thks to #nevermore i've tried this (but it doesn't work) : https://play.golang.org/p/Ffm3q5h636
package main
import (
"os/exec"
"fmt"
"log"
"io"
)
func main() {
cmdb := "git"
args := "clone https://bb#gitlab.com/bb/fzgs.git"
cmd := exec.Command(cmdb, args)
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
go func() {
defer stdin.Close()
io.WriteString(stdin, "QSRDFGHJfZERTYU")
}()
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", out)
}
it gives me this :
2017/05/12 20:42:36 exit status 1
exit status 1
edit bis :
cmdb := "git"
args := "https://bb#gitlab.com/bb/fzgs.git"
cmd := exec.Command(cmdb, "clone", args)
it asks for the password :
Password for 'https://bb#gitlab.com':
The problem is that you are trying to send the password before Git asks you
to do so.
And the way Git asks for password depends on whether credential helper is set. If the credential helper is not set, or doesn't return a password, the user is asked for input.
Solution #1: Include Password into URI
In most cases (at least with Github and Bitbucket) the Git server accepts
credentials passed via HTTPS URI, e.g. https://user:password#domain.com/repo.git.
cmd := exec.Command("git", "clone", "https://bb:PASSWORD#gitlab.com/bb/fzgs.git")
env := os.Environ()
env = append(env, "GIT_ASKPASS=")
cmd.Env = env
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
Solution #2: Custom Credential Helper
Another way is to make a credential helper script, e.g.:
#!/bin/bash -
printf '%s\n' 'YOUR_PASSWORD'
and pass it via GIT_ASKPASS:
cmd := exec.Command("git", "clone", "https://bb#gitlab.com/bb/fzgs.git")
env := os.Environ()
env = append(env, "GIT_ASKPASS=/path/to/fzgs-askpass.sh")
cmd.Env = env
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
If a user is asked for a password in their shell, you can use golang to write to the io.Writer that is os.Stdin
Properly passing data on stdin to a command and receiving data from stdout of that command in golang
os.Stdin is a os.File created by
NewFile(uintptr(syscall.Stdin), "/dev/stdin")
And the password prompt will be reading from this file
You can execute ./passwordtest.sh as in your question, and then write the password to os.Stdin, and the bash script should accept the bytes written by golang as the password.
Alternatively is actually a method for this in the exec package for the Cmd type.
Use cmd to execute your shell script
Input the password using stdin, or cmd.StdinPip()
Read the shells output using cmd.CombinedOutput()
Cmd represents an external command being prepared or run.
https://golang.org/pkg/os/exec/#Cmd.StdinPipe
cmd := exec.Command("cat")
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
go func() {
defer stdin.Close()
io.WriteString(stdin, "values written to stdin are passed to cmd's standard input")
}()
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", out)
StdinPipe returns a pipe that will be connected to the command's standard input when the command starts. The pipe will be closed automatically after Wait sees the command exit. A caller need only call Close to force the pipe to close sooner. For example, if the command being run will not exit until standard input is closed, the caller must close the pipe.
Also, your cmd arguments shouldn't combine clone and the url, try instead
cmd := exec.Command(cmdb, "clone", url)

Golang tarring directory

I am having trouble figuring out how to write a directory into tarball's headerinfo. For example, I have this directory structure:
test [dir]
-- test1.txt
-- subDirA [sub dir]
-- test2.txt
If I tar up test using my Go tar executable, it will untar and have the original structure. If I do a tar -tvf on the tarball, the listing will be:
test/test1.txt
test/subDirA/test2.txt
However if I manually do a tar -cvf of test and then do a tar -tvf, the listing will be:
test/
test/test1.txt
test/subDirA/
test/subDirA/test2.txt
This is what I want my Go tarring program to do as well. In my Go program, I tried to add a new method writeDirToTar to deal with adding directories into the tarWriter. I call either writeFileToTar or writeDirToTar from a fileWalk function. I am getting a "test/subDirA is a directory" error when I call io.Copy(tarWriter, file) in writeDirToTar, which kind of makes sense. Is there a workaround for this?
func writeToTar(fileDir string,
sourceBase string,
tarWriter *tar.Writer,
f os.FileInfo) error {
file, err := os.Open(fileDir)
if err != nil {
log.Print("error opening file")
return err
}
defer file.Close()
// relative paths are used to preserve the directory paths in each file path
relativePath, err := filepath.Rel(sourceBase, fileDir)
tarheader := new(tar.Header)
tarheader.Name = relativePath
tarheader.Size = f.Size()
tarheader.Mode = int64(f.Mode())
tarheader.ModTime = f.ModTime()
err = tarWriter.WriteHeader(tarheader)
if err != nil {
log.Print("error writing tarHeader")
return err
}
_, err = io.Copy(tarWriter, file)
if err != nil {
log.Print("error writing to tarWriter")
return err
}
return nil
}
func writeDirToTar(fileDir string,
sourceBase string,
tarWriter *tar.Writer,
f os.FileInfo) error {
file, err := os.Open(fileDir)
if err != nil {
log.Print("error opening file")
return err
}
defer file.Close()
// relative paths are used to preserve the directory paths in each file path
relativePath, err := filepath.Rel(sourceBase, fileDir)
if tarHeader, err := tar.FileInfoHeader(f, relativePath); err != nil {
log.Print("error writing tarHeader")
return err
}
tarHeader.Name = relativePath
tarheader.Mode = int64(f.Mode())
tarheader.ModTime = f.ModTime()
_, err = io.Copy(tarWriter, file)
if err != nil {
log.Print("error writing to tarWriter")
return err
}
return nil
}
You're missing the Typeflag field in the tar.Header.
For the directory you'll want at least:
tarheader := &tar.Header{
Name: relativePath,
Mode: int64(f.Mode()),
ModTime: f.ModTime(),
Typeflag: tar.TypeDir,
}
You don't want a size here, nor do you want to copy the directory data, since the internal directory data structures mean nothing to tar.
You may also want to add a / to the filename to visually indicate it's a directory and match the POSIX tar behavior.
Alternatively you could just use the tar.FileInfoHeader function just like you did with the files to create the proper header structure.

Resources