mirror of
https://github.com/yudai/gotty.git
synced 2026-01-28 20:26:10 +01:00
Support profile files to customize hterm
This commit is contained in:
parent
afd40ea15d
commit
45f65bfc29
7 changed files with 78 additions and 7 deletions
36
app/app.go
36
app/app.go
|
|
@ -3,12 +3,16 @@ package app
|
|||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/big"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
|
@ -21,7 +25,9 @@ import (
|
|||
type App struct {
|
||||
options Options
|
||||
|
||||
upgrader *websocket.Upgrader
|
||||
upgrader *websocket.Upgrader
|
||||
|
||||
preferences map[string]interface{}
|
||||
titleTemplate *template.Template
|
||||
}
|
||||
|
||||
|
|
@ -31,16 +37,42 @@ type Options struct {
|
|||
PermitWrite bool
|
||||
Credential string
|
||||
RandomUrl bool
|
||||
ProfileFile string
|
||||
TitleFormat string
|
||||
Command []string
|
||||
}
|
||||
|
||||
const DefaultProfileFilePath = "~/.gotty"
|
||||
|
||||
func New(options Options) (*App, error) {
|
||||
titleTemplate, err := template.New("title").Parse(options.TitleFormat)
|
||||
if err != nil {
|
||||
return nil, errors.New("Title format string syntax error")
|
||||
}
|
||||
|
||||
prefString := []byte{}
|
||||
prefPath := options.ProfileFile
|
||||
if options.ProfileFile == DefaultProfileFilePath {
|
||||
usr, _ := user.Current()
|
||||
prefPath = usr.HomeDir + "/.gotty"
|
||||
}
|
||||
if _, err = os.Stat(prefPath); os.IsNotExist(err) {
|
||||
if options.ProfileFile != DefaultProfileFilePath {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
log.Printf("Loading profile path: %s", prefPath)
|
||||
prefString, _ = ioutil.ReadFile(prefPath)
|
||||
}
|
||||
if len(prefString) == 0 {
|
||||
prefString = []byte(("{}"))
|
||||
}
|
||||
var prefMap map[string]interface{}
|
||||
err = json.Unmarshal(prefString, &prefMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &App{
|
||||
options: options,
|
||||
|
||||
|
|
@ -49,6 +81,8 @@ func New(options Options) (*App, error) {
|
|||
WriteBufferSize: 1024,
|
||||
Subprotocols: []string{"gotty"},
|
||||
},
|
||||
|
||||
preferences: prefMap,
|
||||
titleTemplate: titleTemplate,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue