skratchdot / open-golang Goto Github PK
View Code? Open in Web Editor NEWOpen a file, directory, or URI using the OS's default application for that object type. Optionally, you can specify an application to use.
License: MIT License
Open a file, directory, or URI using the OS's default application for that object type. Optionally, you can specify an application to use.
License: MIT License
RunWith should execute a secure version of cmd.exe
, not any cmd
.
open-golang/open/exec_windows.go
Line 30 in eef8423
See https://msrc.microsoft.com/blog/2014/04/ms14-019-fixing-a-binary-hijacking-via-cmd-or-bat-file/
Testcase:
package main
import (
"github.com/skratchdot/open-golang/open"
)
func main() {
open.Start("http://127.0.0.1/?qwe&rty")
}
When this program is run on Windows, the URL http://127.0.0.1/?qwe
is opened.
The open method references xdg-open relative to package source. So the tests pass, but this doesn't work in a shipping product where the executable may be moved or not even be shipped with the source.
open.go:66> app := path.Join(path.Dir(file), "..", "vendor", "xdg-open")
In shipping code, I use:
open.go:66> app := "./xdg-open"
and ship xdg-open alongside the executable.
A different solution would be to default to "xdg-open" (assume it's in the path) and allow it to be settable by the user to another location.
Hello,
I was wondering if you were open to add a function or a flag to Wait for the opened program to close.
It's possible on macOS, with -W (-n and -F could also be used):
-W Causes open to wait until the applications it opens (or that were already open) have exited. > Use with the -n flag to allow open to function as an appropriate app for the $EDITOR environment
-n Open a new instance of the application(s) even if one is already running.`
-F Opens the application "fresh," that is, without restoring windows. Saved persistent state is lost, except for Untitled documents.`
I have a use case of this and discovered your library, but I resorted to going back to exec.Command() to be able to specify this.
Now, xdg-open doesn't have a wait for feature, and I don't think windows does either.
So I understand if this feature is not desired.
Kind regards,
I use next service file;
[Unit]
Description=Goserver
[Service]
Type=simple
Restart=always
RestartSec=3
Environment=GOPATH=/home/user/goserver/
Environment=BROWSER=gvfs-open
WorkingDirectory=/home/user
ExecStart=/usr/bin/go run /home/user/goserver/totalmanager.go
[Install]
WantedBy=multi-user.target
The script with open-golang is inside totalmanager.go
If I run my script "go run goserver/totalmanager.go" it works!
But if I use "sudo systemctl start totalmanager.service" it doesn't work!
May be something wrong with Environment?
We have a CLI tool that runs code like this:
if err := open.Run(url); err != nil {
utils.Log("Unable to launch a browser")
utils.LogDebugError(err)
}
It works fine in Linux, MacOS and in a Windows command prompt. However, it fails when it executes inside Git Bash (which is essentially a MSYS/Cygwin-like environment). The failure message is "Incorrect function".
My suspicion here is that the Windows implementation simply doesn't work in MSYS/Cygwin-style environments. Simply running start $URL
in Git Bash opened up a browser as expected. Also, prefixing the command with winpty
worked (which is included with Git Bash by default โ see https://github.com/rprichard/winpty).
open.Run("https://coding.net/oauth_authorize.html?client_id=123456&redirect_uri=http://127.0.0.1/callback&response_type=code&scope=user,project")
Browser is URL :https://coding.net/oauth_authorize.html?client_id=123456^&redirect_uri=http://127.0.0.1/callback^&response_type=code^&scope=user,project
I don't think it should be replaced, or it's a mistake here.
OS:windows
Hi, I am a newb to both golang and Windows development so forgive a possibly stupid question.
First, this is a very useful library - thanks to dev(s).
Invoking Start or Run causes a blank CMD shell to briefly appear with focus. It does go away quickly but seems to upset users.
Is there a way to suppress the CMD shell in a way that at least it is not visible to the user. Something appearing on the taskbar would be fine.
Thank you!
Note that WSL environments should invoke powershell.exe Start <pth>
. I've updated my little karp utility to account for this.
I've had success in the past vendoring dependencies with git submodules (e.g. in my https://github.com/mcandre/go-ios7crypt project). However, when I try to vendor open-golang in my DuckDuckGo command line launcher project, something breaks. Neither go build
nor go get
work for ddg.
Source: https://github.com/mcandre/mcandre/tree/master/go/ddg
Am I doing something wrong?
Location: open\exec_windows.go:10:2
I can get open-golang to open files (a pdf in my browser) and urls, but not folders. I want to open a folder in Windows Explorer.
The readme says:
Open a file, directory, or URI using the OS's default application
so I assume directories should be supported.
// In cmd.exe, I setup and run the program.
// Install dependencies:
// go get "github.com/skratchdot/open-golang/open/..."
// Ensure directory exists:
// mkdir c:\scratch
// Build:
// go build open_with_open.go
// open_with_open.exe
package main
import (
"fmt"
"github.com/skratchdot/open-golang/open"
)
func main() {
// None of these options work.
exe_path := `c:/scratch`
exe_path = `c:/scratch/`
exe_path = `c:\scratch`
// I see this output.
fmt.Printf("Trying to open folder: %v\n", exe_path)
// nothing happens except command prompt loses focus. I see no error.
err := open.Start(exe_path)
if err != nil {
panic(err)
}
}
I want the equivalent of this in a command prompt:
start c:\scratch
Is open-golang the wrong package? (I'm new to golang, so forgive my ignorance.)
This needs to be released with a proper semver tag.
go 1.19
$ go get -v github.com/haya14busa/goplay/cmd/goplay
github.com/haya14busa/goplay/cmd/goplay imports
github.com/skratchdot/open-golang/open: cannot find module providing package github.com/skratchdot/open-golang/open
Error in open.Start() exec: "start": executable not found in %PATH%
However, running start in both bash and cmd work. Probably and issue with the exec package however I'm not certain.
When using open.Run to open an application in macOS, the error printed is exit status 1
This does not mean much, is it possible to enhance the error making it more meaningful ?
Hey is it possible to open url in a new Window not in a tab ?
hey,
I wanted to be able to open a new incognito window.
I also wanted to pass in environment variable. The golang runtime might pass them around, but i feel like this is insecure. is it ? its rather important i can make sure my environment variable reaches the started window.
Like #13 says i think some options capabilities might do it.
When opening a folder location, the window (at least on windows) appears in background behind other applications. Is there any way to put it in foreground?
Hey,
I've been trying to open a simple JPEG. but I get no error and nothing happens. xdg-open
is installed and I've tested to manually open the JPEG, which worked just fine. Any clue why this might happen?
Hi, I'm working on application for mobile and use rclone library to sync files from cloud providers. The rclone library depends on yours, and uses to open OAuth requests to upstream provider. Since there is no support for mobile within your library I can't proceed using rclone. I would really appreciate if you'll add support to open urls in mobile environment. I know that it is possible since I'm using fyne.io For example, you can see how they are doing open call for android and ios.
Typically I run 2-3 profiles in {firefox,chromium} to distuinigish between {work,personal,other projects}. Is there a way of discovering the running profiles in order to choose which browser to open?
I've written some very simple code to just open a text file
err = open.Run("C:/test.txt")
if err != nil {
return err
}
When I run this executable from the command line, it works fine. When I run another executable which then runs this executable, the code no longer works, but there is no error.
The weird thing is that when mypath
is a directory, windows will happily open file explorer at that directory, even when being called from another executable:
err = open.Run("C:/")
if err != nil {
return err
}
^ This works fine. But any file will not.
If there's some permissions issue going on that's fine but shouldn't this return an error if it's not able to complete?
Any insight into what's going on here would be welcomed too.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.