Skip to content

Commit

Permalink
Moved configuration stuff to config.go.
Browse files Browse the repository at this point in the history
  • Loading branch information
abrander committed Jun 15, 2016
1 parent 206f71e commit 956a4db
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 55 deletions.
64 changes: 64 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"syscall"

"github.com/BurntSushi/toml"
)

type (
config struct {
StatusPath string `toml:"status"`
ListenPath string `toml:"listen"`
}
)

var (
configs map[string]config
)

const (
// configFilename is the name of the configuration file loaded from the
// user's home directory.
configFilename = ".phpfpmtop.conf"

// defaultConfig will be written to configFilename if none is found.
defaultConfig = `# Default section will be used if there's no arguments given to phpfpmtop.
[default]
listen = "/var/run/php5-fpm.sock" # The value of the "listen" option in the PHP-FPM-pool config.
status = "/status" # The value of the "status" option.
`
)

func init() {
home := os.Getenv("HOME")

if runtime.GOOS == "windows" {
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
}

p := path.Join(home, configFilename)
_, err := toml.DecodeFile(p, &configs)

if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT {
// The configuration file was not found. Let's provide a sane
// default for php5-fpm.
configs = make(map[string]config)

ioutil.WriteFile(p, []byte(defaultConfig), 0640)

fmt.Printf("New configuration file written to \033[33m%s\033[0m. Please verify, and restart phpfpmtop.\n\n\033[33m%s\033[0m\n", p, defaultConfig)
os.Exit(1)
} else if err != nil {
fmt.Printf("Error when opening %s: %s", p, err.Error())
os.Exit(1)
}
}
55 changes: 0 additions & 55 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"path"
"runtime"
"sort"
"strings"
"syscall"
"time"
"unsafe"

"github.com/BurntSushi/toml"
)

type (
Expand Down Expand Up @@ -78,58 +73,8 @@ type (
Rows, Columns uint16
XPixel, YPixel uint16
}

config struct {
StatusPath string `toml:"status"`
ListenPath string `toml:"listen"`
}
)

var (
configs map[string]config
)

const (
// configFilename is the name of the configuration file loaded from the
// user's home directory.
configFilename = ".phpfpmtop.conf"

// defaultConfig will be written to configFilename if none is found.
defaultConfig = `# Default section will be used if there's no arguments given to phpfpmtop.
[default]
listen = "/var/run/php5-fpm.sock" # The value of the "listen" option in the PHP-FPM-pool config.
status = "/status" # The value of the "status" option.
`
)

func init() {
home := os.Getenv("HOME")

if runtime.GOOS == "windows" {
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
}

p := path.Join(home, configFilename)
_, err := toml.DecodeFile(p, &configs)

if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT {
// The configuration file was not found. Let's provide a sane
// default for php5-fpm.
configs = make(map[string]config)

ioutil.WriteFile(p, []byte(defaultConfig), 0640)

fmt.Printf("New configuration file written to \033[33m%s\033[0m. Please verify, and restart phpfpmtop.\n\n\033[33m%s\033[0m\n", p, defaultConfig)
os.Exit(1)
} else if err != nil {
fmt.Printf("Error when opening %s: %s", p, err.Error())
os.Exit(1)
}
}

func getTerminalSize() (int, int) {
tty, err := os.Open("/dev/tty")
if err != nil {
Expand Down

0 comments on commit 956a4db

Please sign in to comment.