-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoto.go
More file actions
85 lines (74 loc) · 2 KB
/
Copy pathgoto.go
File metadata and controls
85 lines (74 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"strings"
)
var chromeBinaries = [...]string{
"/usr/bin/google-chrome",
"/usr/bin/google-chrome-stable",
"/usr/local/bin/chrome",
}
func getChromeBinary() (string, error) {
host, _ := os.Hostname()
if host == "stamer" {
return "google-chrome-stable --enable-features=AcceleratedVideoEncoder", nil
}
for _, bin := range chromeBinaries {
_, err := os.Stat(bin)
if err == nil {
return bin, nil
}
}
return "", errors.New("no workable chrome binary found")
}
const corpAppSuffix = "corp.google.com" // example
func main() {
urlToggle := flag.Bool("u", false, "don't interpret link as go/link")
corpToggle := flag.Bool("c", false, "interpret as corp app")
googleToggle := flag.Bool("g", false, "interpret as google app")
profileOverride := flag.Int("p", 1, "override chrome profile index")
flag.Parse()
bin, err := getChromeBinary()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(-1)
}
link := prepLink(flag.Arg(0), *urlToggle, *corpToggle, *googleToggle)
prompt := fmt.Sprintf("%s --profile-directory='Profile %d'", bin, *profileOverride)
if len(link) > 0 {
prompt = fmt.Sprintf("%s --app='%s'", prompt, link)
}
// Apparently chrome inspects it's process parent and simply spawning a process directly fails.
// Spawning from with a shell as parent is somehow OK.
cmd := exec.Command("/bin/sh", "-c", prompt)
cmd.Stdin = os.Stdin
_ = cmd.Run()
}
func prepLink(link string, url, corp, google bool) string {
if strings.HasPrefix(link, "localhost:") {
return fmt.Sprintf("http://%s", link)
}
if url {
if !strings.HasPrefix(link, "https://") {
link = fmt.Sprintf("https://%s", link)
}
return link
}
if corp {
return fmt.Sprintf("https://%s.%s", link, corpAppSuffix)
}
if google {
return fmt.Sprintf("https://%s.%s", link, "google.com")
}
if len(link) > 0 {
if !strings.HasPrefix(link, "go/") {
link = fmt.Sprintf("go/%s", link)
}
return fmt.Sprintf("http://%s", link)
}
return link
}