mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
Merge pull request #210 from bcicen/fix-config-encoding
Fix config encoding
This commit is contained in:
@@ -72,7 +72,7 @@ Option | Description
|
|||||||
`-s` | select initial container sort field
|
`-s` | select initial container sort field
|
||||||
`-scale-cpu` | show cpu as % of system total
|
`-scale-cpu` | show cpu as % of system total
|
||||||
`-v` | output version information and exit
|
`-v` | output version information and exit
|
||||||
`-shell` | specify shell (default: sh)
|
`-shell` | exec shell to use (default: sh)
|
||||||
|
|
||||||
### Keybindings
|
### Keybindings
|
||||||
|
|
||||||
|
|||||||
@@ -90,6 +90,13 @@ func Write() (path string, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remove prior to writing new file
|
||||||
|
if err := os.Remove(path); err != nil {
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
return path, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
|
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return path, fmt.Errorf("failed to open config for writing: %s", err)
|
return path, fmt.Errorf("failed to open config for writing: %s", err)
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package manager
|
package manager
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var ActionNotImplErr = errors.New("action not implemented")
|
||||||
|
|
||||||
type Manager interface {
|
type Manager interface {
|
||||||
Start() error
|
Start() error
|
||||||
Stop() error
|
Stop() error
|
||||||
|
|||||||
@@ -7,29 +7,29 @@ func NewMock() *Mock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mock) Start() error {
|
func (m *Mock) Start() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mock) Stop() error {
|
func (m *Mock) Stop() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mock) Remove() error {
|
func (m *Mock) Remove() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mock) Pause() error {
|
func (m *Mock) Pause() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mock) Unpause() error {
|
func (m *Mock) Unpause() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mock) Restart() error {
|
func (m *Mock) Restart() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mock) Exec(cmd []string) error {
|
func (m *Mock) Exec(cmd []string) error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,29 +7,29 @@ func NewRunc() *Runc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rc *Runc) Start() error {
|
func (rc *Runc) Start() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *Runc) Stop() error {
|
func (rc *Runc) Stop() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *Runc) Remove() error {
|
func (rc *Runc) Remove() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *Runc) Pause() error {
|
func (rc *Runc) Pause() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *Runc) Unpause() error {
|
func (rc *Runc) Unpause() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *Runc) Restart() error {
|
func (rc *Runc) Restart() error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *Runc) Exec(cmd []string) error {
|
func (rc *Runc) Exec(cmd []string) error {
|
||||||
return nil
|
return ActionNotImplErr
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,11 +32,11 @@ func (cs *Mock) Init() {
|
|||||||
rand.Seed(int64(time.Now().Nanosecond()))
|
rand.Seed(int64(time.Now().Nanosecond()))
|
||||||
|
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
cs.makeContainer(3)
|
cs.makeContainer(3, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < 16; i++ {
|
for i := 0; i < 16; i++ {
|
||||||
cs.makeContainer(1)
|
cs.makeContainer(1, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -50,12 +50,28 @@ func (cs *Mock) Wait() struct{} {
|
|||||||
return <-ch
|
return <-ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *Mock) makeContainer(aggression int64) {
|
var healthStates = []string{"starting", "healthy", "unhealthy"}
|
||||||
|
|
||||||
|
func (cs *Mock) makeContainer(aggression int64, health bool) {
|
||||||
collector := collector.NewMock(aggression)
|
collector := collector.NewMock(aggression)
|
||||||
manager := manager.NewMock()
|
manager := manager.NewMock()
|
||||||
c := container.New(makeID(), collector, manager)
|
c := container.New(makeID(), collector, manager)
|
||||||
c.SetMeta("name", makeName())
|
c.SetMeta("name", makeName())
|
||||||
c.SetState(makeState())
|
c.SetState(makeState())
|
||||||
|
if health {
|
||||||
|
var i int
|
||||||
|
c.SetMeta("health", healthStates[i])
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
i++
|
||||||
|
if i >= len(healthStates) {
|
||||||
|
i = 0
|
||||||
|
}
|
||||||
|
c.SetMeta("health", healthStates[i])
|
||||||
|
time.Sleep(12 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
cs.containers = append(cs.containers, c)
|
cs.containers = append(cs.containers, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
6
main.go
6
main.go
@@ -46,7 +46,7 @@ func main() {
|
|||||||
invertFlag = flag.Bool("i", false, "invert default colors")
|
invertFlag = flag.Bool("i", false, "invert default colors")
|
||||||
scaleCpu = flag.Bool("scale-cpu", false, "show cpu as % of system total")
|
scaleCpu = flag.Bool("scale-cpu", false, "show cpu as % of system total")
|
||||||
connectorFlag = flag.String("connector", "docker", "container connector to use")
|
connectorFlag = flag.String("connector", "docker", "container connector to use")
|
||||||
defaultShell = flag.String("shell", "", "default shell")
|
defaultShell = flag.String("shell", "sh", "exec shell to use")
|
||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@@ -65,7 +65,9 @@ func main() {
|
|||||||
|
|
||||||
// init global config and read config file if exists
|
// init global config and read config file if exists
|
||||||
config.Init()
|
config.Init()
|
||||||
config.Read()
|
if err := config.Read(); err != nil {
|
||||||
|
log.Warningf("reading config: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
// override default config values with command line flags
|
// override default config values with command line flags
|
||||||
if *filterFlag != "" {
|
if *filterFlag != "" {
|
||||||
|
|||||||
Reference in New Issue
Block a user