Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLester committed Jul 12, 2023
1 parent 0173b22 commit ee81fdc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
10 changes: 8 additions & 2 deletions cmd/ntpal/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ import (

const daemonName = "ntpald"

var (
SocketPath = fmt.Sprintf("/var/%s.sock", daemonName)
PidPath = fmt.Sprintf("/var/run/%s.pid", daemonName)
LogPath = fmt.Sprintf("/var/log/%s.log", daemonName)
)

var daemonCtx = &daemon.Context{
PidFileName: fmt.Sprintf("/var/run/%s.pid", daemonName),
PidFileName: PidPath,
PidFilePerm: 0644,
LogFileName: fmt.Sprintf("/var/log/%s.log", daemonName),
LogFileName: LogPath,
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
Expand Down
31 changes: 18 additions & 13 deletions cmd/ntpal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/AndrewLester/ntpal/pkg/ntpal"
"github.com/sevlyar/go-daemon"
)

const defaultConfigPath = "/etc/ntp.conf"
Expand Down Expand Up @@ -52,32 +51,38 @@ func main() {
if query != "" {
handleQueryCommand(system, query, queryMessages)
} else {
process, _ := daemonCtx.Search()

if !noDaemon {
if process != nil {
handleNTPalUI(SocketPath)
return
}

process, err := daemonCtx.Reborn()
if err != nil {
if errors.Is(err, daemon.ErrWouldBlock) {
handleNTPalUI(socketPath)
return
}
if errors.Is(err, os.ErrPermission) {
log.Fatal("You do not have permission to start the daemon. Try re-running with sudo.")
return
fmt.Printf("You do not have permission to start the daemon: %v\n", err)
os.Exit(1)
}
log.Fatal("Unable to run: ", err)
fmt.Printf("Error: unable to run: %v\n", err)
os.Exit(1)
}

// Parent process
if process != nil {
fmt.Printf("Daemon process (ntpald) started successfully with PID: %d\n", process.Pid)
return
}

// Child process
defer daemonCtx.Release()

log.Print(strings.Repeat("*", 20))
log.Print("ntpald started", os.Args)
} else {
process, _ := daemonCtx.Search()
if process != nil {
log.Fatal("Stop the ntpal daemon via `ntpal -s` before running in no-daemon mode.")
}
} else if process != nil {
fmt.Println("Stop the ntpal daemon via `ntpal -s` before running in no-daemon mode.")
os.Exit(1)
}

system.Start()
Expand Down
8 changes: 4 additions & 4 deletions cmd/ntpal/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ntpQueryMessage string
type ntpQueryError error
type progressUpdateMessage struct{}

func ntpQueryCommand(m queryCommandModel) tea.Cmd {
func ntpQueryCmd(m queryCommandModel) tea.Cmd {
return func() tea.Msg {
result, err := m.system.Query(m.address, m.messages)
if err != nil {
Expand All @@ -60,15 +60,15 @@ func ntpQueryCommand(m queryCommandModel) tea.Cmd {
}
}

func filterListenCommand(m queryCommandModel) tea.Cmd {
func filterListenCmd(m queryCommandModel) tea.Cmd {
return func() tea.Msg {
<-m.system.FilteredProgress
return progressUpdateMessage{}
}
}

func (m queryCommandModel) Init() tea.Cmd {
return tea.Batch(ntpQueryCommand(m), filterListenCommand(m))
return tea.Batch(ntpQueryCmd(m), filterListenCmd(m))
}

func (m queryCommandModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -87,7 +87,7 @@ func (m queryCommandModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
case progressUpdateMessage:
percentage += 1 / float64(m.messages)
return m, filterListenCommand(m)
return m, filterListenCmd(m)
case ntpQueryMessage:
result = string(msg)
return m, tea.Quit
Expand Down
6 changes: 6 additions & 0 deletions internal/ui/style.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ui

import "github.com/charmbracelet/lipgloss"

var TitleStyle = lipgloss.NewStyle().Inline(true).Bold(true).Foreground(lipgloss.Color("252")).Render
var HelpStyle = lipgloss.NewStyle().Inline(true).Foreground(lipgloss.Color("241")).Render

0 comments on commit ee81fdc

Please sign in to comment.