exec not running command from Golang application - go

I am running a Go application in a Debian environment. All my development has been carried out on OSX and it ran fine, but I am having a problem running a command on shell from my Go code.
cmdName := "cwebp"
cmdArgs := []string{srcPath, "-o", dstPath}
log.Printf("Executing %s : %+v", cmdName, cmdArgs)
cmd := exec.Command(cmdName, cmdArgs...)
_, err := cmd.StdoutPipe()
if err != nil {
log.Printf("Error: %s", err)
}
err = cmd.Run()
if err != nil {
log.Printf("Error: %s", err)
}
The application process is running from my root user and the command works fine when I run it from shell.
Earlier, I thought that I am facing the issue due to an incorrect PATH environment variable. On printing that before running the command, I get the correct path. Moreover, I have tried to use LookPath and got the correct path as /usr/local/bin/cwebp.

The problem was not with the execution of the command but somehow Go was not able to find the correct library. On checking the output of the command, I got the following:
cwebp: error while loading shared libraries: libwebp.so.5: cannot open shared object file: No such file or directory
This led me to the direction that the installation of libwebp must have been faulty. Earlier, I had built libwebp from source. So, I installed it via apt-get install libwebp-dev and the command ran successfully.

Related

How to set IST time in Remote Machine?

loc, err := time.LoadLocation("Asia/Calcutta")
if err != nil{
fmt.Println(err)
}
now := time.Now().In(loc)
fmt.Println(now)
First I wanted IST time format in my remote machine,
But I got the following error, In local machine working fine.
The error comes only for Remote Machine*
I got an error:
The system cannot find the path specified.
panic: time: missing Location in a call to Time. In
//IST -> UTC + 5:30
fixedZone := time.FixedZone("IST", 5*3600+1800)
in := time.Now().In(fixedZone)
log.Println(in)
log.Println(in.Zone())
log.Println("--------------------------")
// my time is UTC+8:00
name, offset := time.Now().Zone()
log.Println(name)
log.Println(offset)
2022/08/24 17:53:35 2022-08-24 15:23:35.0625673 +0530 IST
2022/08/24 17:53:35 IST 19800
2022/08/24 17:53:35 --------------------------
2022/08/24 17:53:35 CST
2022/08/24 17:53:35 28800
This generally may happen when the function time.LoadLocation() is called and it looks for a reference for the time zoneinfo and the same is not available on the Remote machine. You should check as to where you are running this program; the Remote machine also contains a valid path for the zoneinfo.zip file .
If you were using this earlier and have recently upgraded to a new version for Go,also update the zoneinfo.zip file to the latest version from the golang repository on the same remote machine as well.
I would also recommend you to check details on Go Package for func LoadLocation.

A nil pointer panic occurs only in debug mode while calling fmt.Sprintf

What version of Go are you using (go version)?
$ go version
go version go1.18 darwin/amd64
What did you do?
I wrote some simple code below that it try to print a struct
import v1 "k8s.io/api/core/v1"
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
func main() {
data := v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
DeletionTimestamp: nil,
},
}
str := fmt.Sprintf("%#v", data)
fmt.Println(str)
}
What did you expect to see?
same behavior with or without debugger
What did you see instead?
I was intended to print a struct called Namespace which is defined in k8s.io/api/core/v1/types.go, when I run it simply in GoLand(GoLand 2021.3.4) without debug, it runs as expected and the structure of the object had been printed in the console, but when i run it also in Goland but with debug, the program had been interrupted by an error: "bad access: nil dereference". the call stack is:
<autogenerated>:2
fmt.(*pp).handleMethods (print.go:603) fmt
fmt.(*pp).printValue (print.go:723) fmt
fmt.(*pp).printValue (print.go:806) fmt
fmt.(*pp).printValue (print.go:806) fmt
fmt.(*pp).printArg (print.go:712) fmt
fmt.(*pp).doPrintf (print.go:1026) fmt
fmt.Sprintf (print.go:219) fmt
main.main (main.go:97) main
runtime.main (proc.go:250) runtime
runtime.goexit (asm_amd64.s:1571) runtime
- Async Stack Trace
<autogenerated>:2
this is a little bit weird: If this(panic) is a intented behavior, then how can it runs correctly without debugger?
I had the same problem. I tried to upgrade the mac os version, but it did not work.
The main reason is debugserver version,mine before version is:
/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources/debugserver --version
debugserver-#(#)PROGRAM:LLDB PROJECT:lldb-1205.0.27
and, later upgrade debugserver by:
sudo rm -rf /Library/Developer/CommandLineTools
and it will remain to intall again, and the latest version is:
debugserver-#(#)PROGRAM:LLDB PROJECT:lldb-1316.0.9.46
then, everything works.

