|
| 1 | +// Copyright 2024 KusionStack Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package browser |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/url" |
| 19 | + "os/exec" |
| 20 | + "runtime" |
| 21 | + |
| 22 | + "github.com/pkg/browser" |
| 23 | +) |
| 24 | + |
| 25 | +// nativeBrowser should implement the Browser interface. |
| 26 | +var _ Browser = nativeBrowser{} |
| 27 | + |
| 28 | +// NewNativeBrowser creates and returns a Browser that will attempt to interact |
| 29 | +// with the browser-launching mechanisms of the operating system where the |
| 30 | +// program is currently running. |
| 31 | +func NewNativeBrowser() Browser { |
| 32 | + return nativeBrowser{} |
| 33 | +} |
| 34 | + |
| 35 | +type nativeBrowser struct{} |
| 36 | + |
| 37 | +// OpenURL opens given url in a new browser tab. |
| 38 | +func (b nativeBrowser) OpenURL(URL string) error { |
| 39 | + _, err := url.Parse(URL) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + if runtime.GOOS == "linux" { |
| 44 | + _, err = exec.LookPath("xdg-open") |
| 45 | + if err != nil { |
| 46 | + return nil |
| 47 | + } |
| 48 | + } |
| 49 | + return browser.OpenURL(URL) |
| 50 | +} |
0 commit comments