Golang MSI can't see mounted drives on windows

So I have an windows msi installer that I created by cross compiling from a golang program and installed on a client windows machine. The program sends data out from a folder on the windows machine to a Kinesis stream. The program works fine for all files and folders on the local C:\ drive.
My issue is that when the client is using a mounted NTFS drive ("I://, J://, D://") I get an error message with
level=info msg="Ignoring target I:\\xml with invalid stat: open I:\\xml: The system cannot find the path specified."
No matter the mounted drive (E,F,G, etc) I get the same error message.
I've tried this across multiple clients and received the same error.
So just for background- I'm compiling the program using wine to get it to an msi when building. The program is taking on a SYSTEM USER role and I've already checked permissions on the mounted directories (and made them wide open- still no luck). Been banging my head against the wall over this one for a while so the question is: has anyone else had an issue like this?
Don't know how much this will help but here is the specific portion of GO code I'm using:
p, err := filepath.Abs(t.Path)
if err != nil {
Logger.Infof("Ignoring invalid target path: %s, error: %s", t.Path, err)
continue
}
_, err = os.Lstat(t.Path)
if err != nil {
Logger.Infof("Ignoring target %s with invalid stat: %s", t.Path, err)
continue
}
c.targets = append(c.targets, site.WatchTarget{p, t.SrcTag})
I've tried os.Stat, Lstat and even os.Open to no avail. Just about at the end of my tether on this one.
The ideal result would be a suggestion on any changes you could suggest that would allow a system user from a cross compiled go program to see mounted network drives.

Execute git cmd via node.js on windows fails with err

git is installed on windows and works from any directory (it is in System path)
var child_process = require('child_process');
child_process.exec('git --version', function(err, stdout, stderr) {
// stdout is correct - git version 1.7.11.msysgit.1
// but err is not null
// err is [Error: Command failed: ] killed: false, code: 1, signal: null }
}
What can cause this?
I had this issue using Windows 7 with msysgit. Two things seem to solve the issue.
First, verify that your Path system variable includes a path to the Git bin directory, and not the cmd directory:
C:\Program Files (x86)\Git\bin
Next, verify you have a recent version of msysgit. I've verified the issue as solved with version 1.8.4-preview:
https://code.google.com/p/msysgit/downloads/list
Restart your shell and try again.
As a workaround you can pass {env:{PATH:"C:\path to\node"}} as the options argument to process.exec(cmd, opts, callback) as described in docs. I am surprised that the Windows path doesn't carry over but have no explanation why.

Updating go websocket library to latest version

I am running the Go compiler on Ubuntu, installed using sudo apt-get install golang
I've successfully compiled and executed the code for a Trivial example server (See http://golang.org/pkg/websocket/#Handler )
package main
import (
"http"
"io"
"websocket"
)
// Echo the data received on the Web Socket.
func EchoServer(ws *websocket.Conn) {
io.Copy(ws, ws);
}
func main() {
http.Handle("/echo", websocket.Handler(EchoServer));
err := http.ListenAndServe(":12345", nil);
if err != nil {
panic("ListenAndServe: " + err.String())
}
}
However, I fail to connect to the server with my version of Chromium (16.0.912.77). I assume Chrome has implemented the RFC 6455 Websocket (version 13), but that the go websocket library in the Ubuntu golang package is out of date.
So, my question is: How can I update only the websocket package to the latest version?
The latest version of the Go websocket package is net/websocket at code.google.com/p/go.net/websocket, which requires the Go 1 weekly development release.
For Ubuntu golang-weekly: Ubuntu PPA packages for Go.
For weekly development release documentation: Go Programming Language.
I guess the version of Go in Ubuntu package repository is probably r60.3 (or so), which is a bit old now. Use latest weekly, change the code to:
package main
import (
"code.google.com/p/go.net/websocket"
"io"
"net/http"
)
// Echo the data received on the Web Socket.
func EchoServer(ws *websocket.Conn) {
io.Copy(ws, ws)
}
func main() {
http.Handle("/echo", websocket.Handler(EchoServer))
err := http.ListenAndServe(":12345", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
Moreover in the websocket package s/ParseRequestURI/ParseRequest/, then it seems to work here.(1)
Update: Sorry, I wrote/read too fast, it doesn't seem to work, the page shows: "not websocket protocol" (here is Chrome 18.0.1025.33 beta on 64b Ubuntu 10.04)
Update 2012-08-22: The above (1) note about editing the websocket package doesn't hold anymore. The websocket package has been meanwhile updated and the example (main) code above now compiles w/o problems. Anyway, I haven't tested if it afterwards does what is should or not, sorry.

Resources